104 """ |
105 """ |
105 |
106 |
106 key_name = forms.CharField(widget=forms.HiddenInput) |
107 key_name = forms.CharField(widget=forms.HiddenInput) |
107 |
108 |
108 |
109 |
|
110 class UserForm(helper.forms.BaseForm): |
|
111 """Django form displayed when creating or editing a User. |
|
112 """ |
|
113 class Meta: |
|
114 """Inner Meta class that defines some behavior for the form. |
|
115 """ |
|
116 #: db.Model subclass for which the form will gather information |
|
117 model = soc.models.user.User |
|
118 |
|
119 #: list of model fields which will *not* be gathered by the form |
|
120 exclude = ['account', 'former_accounts', 'is_developer'] |
|
121 |
|
122 def clean_link_id(self): |
|
123 link_id = self.cleaned_data.get('link_id') |
|
124 if not validate.isLinkIdFormatValid(link_id): |
|
125 raise forms.ValidationError("This link ID is in wrong format.") |
|
126 |
|
127 user = soc.logic.models.user.logic.getForFields({'link_id': link_id}, |
|
128 unique=True) |
|
129 |
|
130 # Get the currently logged in user account |
|
131 current_account = users.get_current_user() |
|
132 |
|
133 if user: |
|
134 if current_account != user.account: |
|
135 raise forms.ValidationError("This link ID is already in use.") |
|
136 |
|
137 return link_id |
|
138 |
|
139 |
109 class View(base.View): |
140 class View(base.View): |
110 """View methods for the User model. |
141 """View methods for the User model. |
111 """ |
142 """ |
112 |
143 |
113 def __init__(self, original_params=None): |
144 def __init__(self, original_params=None): |
157 page_name: the page name displayed in templates as page and header title |
188 page_name: the page name displayed in templates as page and header title |
158 params: a dict with params for this View |
189 params: a dict with params for this View |
159 kwargs: The Key Fields for the specified entity |
190 kwargs: The Key Fields for the specified entity |
160 """ |
191 """ |
161 |
192 |
162 params = dicts.merge(params, {'edit_template': 'soc/user/edit_self.html'}) |
193 rights = {} |
163 |
194 rights['any_access'] = [access.checkIsLoggedIn] |
164 properties = {'account': users.get_current_user()} |
195 rights['unspecified'] = [access.deny] |
165 |
196 rights['editSelf'] = [access.allow] |
166 entity = self._logic.getForFields(properties, unique=True) |
197 |
167 keys = self._logic.getKeyFieldNames() |
198 try: |
168 values = self._logic.getKeyValues(entity) |
199 self.checkAccess('editSelf', request, rights=rights) |
169 key_fields = dicts.zip(keys, values) |
200 except soc.views.out_of_band.AccessViolationResponse, alt_response: |
170 |
201 return alt_response.response() |
171 return self.edit(request, page_name, params=params, **key_fields) |
202 |
|
203 new_params = {} |
|
204 new_params['edit_template'] = 'soc/user/edit_self.html' |
|
205 new_params['rights'] = rights |
|
206 |
|
207 params = dicts.merge(params, new_params) |
|
208 params = dicts.merge(params, self._params) |
|
209 |
|
210 account = users.get_current_user() |
|
211 properties = {'account': account} |
|
212 |
|
213 user = soc.logic.models.user.logic.getForFields(properties, unique=True) |
|
214 |
|
215 # create default template context for use with any templates |
|
216 context = helper.responses.getUniversalContext(request) |
|
217 |
|
218 if request.method == 'POST': |
|
219 form = UserForm(request.POST) |
|
220 |
|
221 if form.is_valid(): |
|
222 new_link_id = form.cleaned_data.get('link_id') |
|
223 properties = { |
|
224 'link_id': new_link_id, |
|
225 'nick_name': form.cleaned_data.get("nick_name"), |
|
226 'account': account, |
|
227 } |
|
228 |
|
229 # check if user account is not in former_accounts |
|
230 # if it is show error message that account is invalid |
|
231 if soc.logic.models.user.logic.isFormerAccount(account): |
|
232 msg = DEF_USER_ACCOUNT_INVALID_MSG |
|
233 error = out_of_band.ErrorResponse(msg) |
|
234 return simple.errorResponse(request, page_name, error, template, context) |
|
235 |
|
236 user = soc.logic.models.user.logic.updateOrCreateFromFields(properties, {'link_id': new_link_id}) |
|
237 |
|
238 # redirect to /user/profile?s=0 |
|
239 # (causes 'Profile saved' message to be displayed) |
|
240 return helper.responses.redirectToChangedSuffix( |
|
241 request, None, params=params['edit_params']) |
|
242 else: # request.method == 'GET' |
|
243 if user: |
|
244 # is 'Profile saved' parameter present, but referrer was not ourself? |
|
245 # (e.g. someone bookmarked the GET that followed the POST submit) |
|
246 if (request.GET.get(self.DEF_SUBMIT_MSG_PARAM_NAME) |
|
247 and (not helper.requests.isReferrerSelf(request))): |
|
248 # redirect to aggressively remove 'Profile saved' query parameter |
|
249 return http.HttpResponseRedirect(request.path) |
|
250 |
|
251 # referrer was us, so select which submit message to display |
|
252 # (may display no message if ?s=0 parameter is not present) |
|
253 context['notice'] = ( |
|
254 helper.requests.getSingleIndexedParamValue( |
|
255 request, self.DEF_SUBMIT_MSG_PARAM_NAME, |
|
256 values=params['save_message'])) |
|
257 |
|
258 # populate form with the existing User entity |
|
259 form = UserForm(instance=user) |
|
260 else: |
|
261 if request.GET.get(self.DEF_SUBMIT_MSG_PARAM_NAME): |
|
262 # redirect to aggressively remove 'Profile saved' query parameter |
|
263 return http.HttpResponseRedirect(request.path) |
|
264 |
|
265 # no User entity exists for this Google Account, so show a blank form |
|
266 form = UserForm() |
|
267 |
|
268 context['form'] = form |
|
269 |
|
270 template = params['edit_template'] |
|
271 |
|
272 return helper.responses.respond(request, template, context) |
172 |
273 |
173 def _editGet(self, request, entity, form): |
274 def _editGet(self, request, entity, form): |
174 """See base.View._editGet(). |
275 """See base.View._editGet(). |
175 """ |
276 """ |
176 # fill in the email field with the data from the entity |
277 # fill in the email field with the data from the entity |