app/soc/views/site/user/profile.py
changeset 184 7c0b42aecd9b
parent 179 4882d6c5630d
child 189 1cf3e7531382
equal deleted inserted replaced
183:37d98c3cefa5 184:7c0b42aecd9b
   189 
   189 
   190   is_developer = forms.BooleanField(required=False,
   190   is_developer = forms.BooleanField(required=False,
   191       label=soc.models.user.User.is_developer.verbose_name,
   191       label=soc.models.user.User.is_developer.verbose_name,
   192       help_text=soc.models.user.User.is_developer.help_text)
   192       help_text=soc.models.user.User.is_developer.help_text)
   193 
   193 
       
   194   key_name = forms.CharField(widget=forms.HiddenInput)
       
   195   
   194   class Meta:
   196   class Meta:
   195     model = None
   197     model = None
   196  
   198  
   197   def clean_link_name(self):
   199   def clean_link_name(self):
   198     link_name = self.cleaned_data.get('link_name')
   200     link_name = self.cleaned_data.get('link_name')
   199     if not id_user.isLinkNameFormatValid(link_name):
   201     if not id_user.isLinkNameFormatValid(link_name):
   200       raise forms.ValidationError("This link name is in wrong format.")
   202       raise forms.ValidationError("This link name is in wrong format.")
   201     else:
   203     else:
       
   204       key_name = self.data.get('key_name')
   202       if not id_user.isLinkNameAvailableForId(
   205       if not id_user.isLinkNameAvailableForId(
   203           link_name, id=self.cleaned_data.get('id')):
   206           link_name, id=id_user.getUserFromKeyName(key_name).id) :
   204         raise forms.ValidationError("This link name is already in use.")
   207         raise forms.ValidationError("This link name is already in use.")
   205     return link_name
   208     return link_name
   206 
   209 
   207   def clean_id(self):
   210   def clean_id(self):
   208     try:
   211     new_email = self.cleaned_data.get('id')
   209       return users.User(email=self.cleaned_data.get('id'))
   212     form_id = users.User(email=new_email)
   210     except users.UserNotFoundError:
   213     key_name = self.data.get('key_name')
   211       raise forms.ValidationError('Account not found.')
   214     old_email = id_user.getUserFromKeyName(key_name).id.email()
   212     
   215     if new_email != old_email:
       
   216       if id_user.isIdUser(form_id):
       
   217         raise forms.ValidationError("This account is already in use.")
       
   218     return form_id
       
   219 
   213 
   220 
   214 DEF_SITE_USER_PROFILE_EDIT_TMPL = 'soc/site/user/profile/edit.html'
   221 DEF_SITE_USER_PROFILE_EDIT_TMPL = 'soc/site/user/profile/edit.html'
   215 DEF_CREATE_NEW_USER_MSG = ' You can create a new user by visiting' \
   222 DEF_CREATE_NEW_USER_MSG = ' You can create a new user by visiting' \
   216                           ' <a href="/site/user/profile">Create ' \
   223                           ' <a href="/site/user/profile">Create ' \
   217                           'a New User</a> page.'
   224                           'a New User</a> page.'
   253     if form.is_valid():
   260     if form.is_valid():
   254       form_id = form.cleaned_data.get('id')
   261       form_id = form.cleaned_data.get('id')
   255       new_linkname = form.cleaned_data.get('link_name')
   262       new_linkname = form.cleaned_data.get('link_name')
   256       nickname = form.cleaned_data.get('nick_name')
   263       nickname = form.cleaned_data.get('nick_name')
   257       is_developer = form.cleaned_data.get('is_developer')
   264       is_developer = form.cleaned_data.get('is_developer')
       
   265       key_name = form.cleaned_data.get('key_name')
   258       
   266       
   259       user = id_user.updateOrCreateUserFromId(
   267       user = id_user.updateUserForKeyName(key_name=key_name, id=form_id, 
   260         form_id, link_name=new_linkname, nick_name=nickname,
   268           link_name=new_linkname, nick_name=nickname, 
   261         is_developer=is_developer)
   269           is_developer=is_developer)
   262 
   270 
       
   271       if not user:
       
   272         return http.HttpResponseRedirect('/')
       
   273         
   263       # redirect to new /site/user/profile/new_linkname?s=0
   274       # redirect to new /site/user/profile/new_linkname?s=0
   264       # (causes 'Profile saved' message to be displayed)
   275       # (causes 'Profile saved' message to be displayed)
   265       return response_helpers.redirectToChangedSuffix(
   276       return response_helpers.redirectToChangedSuffix(
   266           request, linkname, new_linkname,
   277           request, linkname, new_linkname,
   267           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
   278           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
   283             template_helpers.getSingleIndexedParamValue(
   294             template_helpers.getSingleIndexedParamValue(
   284                 request, profile.SUBMIT_MSG_PARAM_NAME,
   295                 request, profile.SUBMIT_MSG_PARAM_NAME,
   285                 values=profile.SUBMIT_MESSAGES))
   296                 values=profile.SUBMIT_MESSAGES))
   286 
   297 
   287         # populate form with the existing User entity
   298         # populate form with the existing User entity
   288         form = EditForm(initial={
   299         form = EditForm(initial={ 'key_name': user.key().name(),
   289             'id': user.id.email, 'link_name': user.link_name,
   300             'id': user.id.email, 'link_name': user.link_name,
   290             'nick_name': user.nick_name, 'is_developer': user.is_developer})       
   301             'nick_name': user.nick_name, 'is_developer': user.is_developer})       
   291       else:
   302       else:
   292         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   303         if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   293           # redirect to aggressively remove 'Profile saved' query parameter
   304           # redirect to aggressively remove 'Profile saved' query parameter