taskapp/forms/user.py
author Nishanth Amuluru <nishanth@fossee.in>
Wed, 05 Jan 2011 22:30:45 +0530
changeset 219 f04a1ec7a07f
parent 213 a01078a9bbcf
permissions -rw-r--r--
Replaced the word credit with pynt
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
213
a01078a9bbcf send/create notification when a new account is created.
anoop
parents: 206
diff changeset
    12
from pytask.taskapp.utilities.notification import create_notification
13
0fb64b24a1c9 added views, templates for register, login, logout user.
anoop
parents: 12
diff changeset
    13
141
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    14
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
    15
    """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
    16
    
23
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 13
diff changeset
    17
    class Meta:
f33084ea1361 added view profile and edit profile functionalities.
anoop
parents: 13
diff changeset
    18
        model = Profile
219
f04a1ec7a07f Replaced the word credit with pynt
Nishanth Amuluru <nishanth@fossee.in>
parents: 213
diff changeset
    19
        exclude = ('user','rights','dob','pynts')
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
    20
206
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    21
    def clean_photo(self):
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    22
        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
    23
        prev_photo = self.instance.photo
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    24
        if uploaded_photo:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    25
            if uploaded_photo.size > 1048576:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    26
                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
    27
            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
    28
            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
    29
            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
    30
            tmp_file.close()
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    31
            try:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    32
                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
    33
            except IOError:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    34
                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
    35
            os.remove(tmp_im_path)
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    36
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    37
            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
    38
            return uploaded_photo
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    39
        else:
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    40
            return prev_photo
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    41
85660d75683d now user profile editing is done thro forms in a more elegant way.
nishanth
parents: 141
diff changeset
    42
47
4e059a4220cc user should enter unique email address, changed login_redirect_url.
anoop
parents: 44
diff changeset
    43
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
    44
    """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
    45
    
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
    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
    47
    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
    48
    
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
    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
    50
        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
    51
        
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 = 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
    53
        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
    54
        
213
a01078a9bbcf send/create notification when a new account is created.
anoop
parents: 206
diff changeset
    55
        create_notification('NU',new_user)
a01078a9bbcf send/create notification when a new account is created.
anoop
parents: 206
diff changeset
    56
        
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
    57
        return new_user
141
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
def UserChoiceForm(choices, instance=None):
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    60
    """ 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
    61
    """
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    62
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    63
    class myForm(forms.Form):
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    64
        user = forms.ChoiceField(choices, required=True)
2489392ffb56 added the functionality to request a user to be AD MG DV.
nishanth
parents: 47
diff changeset
    65
    return myForm(instance) if instance else myForm()