Revision 794852f2 astakos/im/forms.py
b/astakos/im/forms.py | ||
---|---|---|
58 | 58 |
""" |
59 | 59 |
Extends the built in UserCreationForm in several ways: |
60 | 60 |
|
61 |
* Adds an email field, which uses the custom UniqueUserEmailField, |
|
62 |
that is, the form does not validate if the email address already exists |
|
63 |
in the User table. |
|
64 |
* The username field is generated based on the email, and isn't visible. |
|
61 |
* Adds an email field, which uses the custom UniqueUserEmailField. |
|
62 |
* The username field isn't visible and it is assigned the email value. |
|
65 | 63 |
* first_name and last_name fields are added. |
66 |
* Data not saved by the default behavior of UserCreationForm is saved. |
|
67 | 64 |
""" |
68 | 65 |
|
69 | 66 |
username = forms.CharField(required = False, max_length = 30) |
... | ... | |
71 | 68 |
first_name = forms.CharField(required = False, max_length = 30) |
72 | 69 |
last_name = forms.CharField(required = False, max_length = 30) |
73 | 70 |
|
71 |
class Meta: |
|
72 |
model = AstakosUser |
|
73 |
fields = ("username",) |
|
74 |
|
|
74 | 75 |
def __init__(self, *args, **kwargs): |
75 | 76 |
""" |
76 | 77 |
Changes the order of fields, and removes the username field. |
77 | 78 |
""" |
78 |
super(UserCreationForm, self).__init__(*args, **kwargs) |
|
79 |
super(ExtendedUserCreationForm, self).__init__(*args, **kwargs)
|
|
79 | 80 |
self.fields.keyOrder = ['email', 'first_name', 'last_name', |
80 | 81 |
'password1', 'password2'] |
81 | 82 |
|
... | ... | |
85 | 86 |
""" |
86 | 87 |
cleaned_data = super(UserCreationForm, self).clean(*args, **kwargs) |
87 | 88 |
if cleaned_data.has_key('email'): |
88 |
#cleaned_data['username'] = self.__generate_username( |
|
89 |
# cleaned_data['email']) |
|
90 | 89 |
cleaned_data['username'] = cleaned_data['email'] |
91 | 90 |
return cleaned_data |
92 | 91 |
|
... | ... | |
96 | 95 |
save behavior is complete. |
97 | 96 |
""" |
98 | 97 |
user = super(UserCreationForm, self).save(commit) |
99 |
if user: |
|
100 |
kwargs = {} |
|
101 |
for field in self.fields: |
|
102 |
if hasattr(AstakosUser(), field): |
|
103 |
kwargs[field] = self.cleaned_data[field] |
|
104 |
user = get_or_create_user(username=self.cleaned_data['email'], **kwargs) |
|
105 | 98 |
return user |
106 | 99 |
|
107 | 100 |
class InvitedExtendedUserCreationForm(ExtendedUserCreationForm): |
... | ... | |
112 | 105 |
""" |
113 | 106 |
inviter = forms.CharField(widget=forms.TextInput(), |
114 | 107 |
label=_('Inviter Real Name')) |
108 |
class Meta: |
|
109 |
model = AstakosUser |
|
110 |
fields = ("username",) |
|
115 | 111 |
|
116 | 112 |
def __init__(self, *args, **kwargs): |
117 |
super(RegisterForm, self).__init__(*args, **kwargs)
|
|
113 |
super(InvitedExtendedUserCreationForm, self).__init__(*args, **kwargs)
|
|
118 | 114 |
|
119 | 115 |
#set readonly form fields |
120 | 116 |
self.fields['inviter'].widget.attrs['readonly'] = True |
117 |
self.fields['email'].widget.attrs['readonly'] = True |
|
118 |
self.fields['username'].widget.attrs['readonly'] = True |
|
121 | 119 |
|
122 | 120 |
class ProfileForm(forms.ModelForm): |
123 | 121 |
""" |
Also available in: Unified diff