1 #!/usr/bin/python2.5 |
1 #!/usr/bin/python2.5 |
|
2 |
|
3 import os |
|
4 import PIL |
|
5 |
|
6 from pytask.taskapp.utilities.helper import get_key |
2 |
7 |
3 from django import forms |
8 from django import forms |
4 from pytask.taskapp.models import GENDER_CHOICES, Profile |
9 from pytask.taskapp.models import GENDER_CHOICES, Profile |
5 from registration.forms import RegistrationFormUniqueEmail |
10 from registration.forms import RegistrationFormUniqueEmail |
6 from registration.models import RegistrationProfile |
11 from registration.models import RegistrationProfile |
9 """Form used to edit the profile of a user""" |
14 """Form used to edit the profile of a user""" |
10 |
15 |
11 class Meta: |
16 class Meta: |
12 model = Profile |
17 model = Profile |
13 exclude = ('user','rights','dob','credits') |
18 exclude = ('user','rights','dob','credits') |
|
19 |
|
20 def clean_photo(self): |
|
21 uploaded_photo = self.data.get('photo', None) |
|
22 prev_photo = self.instance.photo |
|
23 if uploaded_photo: |
|
24 if uploaded_photo.size > 1048576: |
|
25 raise forms.ValidationError('Images only smaller than 1MB allowed') |
|
26 tmp_im_path = '/tmp/'+get_key() |
|
27 tmp_file = open(tmp_im_path, 'w') |
|
28 tmp_file.write(uploaded_photo.read()) |
|
29 tmp_file.close() |
|
30 try: |
|
31 PIL.Image.open(tmp_im_path) |
|
32 except IOError: |
|
33 raise forms.ValidationError('Image format unknown') |
|
34 os.remove(tmp_im_path) |
|
35 |
|
36 if prev_photo: os.remove(prev_photo.path) |
|
37 return uploaded_photo |
|
38 else: |
|
39 return prev_photo |
|
40 |
14 |
41 |
15 class RegistrationFormCustom(RegistrationFormUniqueEmail): |
42 class RegistrationFormCustom(RegistrationFormUniqueEmail): |
16 """Used instead of RegistrationForm used by default django-registration backend, this adds date of birth and gender to the default django-registration RegistrationForm""" |
43 """Used instead of RegistrationForm used by default django-registration backend, this adds date of birth and gender to the default django-registration RegistrationForm""" |
17 |
44 |
18 dob = forms.DateField(help_text = "YYYY-MM-DD", required=True, label=u'date of birth') |
45 dob = forms.DateField(help_text = "YYYY-MM-DD", required=True, label=u'date of birth') |