Revision 5ed6816e astakos/im/forms.py
b/astakos/im/forms.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from django import forms |
35 | 35 |
from django.utils.translation import ugettext as _ |
36 |
from django.contrib.auth.forms import UserCreationForm |
|
36 |
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
|
|
37 | 37 |
from django.conf import settings |
38 |
from django.core.validators import email_re |
|
39 |
|
|
38 | 40 |
from hashlib import new as newhasher |
39 | 41 |
|
40 | 42 |
from astakos.im.models import AstakosUser |
41 | 43 |
from astakos.im.util import get_or_create_user |
42 | 44 |
|
43 | 45 |
import logging |
46 |
import uuid |
|
44 | 47 |
|
45 | 48 |
class UniqueUserEmailField(forms.EmailField): |
46 | 49 |
""" |
... | ... | |
61 | 64 |
Extends the built in UserCreationForm in several ways: |
62 | 65 |
|
63 | 66 |
* Adds an email field, which uses the custom UniqueUserEmailField. |
64 |
* The username field isn't visible and it is assigned the email value.
|
|
67 |
* The username field isn't visible and it is assigned a generated id.
|
|
65 | 68 |
* first_name and last_name fields are added. |
69 |
* User is created not active. |
|
66 | 70 |
""" |
67 | 71 |
|
68 |
username = forms.CharField(required = False, max_length = 30) |
|
69 |
email = UniqueUserEmailField(required = True, label = 'Email address') |
|
70 |
first_name = forms.CharField(required = False, max_length = 30) |
|
71 |
last_name = forms.CharField(required = False, max_length = 30) |
|
72 |
|
|
73 | 72 |
class Meta: |
74 | 73 |
model = AstakosUser |
75 |
fields = ("username",)
|
|
74 |
fields = ("email", "first_name", "last_name")
|
|
76 | 75 |
|
77 | 76 |
def __init__(self, *args, **kwargs): |
78 | 77 |
""" |
... | ... | |
82 | 81 |
self.fields.keyOrder = ['email', 'first_name', 'last_name', |
83 | 82 |
'password1', 'password2'] |
84 | 83 |
|
85 |
def clean(self, *args, **kwargs): |
|
86 |
""" |
|
87 |
Normal cleanup + username generation. |
|
88 |
""" |
|
89 |
cleaned_data = super(ExtendedUserCreationForm, self).clean(*args, **kwargs) |
|
90 |
if cleaned_data.has_key('email'): |
|
91 |
cleaned_data['username'] = cleaned_data['email'] |
|
92 |
return cleaned_data |
|
93 |
|
|
94 | 84 |
def save(self, commit=True): |
95 | 85 |
""" |
96 | 86 |
Saves the email, first_name and last_name properties, after the normal |
97 | 87 |
save behavior is complete. |
98 | 88 |
""" |
99 | 89 |
user = super(ExtendedUserCreationForm, self).save(commit=False) |
90 |
user.username = uuid.uuid4().hex[:30] |
|
91 |
user.is_active = False |
|
100 | 92 |
user.renew_token() |
101 | 93 |
user.save() |
102 | 94 |
logging.info('Created user %s', user) |
103 | 95 |
return user |
104 | 96 |
|
105 |
class InvitedExtendedUserCreationForm(UserCreationForm): |
|
97 |
class InvitedExtendedUserCreationForm(ExtendedUserCreationForm):
|
|
106 | 98 |
""" |
107 |
Extends the built in UserCreationForm in several ways: |
|
108 |
|
|
109 |
* Adds an email field, which uses the custom UniqueUserEmailField. |
|
110 |
* The username field isn't visible and it is assigned the email value. |
|
111 |
* first_name and last_name fields are added. |
|
99 |
Extends the ExtendedUserCreationForm: adds an inviter readonly field. |
|
112 | 100 |
""" |
113 | 101 |
|
114 |
username = forms.CharField(required = False, max_length = 30) |
|
115 |
email = UniqueUserEmailField(required = True, label = 'Email address') |
|
116 |
first_name = forms.CharField(required = False, max_length = 30) |
|
117 |
last_name = forms.CharField(required = False, max_length = 30) |
|
118 | 102 |
inviter = forms.CharField(widget=forms.TextInput(), label=_('Inviter Real Name')) |
103 |
level = forms.IntegerField(widget=forms.TextInput(), label=_('Level')) |
|
119 | 104 |
|
120 | 105 |
class Meta: |
121 | 106 |
model = AstakosUser |
122 |
fields = ("username",)
|
|
107 |
fields = ("email", "first_name", "last_name")
|
|
123 | 108 |
|
124 | 109 |
def __init__(self, *args, **kwargs): |
125 | 110 |
""" |
126 | 111 |
Changes the order of fields, and removes the username field. |
127 | 112 |
""" |
128 | 113 |
super(InvitedExtendedUserCreationForm, self).__init__(*args, **kwargs) |
129 |
self.fields.keyOrder = ['email', 'inviter', 'first_name', 'last_name',
|
|
130 |
'password1', 'password2'] |
|
114 |
self.fields.keyOrder = ['email', 'inviter', 'level', 'first_name',
|
|
115 |
'last_name', 'password1', 'password2']
|
|
131 | 116 |
#set readonly form fields |
132 | 117 |
self.fields['inviter'].widget.attrs['readonly'] = True |
118 |
self.fields['level'].widget.attrs['readonly'] = True |
|
133 | 119 |
self.fields['email'].widget.attrs['readonly'] = True |
134 | 120 |
self.fields['username'].widget.attrs['readonly'] = True |
135 |
|
|
136 |
def clean(self, *args, **kwargs): |
|
137 |
""" |
|
138 |
Normal cleanup + username generation. |
|
139 |
""" |
|
140 |
cleaned_data = super(UserCreationForm, self).clean(*args, **kwargs) |
|
141 |
if cleaned_data.has_key('email'): |
|
142 |
cleaned_data['username'] = cleaned_data['email'] |
|
143 |
return cleaned_data |
|
144 |
|
|
145 |
def save(self, commit=True): |
|
146 |
""" |
|
147 |
Saves the email, first_name and last_name properties, after the normal |
|
148 |
save behavior is complete. |
|
149 |
""" |
|
150 |
user = super(InvitedExtendedUserCreationForm, self).save(commit=False) |
|
151 |
user.renew_token() |
|
152 |
user.save() |
|
153 |
logging.info('Created user %s', user) |
|
154 |
return user |
|
121 |
|
|
122 |
class LoginForm(AuthenticationForm): |
|
123 |
username = forms.EmailField(label=_("Email")) |
|
155 | 124 |
|
156 | 125 |
class ProfileForm(forms.ModelForm): |
157 | 126 |
""" |
... | ... | |
163 | 132 |
""" |
164 | 133 |
class Meta: |
165 | 134 |
model = AstakosUser |
166 |
exclude = ('groups', 'user_permissions') |
|
135 |
exclude = ('is_active', 'is_superuser', 'is_staff', 'is_verified', 'groups', 'user_permissions')
|
|
167 | 136 |
|
168 | 137 |
def __init__(self, *args, **kwargs): |
169 | 138 |
super(ProfileForm, self).__init__(*args, **kwargs) |
170 | 139 |
instance = getattr(self, 'instance', None) |
171 | 140 |
ro_fields = ('username','date_joined', 'updated', 'auth_token', |
172 | 141 |
'auth_token_created', 'auth_token_expires', 'invitations', |
173 |
'level', 'last_login', 'email', 'is_active', 'is_superuser', |
|
174 |
'is_staff') |
|
142 |
'level', 'last_login', 'email', ) |
|
175 | 143 |
if instance and instance.id: |
176 | 144 |
for field in ro_fields: |
177 | 145 |
if isinstance(self.fields[field].widget, forms.CheckboxInput): |
... | ... | |
194 | 162 |
label=u'Message', required=False) |
195 | 163 |
feedback_data = forms.CharField(widget=forms.Textarea(), |
196 | 164 |
label=u'Data', required=False) |
165 |
|
|
166 |
class SendInvitationForm(forms.Form): |
|
167 |
""" |
|
168 |
Form for sending an invitations |
|
169 |
""" |
|
170 |
|
|
171 |
email = forms.EmailField(required = True, label = 'Email address') |
|
172 |
first_name = forms.EmailField(label = 'First name') |
|
173 |
last_name = forms.EmailField(label = 'Last name') |
|
197 | 174 |
|
Also available in: Unified diff