167 |
167 |
168 return response_helpers.respond(request, template, context) |
168 return response_helpers.respond(request, template, context) |
169 |
169 |
170 |
170 |
171 class EditForm(forms_helpers.DbModelForm): |
171 class EditForm(forms_helpers.DbModelForm): |
172 """Django form displayed when Developer creates or edits a User. |
172 """Django form displayed when Developer edits a User. |
173 |
173 |
174 This form is manually specified, instead of using |
174 This form is manually specified, instead of using |
175 model = soc.models.user.User |
175 model = soc.models.user.User |
176 in the Meta class, because the form behavior is unusual and normally |
176 in the Meta class, because the form behavior is unusual and normally |
177 required Properties of the User model need to sometimes be omitted. |
177 required Properties of the User model need to sometimes be omitted. |
317 |
317 |
318 context.update({'form': form, |
318 context.update({'form': form, |
319 'existing_user': user}) |
319 'existing_user': user}) |
320 |
320 |
321 return response_helpers.respond(request, template, context) |
321 return response_helpers.respond(request, template, context) |
|
322 |
|
323 |
|
324 class CreateForm(forms_helpers.DbModelForm): |
|
325 """Django form displayed when Developer creates a User. |
|
326 |
|
327 This form is manually specified, instead of using |
|
328 model = soc.models.user.User |
|
329 in the Meta class, because the form behavior is unusual and normally |
|
330 required Properties of the User model need to sometimes be omitted. |
|
331 """ |
|
332 id = forms.EmailField( |
|
333 label=soc.models.user.User.id.verbose_name, |
|
334 help_text=soc.models.user.User.id.help_text) |
|
335 |
|
336 link_name = forms.CharField( |
|
337 label=soc.models.user.User.link_name.verbose_name, |
|
338 help_text=soc.models.user.User.link_name.help_text) |
|
339 |
|
340 nick_name = forms.CharField( |
|
341 label=soc.models.user.User.nick_name.verbose_name) |
|
342 |
|
343 is_developer = forms.BooleanField(required=False, |
|
344 label=soc.models.user.User.is_developer.verbose_name, |
|
345 help_text=soc.models.user.User.is_developer.help_text) |
|
346 |
|
347 class Meta: |
|
348 model = None |
|
349 |
|
350 |
|
351 def clean_link_name(self): |
|
352 link_name = self.cleaned_data.get('link_name') |
|
353 if not id_user.isLinkNameFormatValid(link_name): |
|
354 raise forms.ValidationError("This link name is in wrong format.") |
|
355 else: |
|
356 if id_user.doesLinkNameExist(link_name): |
|
357 raise forms.ValidationError("This link name is already in use.") |
|
358 return link_name |
|
359 |
|
360 def clean_id(self): |
|
361 new_email = self.cleaned_data.get('id') |
|
362 form_id = users.User(email=new_email) |
|
363 if id_user.isIdUser(form_id): |
|
364 raise forms.ValidationError("This account is already in use.") |
|
365 return form_id |
|
366 |
|
367 |
|
368 DEF_SITE_CREATE_USER_PROFILE_TMPL = 'soc/site/user/profile/edit.html' |
|
369 |
|
370 def create(request, template=DEF_SITE_CREATE_USER_PROFILE_TMPL): |
|
371 """View for a Developer to modify the properties of a User Model entity. |
|
372 |
|
373 Args: |
|
374 request: the standard django request object |
|
375 template: the "sibling" template (or a search list of such templates) |
|
376 from which to construct the public.html template name (or names) |
|
377 |
|
378 Returns: |
|
379 A subclass of django.http.HttpResponse which either contains the form to |
|
380 be filled out, or a redirect to the correct view in the interface. |
|
381 """ |
|
382 # create default template context for use with any templates |
|
383 context = response_helpers.getUniversalContext(request) |
|
384 |
|
385 alt_response = simple.getAltResponseIfNotDeveloper(request, |
|
386 context=context) |
|
387 if alt_response: |
|
388 return alt_response |
|
389 |
|
390 if request.method == 'POST': |
|
391 form = CreateForm(request.POST) |
|
392 |
|
393 if form.is_valid(): |
|
394 form_id = form.cleaned_data.get('id') |
|
395 new_linkname = form.cleaned_data.get('link_name') |
|
396 nickname = form.cleaned_data.get('nick_name') |
|
397 is_developer = form.cleaned_data.get('is_developer') |
|
398 |
|
399 user = id_user.updateOrCreateUserFromId(id=form_id, |
|
400 link_name=new_linkname, nick_name=nickname, |
|
401 is_developer=is_developer) |
|
402 |
|
403 if not user: |
|
404 return http.HttpResponseRedirect('/') |
|
405 |
|
406 # redirect to new /site/user/profile/new_linkname?s=0 |
|
407 # (causes 'Profile saved' message to be displayed) |
|
408 return response_helpers.redirectToChangedSuffix( |
|
409 request, None, new_linkname, |
|
410 params=profile.SUBMIT_PROFILE_SAVED_PARAMS) |
|
411 else: # method == 'GET': |
|
412 # no link name specified, so start with an empty form |
|
413 form = CreateForm() |
|
414 |
|
415 context.update({'form': form}) |
|
416 |
|
417 return response_helpers.respond(request, template, context) |