Advertisement
Guest User

Resize File

a guest
Mar 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. #format_checker.py
  2.  
  3. from django.db.models import FileField
  4. from django.forms import forms
  5. from django.template.defaultfilters import filesizeformat
  6. from django.utils.translation import ugettext_lazy as _
  7.  
  8.  
  9. class ContentTypeRestrictedFileField(FileField):
  10.  
  11.     """
  12.        Same as FileField, but you can specify:
  13.        * content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
  14.        * max_upload_size - a number indicating the maximum file size allowed for upload.
  15.        2.5MB - 2621440
  16.        5MB - 5242880
  17.        10MB - 10485760
  18.        20MB - 20971520
  19.        50MB - 5242880
  20.        100MB 104857600
  21.        250MB - 214958080
  22.        500MB - 429916160
  23.    """
  24.  
  25.     def __init__(self, *args, **kwargs):
  26.  
  27.         self.content_types = kwargs.pop("content_types", None)
  28.         self.max_upload_size = kwargs.pop("max_upload_size", "2621440")
  29.  
  30.         super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)
  31.  
  32.  
  33.     def clean(self, *args, **kwargs):
  34.         data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)
  35.  
  36.         file = data.file
  37.         try:
  38.             content_type = file.content_type
  39.             if content_type in self.content_types:
  40.                 if file._size > self.max_upload_size:
  41.                     raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (
  42.                         filesizeformat(self.max_upload_size), filesizeformat(file._size)))
  43.                 else:
  44.                     raise forms.ValidationError(_('Filetype not supported.'))
  45.         except AttributeError:
  46.             pass
  47.  
  48.         return data
  49.  
  50.  
  51.  
  52. #models.py
  53.  
  54. from PIL import Image
  55.  
  56. class Menu(models.Model):
  57.  
  58.     name = models.CharField(max_length=100)
  59.     price = models.IntegerField()
  60.     image = ContentTypeRestrictedFileField(
  61.         upload_to='menu/image/',
  62.         content_types=[[
  63.             'image/jpeg',
  64.             'image/jpg',
  65.             'image/png'
  66.             ]
  67.         ],
  68.         max_upload_size=2621440,
  69.         blank=True,
  70.         null=True,
  71.         help_text="Max size is 2.5MB"
  72.         )
  73.     note = models.TextField(blank=True)
  74.  
  75.     objects = models.Manager()
  76.  
  77.     def __str__(self):
  78.         return '{0}: {1}'.format(self.kelurahan.name, self.name)
  79.  
  80.    
  81.     def save(self, *args, **kwargs):
  82.         image = Image.open(self.image)
  83.  
  84.         image_ext = os.path.splitext(self.image.path)[1][1:]
  85.  
  86.  
  87.         image = image.resize((240, 480), Image.ANTIALIAS)
  88.         self.image = image
  89.  
  90.         super(Menu, self).save(*args, **kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement