21 '"Todd Larsen" <tlarsen@google.com>', |
21 '"Todd Larsen" <tlarsen@google.com>', |
22 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
22 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
23 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
23 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
24 ] |
24 ] |
25 |
25 |
|
26 |
|
27 from google.appengine.api import users |
26 |
28 |
27 from django import forms |
29 from django import forms |
28 |
30 |
29 from soc.logic import validate |
31 from soc.logic import validate |
30 from soc.logic.models import user as user_logic |
32 from soc.logic.models import user as user_logic |
55 raise forms.ValidationError("This user does not exist.") |
57 raise forms.ValidationError("This user does not exist.") |
56 |
58 |
57 return user_entity |
59 return user_entity |
58 return wrapped |
60 return wrapped |
59 |
61 |
|
62 |
|
63 def clean_user_not_exist(field_name): |
|
64 """Check if the field_name value is a valid link_id and a user with the |
|
65 link id does not exist. |
|
66 """ |
|
67 |
|
68 def wrapped(self): |
|
69 link_id = self.cleaned_data.get(field_name).lower() |
|
70 |
|
71 if not validate.isLinkIdFormatValid(link_id): |
|
72 raise forms.ValidationError("This link ID is in wrong format.") |
|
73 |
|
74 user_entity = user_logic.logic.getForFields({'link_id' : link_id}, unique=True) |
|
75 |
|
76 if user_entity: |
|
77 # user exists already |
|
78 raise forms.ValidationError("There is already a user with this link id.") |
|
79 |
|
80 return link_id |
|
81 return wrapped |
|
82 |
60 def clean_users_not_same(field_name): |
83 def clean_users_not_same(field_name): |
61 """Check if the field_name field is a valid user and is not |
84 """Check if the field_name field is a valid user and is not |
62 equal to the current user. |
85 equal to the current user. |
63 """ |
86 """ |
64 |
87 |
75 |
98 |
76 return user_entity |
99 return user_entity |
77 return wrapped |
100 return wrapped |
78 |
101 |
79 |
102 |
|
103 def clean_user_account(field_name): |
|
104 """Returns the User with the given field_name value. |
|
105 """ |
|
106 def wrapped(self): |
|
107 email_adress = self.cleaned_data.get(field_name).lower() |
|
108 |
|
109 # get the user account for this email |
|
110 user_account = users.User(email_adress) |
|
111 |
|
112 return user_account |
|
113 return wrapped |
|
114 |
|
115 |
|
116 def clean_user_account_not_in_use(field_name): |
|
117 """Check if the field_name value contains an email |
|
118 address that hasn't been used for an existing account. |
|
119 """ |
|
120 |
|
121 def wrapped(self): |
|
122 email_adress = self.cleaned_data.get(field_name).lower() |
|
123 |
|
124 # get the user account for this email and check if it's in use |
|
125 user_account = users.User(email_adress) |
|
126 |
|
127 fields = {'account' : user_account} |
|
128 user_entity = user_logic.logic.getForFields(fields, unique=True) |
|
129 |
|
130 if user_entity or user_logic.logic.isFormerAccount(user_account): |
|
131 raise forms.ValidationError("There is already a user with this email adress.") |
|
132 |
|
133 return user_account |
|
134 return wrapped |
|
135 |
|
136 |
80 def clean_feed_url(self): |
137 def clean_feed_url(self): |
81 feed_url = self.cleaned_data.get('feed_url') |
138 feed_url = self.cleaned_data.get('feed_url') |
82 |
139 |
83 if feed_url == '': |
140 if feed_url == '': |
84 # feed url not supplied (which is OK), so do not try to validate it |
141 # feed url not supplied (which is OK), so do not try to validate it |
103 |
160 |
104 # call the Django URLField cleaning method to properly clean/validate this field |
161 # call the Django URLField cleaning method to properly clean/validate this field |
105 return forms.URLField.clean(self.fields[field_name], value) |
162 return forms.URLField.clean(self.fields[field_name], value) |
106 return wrapped |
163 return wrapped |
107 |
164 |
|
165 |
|
166 def validate_user_edit(link_id_field, account_field): |
|
167 """Clean method for cleaning user edit form. |
|
168 |
|
169 Raises ValidationError if: |
|
170 -Another User has the given email address as account |
|
171 -Another User has the given email address in it's FormerAccounts list |
|
172 """ |
|
173 def wrapper(self): |
|
174 cleaned_data = self.cleaned_data |
|
175 |
|
176 link_id = cleaned_data.get(link_id_field) |
|
177 user_account = cleaned_data.get(account_field) |
|
178 |
|
179 # if both fields were valid do this check |
|
180 if link_id and user_account: |
|
181 # get the user from the link_id in the form |
|
182 fields = {'link_id': link_id} |
|
183 user_entity = user_logic.logic.getForFields(fields, unique=True) |
|
184 |
|
185 former_accounts = user_entity.former_accounts |
|
186 |
|
187 # if it's not the user's current account or one of his former accounts |
|
188 if (user_entity.account != user_account and |
|
189 user_account not in former_accounts): |
|
190 |
|
191 # get the user having the given account |
|
192 fields = {'account': user_account} |
|
193 user_from_account_entity = user_logic.logic.getForFields(fields, unique=True) |
|
194 |
|
195 # if there is a user with the given account or it's a former account |
|
196 if user_from_account_entity or user_logic.logic.isFormerAccount(user_account): |
|
197 # raise an error because this email address can't be used |
|
198 raise forms.ValidationError("There is already a user with this email adress.") |
|
199 |
|
200 return cleaned_data |
|
201 return wrapper |
|
202 |