taskapp/forms/user.py
changeset 236 39f83b4cf557
parent 235 e338eaeccad7
equal deleted inserted replaced
235:e338eaeccad7 236:39f83b4cf557
     1 #!/usr/bin/python2.5
       
     2 
       
     3 import os
       
     4 import PIL
       
     5 
       
     6 from pytask.taskapp.utilities.helper import get_key
       
     7 
       
     8 from django import forms
       
     9 from pytask.taskapp.models import GENDER_CHOICES, Profile
       
    10 from registration.forms import RegistrationFormUniqueEmail
       
    11 from registration.models import RegistrationProfile
       
    12 from pytask.taskapp.utilities.notification import create_notification
       
    13 
       
    14 class UserProfileEditForm(forms.ModelForm):
       
    15     """Form used to edit the profile of a user"""
       
    16     
       
    17     class Meta:
       
    18         model = Profile
       
    19         exclude = ('user','rights','dob','pynts')
       
    20 
       
    21     def clean_photo(self):
       
    22         uploaded_photo = self.data.get('photo', None)
       
    23         prev_photo = self.instance.photo
       
    24         if uploaded_photo:
       
    25             if uploaded_photo.size > 1048576:
       
    26                 raise forms.ValidationError('Images only smaller than 1MB allowed')
       
    27             tmp_im_path = '/tmp/'+get_key()
       
    28             tmp_file = open(tmp_im_path, 'w')
       
    29             tmp_file.write(uploaded_photo.read())
       
    30             tmp_file.close()
       
    31             try:
       
    32                 PIL.Image.open(tmp_im_path)
       
    33             except IOError:
       
    34                 raise forms.ValidationError('Image format unknown')
       
    35             os.remove(tmp_im_path)
       
    36 
       
    37             if prev_photo: os.remove(prev_photo.path)
       
    38             return uploaded_photo
       
    39         else:
       
    40             return prev_photo
       
    41 
       
    42 
       
    43 class RegistrationFormCustom(RegistrationFormUniqueEmail):
       
    44     """Used instead of RegistrationForm used by default django-registration backend, this adds date of birth and gender to the default django-registration RegistrationForm"""
       
    45     
       
    46     dob = forms.DateField(help_text = "YYYY-MM-DD", required=True, label=u'date of birth')
       
    47     gender = forms.ChoiceField(choices = GENDER_CHOICES, required=True, label=u'gender')
       
    48     
       
    49     def save(self,profile_callback=None):
       
    50         new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],password=self.cleaned_data['password1'],email=self.cleaned_data['email'])
       
    51         
       
    52         new_profile = Profile(user=new_user,dob=self.cleaned_data['dob'],gender=self.cleaned_data['gender'])
       
    53         new_profile.save()
       
    54         
       
    55         create_notification('NU',new_user)
       
    56         
       
    57         return new_user
       
    58 
       
    59 def UserChoiceForm(choices, instance=None):
       
    60     """ take a list of users and return a choice form.
       
    61     """
       
    62 
       
    63     class myForm(forms.Form):
       
    64         user = forms.ChoiceField(choices, required=True)
       
    65     return myForm(instance) if instance else myForm()