Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

109 Zeilen
3.2 KiB

  1. import requests
  2. from io import BytesIO
  3. from django.conf import settings
  4. from django.core.files.uploadedfile import InMemoryUploadedFile
  5. from django.db import models
  6. from django.dispatch import receiver
  7. from django_images.models import Image as BaseImage, Thumbnail
  8. from taggit.managers import TaggableManager
  9. from users.models import User
  10. class ImageManager(models.Manager):
  11. _default_ua = {
  12. 'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) '
  13. 'AppleWebKit/537.36 (KHTML, like Gecko) '
  14. 'Chrome/48.0.2564.82 Safari/537.36',
  15. }
  16. # FIXME: Move this into an asynchronous task
  17. def create_for_url(self, url, referer=None):
  18. file_name = url.split("/")[-1].split('#')[0].split('?')[0]
  19. buf = BytesIO()
  20. headers = dict(self._default_ua)
  21. if referer is not None:
  22. headers["Referer"] = referer
  23. response = requests.get(url, headers=headers)
  24. buf.write(response.content)
  25. obj = InMemoryUploadedFile(buf, 'image', file_name,
  26. None, buf.tell(), None)
  27. # create the image and its thumbnails in one transaction, removing
  28. # a chance of getting Database into a inconsistent state when we
  29. # try to create thumbnails one by one later
  30. image = self.create(image=obj)
  31. Thumbnail.objects.get_or_create_at_sizes(image, settings.IMAGE_SIZES.keys())
  32. return image
  33. class Image(BaseImage):
  34. objects = ImageManager()
  35. class Sizes:
  36. standard = "standard"
  37. thumbnail = "thumbnail"
  38. square = "square"
  39. class Meta:
  40. proxy = True
  41. @property
  42. def standard(self):
  43. return Thumbnail.objects.get(
  44. original=self, size=self.Sizes.standard
  45. )
  46. @property
  47. def thumbnail(self):
  48. return Thumbnail.objects.get(
  49. original=self, size=self.Sizes.thumbnail
  50. )
  51. @property
  52. def square(self):
  53. return Thumbnail.objects.get(
  54. original=self, size=self.Sizes.square
  55. )
  56. class Board(models.Model):
  57. class Meta:
  58. unique_together = ("submitter", "name")
  59. index_together = ("submitter", "name")
  60. submitter = models.ForeignKey(User)
  61. name = models.CharField(max_length=128, blank=False, null=False)
  62. pins = models.ManyToManyField("Pin", related_name="pins", blank=True)
  63. published = models.DateTimeField(auto_now_add=True)
  64. class Pin(models.Model):
  65. submitter = models.ForeignKey(User)
  66. url = models.CharField(null=True, blank=True, max_length=256)
  67. # origin is tha same as referer but not work,
  68. # should be removed some day
  69. origin = models.CharField(null=True, blank=True, max_length=256)
  70. referer = models.CharField(null=True, blank=True, max_length=256)
  71. description = models.TextField(blank=True, null=True)
  72. image = models.ForeignKey(Image, related_name='pin')
  73. published = models.DateTimeField(auto_now_add=True)
  74. tags = TaggableManager()
  75. def tag_list(self):
  76. return self.tags.all()
  77. def __unicode__(self):
  78. return '%s - %s' % (self.submitter, self.published)
  79. @receiver(models.signals.post_delete, sender=Pin)
  80. def delete_pin_images(sender, instance, **kwargs):
  81. try:
  82. instance.image.delete()
  83. except Image.DoesNotExist:
  84. pass