Tutorials: models.py

File models.py, 2.8 KB (added by Netai Nayek, 10 years ago)

create thumbnail on image upload this is so simple

Line 
1from django.db import models
2from datetime import datetime, timedelta
3
4class User(models.Model):
5 SEX = (
6 ('M', 'Male'),
7 ('F', 'Female'),
8 )
9 fname = models.CharField(max_length=255,verbose_name=u"First Name")
10 lname= models.CharField(max_length=255)
11 email=models.EmailField(max_length=255)
12 mobile= models.CharField(max_length=20)
13 skype= models.CharField(max_length=20,null=True,blank=True)
14 gender = models.CharField(max_length=1,choices=SEX,blank=False,default="")
15 dob = models.DateField()
16 qualification = models.ForeignKey('Qualification')
17 website = models.URLField(max_length=200,null=True,blank=True)
18 address = models.TextField(null=True,blank=True)
19 skills = models.ForeignKey('Skills')
20 experience = models.IntegerField(null=True,blank=True,choices=((int(x), x) for x in range(1,7)))
21 remarks = models.TextField(null=True,blank=True)
22 join_date=models.DateTimeField(editable=False,default=datetime.now())
23 profile_pic = models.ImageField(upload_to = 'user/',null=True,blank=True)
24 thumb_pic = models.ImageField(upload_to = 'user/thumb/',null=True,blank=True)
25
26 def delete(self, *args, **kwargs):
27 storage, path = self.profile_pic.storage, self.profile_pic.path
28 storage1, path1 = self.thumb_pic.storage, self.thumb_pic.path
29 super(User, self).delete(*args, **kwargs)
30 storage.delete(path)
31 storage1.delete(path1)
32
33 def save(self, *args, **kwargs):
34 try:
35 this = User.objects.get(id=self.id)
36 create_thumbnail(self)
37 if this.profile_pic != self.profile_pic:
38 this.profile_pic.delete(save=False)
39 this.thumb_pic.delete(save=False)
40 except: pass
41 super(User, self).save(*args, **kwargs)
42
43class Qualification(models.Model):
44 qualification_name= models.CharField(max_length=200)
45 def __str__(self):
46 return '%s' % (self.qualification_name)
47
48class Skills(models.Model):
49 skills= models.CharField(max_length=200)
50 def __str__(self):
51 return '%s' % (self.skills)
52
53def default_start_time():
54 now = datetime.now()
55 start = now.replace(hour=22, minute=0, second=0, microsecond=0)
56 return start if start > now else start + timedelta(days=1)
57
58def create_thumbnail(obj):
59 if not obj.profile_pic:
60 return
61 from PIL import Image
62 from cStringIO import StringIO
63 from django.core.files.uploadedfile import SimpleUploadedFile
64 import os
65 THUMBNAIL_SIZE = (200,200)
66 DJANGO_TYPE = obj.profile_pic.file.content_type
67 if DJANGO_TYPE == 'image/jpeg':
68 PIL_TYPE = 'jpeg'
69 FILE_EXTENSION = 'jpg'
70 elif DJANGO_TYPE == 'image/png':
71 PIL_TYPE = 'png'
72 FILE_EXTENSION = 'png'
73 image = Image.open(StringIO(obj.profile_pic.read()))
74 image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
75 temp_handle = StringIO()
76 image.save(temp_handle, PIL_TYPE)
77 temp_handle.seek(0)
78 suf = SimpleUploadedFile(os.path.split(obj.profile_pic.name)[-1],temp_handle.read(), content_type=DJANGO_TYPE)
79 obj.thumb_pic.save('%s.%s'%(os.path.splitext(suf.name)[0],FILE_EXTENSION), suf, save=False)
Back to Top