Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

98 rindas
2.9 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. for size in settings.IMAGE_SIZES.keys():
  32. Thumbnail.objects.get_or_create_at_size(image.pk, size)
  33. return image
  34. class Image(BaseImage):
  35. objects = ImageManager()
  36. class Sizes:
  37. standard = "standard"
  38. thumbnail = "thumbnail"
  39. square = "square"
  40. class Meta:
  41. proxy = True
  42. @property
  43. def standard(self):
  44. return Thumbnail.objects.get(
  45. original=self, size=self.Sizes.standard
  46. )
  47. @property
  48. def thumbnail(self):
  49. return Thumbnail.objects.get(
  50. original=self, size=self.Sizes.thumbnail
  51. )
  52. @property
  53. def square(self):
  54. return Thumbnail.objects.get(
  55. original=self, size=self.Sizes.square
  56. )
  57. class Pin(models.Model):
  58. submitter = models.ForeignKey(User)
  59. url = models.CharField(null=True, blank=True, max_length=256)
  60. # origin is tha same as referer but not work,
  61. # should be removed some day
  62. origin = models.CharField(null=True, blank=True, max_length=256)
  63. referer = models.CharField(null=True, blank=True, max_length=256)
  64. description = models.TextField(blank=True, null=True)
  65. image = models.ForeignKey(Image, related_name='pin')
  66. published = models.DateTimeField(auto_now_add=True)
  67. tags = TaggableManager()
  68. def tag_list(self):
  69. return self.tags.all()
  70. def __unicode__(self):
  71. return '%s - %s' % (self.submitter, self.published)
  72. @receiver(models.signals.post_delete, sender=Pin)
  73. def delete_pin_images(sender, instance, **kwargs):
  74. try:
  75. instance.image.delete()
  76. except Image.DoesNotExist:
  77. pass