taskapp/forms/user.py
author anoop
Mon, 08 Mar 2010 13:37:26 +0530
changeset 207 2de52334fe6c
parent 206 85660d75683d
child 213 a01078a9bbcf
permissions -rw-r--r--
changed the MEDIA_URL in settings.py.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
     1
#!/usr/bin/python2.5
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
     2
206
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
     3
import os
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
     4
import PIL
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
     5
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
     6
from pytask.taskapp.utilities.helper import get_key
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
     7
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
     8
from django import forms
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 13
diff changeset
     9
from pytask.taskapp.models import GENDER_CHOICES, Profile
47
4e059a4220cc user should enter unique email address, changed login_redirect_url.
anoop
parents: 44
diff changeset
    10
from registration.forms import RegistrationFormUniqueEmail
44
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    11
from registration.models import RegistrationProfile
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    12
141
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    13
class UserProfileEditForm(forms.ModelForm):
44
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    14
    """Form used to edit the profile of a user"""
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    15
    
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 13
diff changeset
    16
    class Meta:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 13
diff changeset
    17
        model = Profile
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 13
diff changeset
    18
        exclude = ('user','rights','dob','credits')
44
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    19
206
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    20
    def clean_photo(self):
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    21
        uploaded_photo = self.data.get('photo', None)
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    22
        prev_photo = self.instance.photo
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    23
        if uploaded_photo:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    24
            if uploaded_photo.size > 1048576:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    25
                raise forms.ValidationError('Images only smaller than 1MB allowed')
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    26
            tmp_im_path = '/tmp/'+get_key()
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    27
            tmp_file = open(tmp_im_path, 'w')
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    28
            tmp_file.write(uploaded_photo.read())
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    29
            tmp_file.close()
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    30
            try:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    31
                PIL.Image.open(tmp_im_path)
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    32
            except IOError:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    33
                raise forms.ValidationError('Image format unknown')
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    34
            os.remove(tmp_im_path)
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    35
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    36
            if prev_photo: os.remove(prev_photo.path)
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    37
            return uploaded_photo
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    38
        else:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    39
            return prev_photo
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    40
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    41
47
4e059a4220cc user should enter unique email address, changed login_redirect_url.
anoop
parents: 44
diff changeset
    42
class RegistrationFormCustom(RegistrationFormUniqueEmail):
44
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    43
    """Used instead of RegistrationForm used by default django-registration backend, this adds date of birth and gender to the default django-registration RegistrationForm"""
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    44
    
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    45
    dob = forms.DateField(help_text = "YYYY-MM-DD", required=True, label=u'date of birth')
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    46
    gender = forms.ChoiceField(choices = GENDER_CHOICES, required=True, label=u'gender')
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    47
    
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    48
    def save(self,profile_callback=None):
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    49
        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],password=self.cleaned_data['password1'],email=self.cleaned_data['email'])
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    50
        
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    51
        new_profile = Profile(user=new_user,dob=self.cleaned_data['dob'],gender=self.cleaned_data['gender'])
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    52
        new_profile.save()
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    53
        
2b09336352b5 fixed creation of user profile at registration time, added custom registration form so as to include dob and gender.
anoop
parents: 40
diff changeset
    54
        return new_user
141
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    55
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    56
def UserChoiceForm(choices, instance=None):
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    57
    """ take a list of users and return a choice form.
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    58
    """
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    59
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    60
    class myForm(forms.Form):
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    61
        user = forms.ChoiceField(choices, required=True)
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    62
    return myForm(instance) if instance else myForm()