0
|
1 |
from django import forms |
|
2 |
from django.contrib.auth.models import User, Permission, Group |
|
3 |
from django.forms.util import ErrorList |
|
4 |
from django.contrib.auth.forms import UserCreationForm |
|
5 |
from django.utils.translation import gettext as _ |
|
6 |
from models import ldt, IriGroup |
|
7 |
from ldt.management import get_content_type_list |
|
8 |
|
|
9 |
|
|
10 |
class ldtForm(UserCreationForm): |
|
11 |
|
|
12 |
class Meta: |
|
13 |
model = ldt |
|
14 |
|
|
15 |
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, |
|
16 |
initial=None, error_class=ErrorList, label_suffix=':', |
|
17 |
empty_permitted=False, instance=None): |
|
18 |
|
|
19 |
if instance: |
|
20 |
initial = initial or {} |
|
21 |
|
|
22 |
super(ldtForm, self).__init__(data, files, auto_id, prefix, |
|
23 |
initial, error_class, label_suffix, empty_permitted, instance) |
|
24 |
|
|
25 |
# filtre les permissions necessaires |
|
26 |
content_type_list = get_content_type_list() |
|
27 |
self.fields['user_permissions'].queryset = Permission.objects.filter(content_type__in = content_type_list) |
|
28 |
|
|
29 |
if instance: |
|
30 |
self.fields['password1'].required = False |
|
31 |
self.fields['password1'].label = _('New password') |
|
32 |
self.fields['password2'].required = False |
|
33 |
self.fields['password2'].label = _('New password confirmation') |
|
34 |
|
|
35 |
self._password_change = True |
|
36 |
|
|
37 |
def clean_username(self): |
|
38 |
if self.instance: |
|
39 |
return self.cleaned_data['username'] |
|
40 |
return super(ldtForm, self).clean_username() |
|
41 |
|
|
42 |
def clean_password2(self): |
|
43 |
if self.instance and self.cleaned_data['password1'] == '' and self.cleaned_data['password2'] == '': |
|
44 |
self._password_change = False |
|
45 |
return u'' |
|
46 |
return super(ldtForm, self).clean_password2() |
|
47 |
|
|
48 |
|
|
49 |
def save(self, commit=True): |
|
50 |
Super = self._password_change and ldtForm or UserCreationForm |
|
51 |
user = super(Super, self).save(commit=False) |
|
52 |
# if user.pk != None: |
|
53 |
# self.save_m2m() |
|
54 |
|
|
55 |
if commit: |
|
56 |
user.save() |
|
57 |
|
|
58 |
return user |
|
59 |
|
|
60 |
class IriGroupForm(forms.ModelForm): |
|
61 |
class meta: |
|
62 |
model = IriGroup |
|
63 |
|
|
64 |
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, |
|
65 |
initial=None, error_class=ErrorList, label_suffix=':', |
|
66 |
empty_permitted=False, instance=None): |
|
67 |
if instance: |
|
68 |
initial = initial or {} |
|
69 |
|
|
70 |
super(IriGroupForm, self).__init__(data, files, auto_id, prefix, |
|
71 |
initial, error_class, label_suffix, empty_permitted, instance) |
|
72 |
|
|
73 |
# filtre les permissions necessaires |
|
74 |
content_type_list = get_content_type_list() |
|
75 |
self.fields['permissions'].queryset = Permission.objects.filter(content_type__in = content_type_list) |
|
76 |
|
|
77 |
class EmailChangeForm(forms.Form): |
|
78 |
email1 = forms.EmailField(label=_("E-mail"), max_length=75) |
|
79 |
email2 = forms.EmailField(label=_("E-mail"), max_length=75) |
|
80 |
|
|
81 |
def __init__(self, user=None, *args, **kwargs): |
|
82 |
self.user = user |
|
83 |
super(EmailChangeForm, self).__init__(*args, **kwargs) |
|
84 |
|
|
85 |
def clean_email2(self): |
|
86 |
email1 = self.cleaned_data.get('email1') |
|
87 |
email2 = self.cleaned_data.get('email2') |
|
88 |
if email1 and email2: |
|
89 |
if email1 != email2: |
|
90 |
raise forms.ValidationError(_("The two emails didn't match.")) |
|
91 |
return email2 |
|
92 |
|
|
93 |
|
|
94 |
def save(self): |
|
95 |
self.user.email=self.cleaned_data['email1'] |
|
96 |
self.user.save() |
|
97 |
return self.user |
|
98 |
|