models.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # coding=utf-8
  2. import os
  3. from PIL import Image
  4. from django.conf import settings
  5. from django.db import models
  6. from utils.file_operation import UploadFile, DeleteFile
  7. class UploadManager(models.Manager):
  8. def _addnew(self, file, path):
  9. width = None
  10. height = None
  11. path = UploadManager.calculatePath(path)
  12. filename = UploadFile(file, path)
  13. fullname = "%s%s" % (settings.MEDIA_ROOT, filename)
  14. size = os.path.getsize(fullname)
  15. try:
  16. img = Image.open(fullname)
  17. width, height = img.size
  18. #上传产品图片, 缩略图压缩宽或高最大200
  19. if width > 1440 and path == 'product_image/':
  20. img = img.resize((1440, int((height / width) * 1440)), Image.ANTIALIAS)
  21. img.save(fullname)
  22. width, height = img.size
  23. except:
  24. pass
  25. instance = self.model(
  26. picture="%s%s" % (settings.MEDIA_URL, filename),
  27. width=width or 0,
  28. height=height or 0,
  29. file_size="%.2f" % (float(size) / 1024),
  30. )
  31. instance.save()
  32. return instance
  33. def _update(self, file, path, upload):
  34. DeleteFile(upload.picture)
  35. width = None
  36. height = None
  37. path = UploadManager.calculatePath(path)
  38. filename = UploadFile(file, path)
  39. fullname = "%s%s" % (settings.MEDIA_ROOT, filename)
  40. size = os.path.getsize(fullname)
  41. try:
  42. img = Image.open(fullname)
  43. width, height = img.size
  44. # 缩略图压缩宽或高最大200
  45. # if width > 1440:
  46. # img = img.resize((1440, int((height / width) * 1440)), Image.ANTIALIAS)
  47. # img.save(fullname)
  48. # width, height = img.size
  49. except:
  50. pass
  51. Upload.objects.filter(id=upload.id).update(
  52. picture="%s%s" % (settings.MEDIA_URL, filename),
  53. width=width,
  54. height=height,
  55. file_size="%.2f" % (float(size) / 1024),
  56. )
  57. @staticmethod
  58. def calculatePath(path='user_image'):
  59. if path == 'commodity_image':
  60. return commodity_image
  61. elif path == 'user_image':
  62. return user_image
  63. commodity_image = "product_image/"
  64. user_image = "user_image/"
  65. class Upload(models.Model):
  66. picture = models.CharField(verbose_name=u'图片路径', max_length=250)
  67. width = models.IntegerField(verbose_name=u"图片宽度", blank=True, default=0)
  68. height = models.IntegerField(verbose_name=u"图片高度", blank=True, default=0)
  69. create_time = models.DateTimeField(verbose_name=u'上传时间', auto_now_add=True, editable=False)
  70. file_size = models.FloatField(verbose_name="文件大小", blank=True, default=0)
  71. objects = UploadManager()
  72. class Meta:
  73. db_table = 'system_upload'
  74. verbose_name = u'文件上传'
  75. ordering = ['-create_time']
  76. index_together = (
  77. 'create_time',
  78. )
  79. default_permissions = ()
  80. def del_images(self):
  81. picture = self.picture
  82. self.delete()
  83. DeleteFile(picture)
  84. def get_path(self):
  85. return '%s%s' % (settings.SERVER_DOMAIN, self.picture)
  86. def get_picture(self):
  87. return self.picture