attachment.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from django.conf import settings
  2. from django.core.files import File
  3. from django.core.files.base import ContentFile
  4. from django.core.urlresolvers import reverse
  5. from django.db import models
  6. from django.utils import six, timezone
  7. from django.utils.crypto import get_random_string
  8. from PIL import Image
  9. class Attachment(models.Model):
  10. uuid = models.CharField(max_length=64)
  11. filetype = models.ForeignKey('AttachmentType')
  12. post = models.ForeignKey(
  13. 'Post',
  14. blank=True,
  15. null=True,
  16. on_delete=models.SET_NULL
  17. )
  18. uploaded_on = models.DateTimeField(default=timezone.now)
  19. uploader = models.ForeignKey(
  20. settings.AUTH_USER_MODEL,
  21. blank=True,
  22. null=True,
  23. on_delete=models.SET_NULL
  24. )
  25. uploader_name = models.CharField(max_length=255)
  26. uploader_slug = models.CharField(max_length=255)
  27. uploader_ip = models.GenericIPAddressField()
  28. filename = models.CharField(max_length=255)
  29. thumbnail = models.ImageField(blank=True, null=True, upload_to='attachments')
  30. image = models.ImageField(blank=True, null=True, upload_to='attachments')
  31. file = models.FileField(blank=True, null=True, upload_to='attachments')
  32. downloads = models.PositiveIntegerField(default=0)
  33. @classmethod
  34. def generate_new_uuid(cls):
  35. return get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)
  36. @property
  37. def is_image(self):
  38. return bool(self.image)
  39. @property
  40. def is_file(self):
  41. return not self.is_image
  42. def get_absolute_url(self):
  43. return reverse('misago:attachment', kwargs={
  44. 'pk': self.pk,
  45. 'uuid': self.uuid,
  46. })
  47. def get_thumbnail_url(self):
  48. if self.is_image:
  49. return reverse('misago:attachment-thumbnail', kwargs={
  50. 'pk': self.pk,
  51. 'uuid': self.uuid,
  52. })
  53. else:
  54. return None
  55. def set_file(self, upload):
  56. file_secret = get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)
  57. self.file = File(upload, '.'.join([file_secret, self.filetype.extensions_list[0]]))
  58. def set_image(self, upload):
  59. fileformat = self.filetype.extensions_list[0]
  60. image_secret = get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)
  61. image_filename = '.'.join([image_secret, fileformat])
  62. self.image = File(upload, image_filename)
  63. thumbnail = Image.open(upload)
  64. thumbnail.thumbnail((500, 500))
  65. if six.PY3:
  66. thumb_stream = six.StringIO()
  67. else:
  68. thumb_stream = six.BytesIO()
  69. thumbnail.save(thumb_stream, fileformat)
  70. thumb_secret = get_random_string(settings.MISAGO_ATTACHMENT_SECRET_LENGTH)
  71. thumb_filename = '.'.join([thumb_secret, fileformat])
  72. self.thumbnail = ContentFile(thumb_stream, thumb_filename)