src/ldt/ldt/user/forms.py
author ymh <ymh.work@gmail.com>
Fri, 24 May 2013 17:30:29 +0200
changeset 1195 81e5be7f10f9
parent 1193 cd67b17d257d
child 1219 dd46f7a3b064
permissions -rw-r--r--
make user profile changes work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     1
from django import forms
169
64f24f8841ec Add profile to user
verrierj
parents: 165
diff changeset
     2
from django.conf import settings
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 949
diff changeset
     3
from django.contrib.auth import get_user_model
1193
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
     4
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
     5
from django.contrib.auth.models import Group
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
     6
from django.forms.util import ErrorList
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     7
from django.utils.translation import gettext as _
130
1cc949de2d1f Useless code removed. Iri group finally removed from the model. User's templates cleaned. Little css changed for home.
cavaliet
parents: 128
diff changeset
     8
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 949
diff changeset
     9
User = get_user_model()
1193
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    10
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    11
class LdtForm(UserCreationForm):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    12
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    13
    class Meta:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    14
        model = User
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    15
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    16
    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    17
                 initial=None, error_class=ErrorList, label_suffix=':',
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    18
                 empty_permitted=False, instance=None):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    19
                 
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    20
        if instance:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    21
            initial = initial or {}           
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    22
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    23
        super(LdtForm, self).__init__(data, files, auto_id, prefix,
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    24
            initial, error_class, label_suffix, empty_permitted, instance)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    25
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    26
        if instance:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    27
            self.fields['password1'].required = False
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    28
            self.fields['password1'].label = _('New password')
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    29
            self.fields['password2'].required = False
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    30
            self.fields['password2'].label = _('New password confirmation')
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    31
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    32
        self._password_change = True
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    33
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    34
    def clean_username(self):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    35
        if self.instance:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    36
            return self.cleaned_data['username']
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    37
        return super(LdtForm, self).clean_username()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    38
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    39
    def clean_password2(self): 
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    40
        if self.instance and self.cleaned_data['password1'] == '' and self.cleaned_data['password2'] == '':
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    41
            self._password_change = False
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    42
            return u''
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    43
        return super(LdtForm, self).clean_password2()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    44
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    45
   
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    46
    def save(self, commit=True):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    47
        Super = self._password_change and LdtForm  or UserCreationForm  
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    48
        user = super(Super, self).save(commit=False)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    49
        # if user.pk != None:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    50
            # self.save_m2m()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    51
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    52
        if commit:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    53
            user.save()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    54
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    55
        return user
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    56
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    57
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    58
class EmailChangeForm(forms.Form):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    59
    email1 = forms.EmailField(label=_("E-mail"), max_length=75)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    60
    email2 = forms.EmailField(label=_("E-mail"), max_length=75)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    61
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    62
    def __init__(self, user=None, *args, **kwargs):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    63
        self.user = user
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    64
        super(EmailChangeForm, self).__init__(*args, **kwargs)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    65
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    66
    def clean_email2(self):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    67
        email1 = self.cleaned_data.get('email1')
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    68
        email2 = self.cleaned_data.get('email2')
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    69
        if email1 and email2:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    70
            if email1 != email2:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    71
                raise forms.ValidationError(_("The two emails didn't match."))
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    72
        return email2
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    73
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    74
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    75
    def save(self):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    76
        self.user.email = self.cleaned_data['email1']
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    77
        self.user.save()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    78
        return self.user
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    79
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    80
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    81
class NameChangeForm(forms.Form):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    82
    first_name = forms.CharField(label=_("First name"), max_length=30)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    83
    last_name = forms.CharField(label=_("Last name"), max_length=30)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    84
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    85
    def __init__(self, user=None, *args, **kwargs):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    86
        self.user = user
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    87
        super(NameChangeForm, self).__init__(*args, **kwargs)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    88
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    89
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    90
    def save(self):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    91
        self.user.first_name = self.cleaned_data['first_name']
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    92
        self.user.last_name = self.cleaned_data['last_name']
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    93
        self.user.save()
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    94
        return self.user
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    95
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    96
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    97
class ProfileForm(UserChangeForm):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    98
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
    99
    def __init__(self, user=None, *args, **kwargs):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   100
        #self.user = user
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   101
        super(ProfileForm, self).__init__(*args, **kwargs)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   102
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   103
 
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   104
    class Meta:
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   105
        model = User
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   106
        fields = ('username', 'email', 'first_name', 'last_name', 'password')
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   107
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   108
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   109
class LanguageChangeForm(forms.Form):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   110
    language = forms.ChoiceField(label=_("Language"), choices=settings.LANGUAGES)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   111
    
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   112
    def __init__(self, user=None, *args, **kwargs):
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   113
        self.user = user
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   114
        super(LanguageChangeForm, self).__init__(*args, **kwargs)
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   115
        
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   116
    def save(self):
1195
81e5be7f10f9 make user profile changes work
ymh <ymh.work@gmail.com>
parents: 1193
diff changeset
   117
        self.user.language = self.cleaned_data['language']
81e5be7f10f9 make user profile changes work
ymh <ymh.work@gmail.com>
parents: 1193
diff changeset
   118
        self.user.save()
1193
cd67b17d257d - correct url in template
ymh <ymh.work@gmail.com>
parents: 1191
diff changeset
   119
        return self.user
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   120
           
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   121
class PictureForm(forms.Form):
792
e4180c2d11eb Modifications in order to download the thumbnail picture and put it as default picture when creating youtube, dailymotion or vimeo contents
grandjoncl
parents: 511
diff changeset
   122
    image=forms.ImageField(label=_("Profile picture"), required=False)
e4180c2d11eb Modifications in order to download the thumbnail picture and put it as default picture when creating youtube, dailymotion or vimeo contents
grandjoncl
parents: 511
diff changeset
   123
    url_image=forms.CharField(max_length=1024, required=False)
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 174
diff changeset
   124
    
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   125
    def __init__(self, model=None, *args, **kwargs):
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   126
        self.model = model
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   127
        super(PictureForm, self).__init__(*args, **kwargs)
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 174
diff changeset
   128
        
333
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   129
    def clean_image(self):
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   130
        image = self.cleaned_data['image']
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   131
                
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   132
        if image and image.size > settings.PROFILE_IMG_MAX_SIZE:
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   133
            raise forms.ValidationError(_("Image size is limited to %s" % settings.PROFILE_IMG_MAX_SIZE))
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   134
    
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   135
        return image
4ddf8c0eeab4 Image size is checked before save + project image can be set manually
verrierj
parents: 327
diff changeset
   136
    
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 174
diff changeset
   137
    def save(self):
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   138
        
327
e801e50edcce A description can be added to a group
verrierj
parents: 325
diff changeset
   139
        if not self.cleaned_data['image']:
e801e50edcce A description can be added to a group
verrierj
parents: 325
diff changeset
   140
            return None
e801e50edcce A description can be added to a group
verrierj
parents: 325
diff changeset
   141
        
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   142
        class_name = self.model.__class__.__name__
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 949
diff changeset
   143
        if isinstance(self.model, Group):
511
2edec83f1bd8 Fix bugs in forms
verrierj
parents: 342
diff changeset
   144
            instance_name = "%s" % self.model.id
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   145
            img_container = self.model.get_profile()
1191
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 949
diff changeset
   146
        if isinstance(self.model, User):
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 949
diff changeset
   147
            instance_name = "%s" % self.model.id
b6e0b1811723 Migrate to django 1.5 :
ymh <ymh.work@gmail.com>
parents: 949
diff changeset
   148
            img_container = self.model
342
17d615b49a91 Extend image fields size to 200 characters + minor bugs
verrierj
parents: 333
diff changeset
   149
        elif class_name == 'Content':
17d615b49a91 Extend image fields size to 200 characters + minor bugs
verrierj
parents: 333
diff changeset
   150
            instance_name = self.model.iri_id
17d615b49a91 Extend image fields size to 200 characters + minor bugs
verrierj
parents: 333
diff changeset
   151
            img_container = self.model
17d615b49a91 Extend image fields size to 200 characters + minor bugs
verrierj
parents: 333
diff changeset
   152
        elif class_name == 'Project':
17d615b49a91 Extend image fields size to 200 characters + minor bugs
verrierj
parents: 333
diff changeset
   153
            instance_name = self.model.ldt_id
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   154
            img_container = self.model
327
e801e50edcce A description can be added to a group
verrierj
parents: 325
diff changeset
   155
            
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 174
diff changeset
   156
        # We change the file name and keep the extension.
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 174
diff changeset
   157
        filename = self.cleaned_data['image'].name
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 174
diff changeset
   158
        ext = filename.split(".")[-1]
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   159
        self.cleaned_data['image'].name = instance_name + "." + ext
314
1a8620e5ebb0 Add memcached and sorl thumbnail pour thumbnail management. Set default pict on content, project and user.
cavaliet
parents: 174
diff changeset
   160
        # We save the picture with the correct name
325
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   161
        img_container.image = self.cleaned_data['image']
f4cb93c06b42 Images can be added to a group or a content.
verrierj
parents: 314
diff changeset
   162
        img_container.save()
327
e801e50edcce A description can be added to a group
verrierj
parents: 325
diff changeset
   163
        return self.model   
169
64f24f8841ec Add profile to user
verrierj
parents: 165
diff changeset
   164