25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

models.py 3.2 KiB

8 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. private = models.BooleanField(default=False, blank=False)
  63. pins = models.ManyToManyField("Pin", related_name="pins", blank=True)
  64. published = models.DateTimeField(auto_now_add=True)
  65. class Pin(models.Model):
  66. submitter = models.ForeignKey(User)
  67. private = models.BooleanField(default=False, blank=False)
  68. url = models.CharField(null=True, blank=True, max_length=256)
  69. referer = models.CharField(null=True, blank=True, max_length=256)
  70. description = models.TextField(blank=True, null=True)
  71. image = models.ForeignKey(Image, related_name='pin')
  72. published = models.DateTimeField(auto_now_add=True)
  73. tags = TaggableManager()
  74. def tag_list(self):
  75. return self.tags.all()
  76. def __unicode__(self):
  77. return '%s - %s' % (self.submitter, self.published)
  78. @receiver(models.signals.post_delete, sender=Pin)
  79. def delete_pin_images(sender, instance, **kwargs):
  80. try:
  81. instance.image.delete()
  82. except Image.DoesNotExist:
  83. pass