app/soc/views/site/user/profile.py
changeset 298 c76a366c7ab4
parent 294 1fdaab4a6ef2
child 299 a1cc853a56e5
equal deleted inserted replaced
297:35211afcd563 298:c76a366c7ab4
   139           if nearest_user_range_start is not None:            
   139           if nearest_user_range_start is not None:            
   140             context['lookup_link'] = './list?offset=%s&limit=%s' % (
   140             context['lookup_link'] = './list?offset=%s&limit=%s' % (
   141                 nearest_user_range_start, range_width)
   141                 nearest_user_range_start, range_width)
   142       if not user:
   142       if not user:
   143         # user not found yet, so see if link name was provided
   143         # user not found yet, so see if link name was provided
   144         linkname = form.cleaned_data.get('link_name')
   144         link_name = form.cleaned_data.get('link_name')
   145         
   145         
   146         if linkname:
   146         if link_name:
   147           # link name provided, so try to look up by link name 
   147           # link name provided, so try to look up by link name 
   148           user = id_user.getUserFromLinkName(linkname)
   148           user = id_user.getUserFromLinkName(link_name)
   149         
   149         
   150           if user:
   150           if user:
   151             lookup_message = ugettext_lazy('User found by link name.')
   151             lookup_message = ugettext_lazy('User found by link name.')
   152             email_error = None  # clear previous error, since User was found
   152             email_error = None  # clear previous error, since User was found
   153           else:
   153           else:
   154             context['linkname_error'] = ugettext_lazy(
   154             context['link_name_error'] = ugettext_lazy(
   155                 'User with that link name not found.')
   155                 'User with that link name not found.')
   156             range_width = helper.lists.getPreferredListPagination()
   156             range_width = helper.lists.getPreferredListPagination()
   157             nearest_user_range_start = id_user.findNearestUsersOffset(
   157             nearest_user_range_start = id_user.findNearestUsersOffset(
   158                 range_width, link_name=linkname)
   158                 range_width, link_name=link_name)
   159             
   159             
   160             if nearest_user_range_start is not None:
   160             if nearest_user_range_start is not None:
   161               context['lookup_link'] = './list?offset=%s&limit=%s' % (
   161               context['lookup_link'] = './list?offset=%s&limit=%s' % (
   162                   nearest_user_range_start, range_width)
   162                   nearest_user_range_start, range_width)
   163     # else: form was not valid
   163     # else: form was not valid
   236 DEF_SITE_USER_PROFILE_EDIT_TMPL = 'soc/site/user/profile/edit.html'
   236 DEF_SITE_USER_PROFILE_EDIT_TMPL = 'soc/site/user/profile/edit.html'
   237 DEF_CREATE_NEW_USER_MSG = ' You can create a new user by visiting' \
   237 DEF_CREATE_NEW_USER_MSG = ' You can create a new user by visiting' \
   238                           ' <a href="/site/user/profile">Create ' \
   238                           ' <a href="/site/user/profile">Create ' \
   239                           'a New User</a> page.'
   239                           'a New User</a> page.'
   240 
   240 
   241 def edit(request, linkname=None, template=DEF_SITE_USER_PROFILE_EDIT_TMPL):
   241 def edit(request, link_name=None, template=DEF_SITE_USER_PROFILE_EDIT_TMPL):
   242   """View for a Developer to modify the properties of a User Model entity.
   242   """View for a Developer to modify the properties of a User Model entity.
   243 
   243 
   244   Args:
   244   Args:
   245     request: the standard django request object
   245     request: the standard django request object
   246     linkname: the User's site-unique "linkname" extracted from the URL
   246     link_name: the User's site-unique "link_name" extracted from the URL
   247     template: the "sibling" template (or a search list of such templates)
   247     template: the "sibling" template (or a search list of such templates)
   248       from which to construct the public.html template name (or names)
   248       from which to construct the public.html template name (or names)
   249 
   249 
   250   Returns:
   250   Returns:
   251     A subclass of django.http.HttpResponse which either contains the form to
   251     A subclass of django.http.HttpResponse which either contains the form to
   260   # create default template context for use with any templates
   260   # create default template context for use with any templates
   261   context = helper.responses.getUniversalContext(request)
   261   context = helper.responses.getUniversalContext(request)
   262 
   262 
   263   user = None  # assume that no User entity will be found
   263   user = None  # assume that no User entity will be found
   264 
   264 
   265   # try to fetch User entity corresponding to linkname if one exists    
   265   # try to fetch User entity corresponding to link_name if one exists
   266   try:
   266   if link_name:
   267     user = id_user.getUserIfLinkName(linkname)
   267     user = id_user.getUserFromLinkName(link_name)
   268   except out_of_band.ErrorResponse, error:
       
   269     # show custom 404 page when link name doesn't exist in Datastore
       
   270     error.message = error.message + DEF_CREATE_NEW_USER_MSG
       
   271     return simple.errorResponse(request, error, template, context)
       
   272 
   268 
   273   if request.method == 'POST':
   269   if request.method == 'POST':
   274     form = EditForm(request.POST)
   270     form = EditForm(request.POST)
   275 
   271 
   276     if form.is_valid():
   272     if form.is_valid():
   277       form_id = form.cleaned_data.get('id')
       
   278       new_linkname = form.cleaned_data.get('link_name')
       
   279       nickname = form.cleaned_data.get('nick_name')
       
   280       is_developer = form.cleaned_data.get('is_developer')
       
   281       key_name = form.cleaned_data.get('key_name')
   273       key_name = form.cleaned_data.get('key_name')
       
   274       new_link_name = form.cleaned_data.get('link_name')
       
   275 
       
   276       properties = {}
       
   277       properties['id'] = form.cleaned_data.get('id')
       
   278       properties['link_name']  = new_link_name
       
   279       properties['nick_name']  = form.cleaned_data.get('nick_name')
       
   280       properties['is_developer'] = form.cleaned_data.get('is_developer')
   282       
   281       
   283       user = id_user.updateUserForKeyName(key_name=key_name, id=form_id, 
   282       user = soc.logic.user_logic.updateOrCreateFromKeyName(properties, key_name)
   284           link_name=new_linkname, nick_name=nickname, 
   283 
   285           is_developer=is_developer)
   284       #raise forms.ValidationError("lesseee: " + new_link_name + " " +  user.link_name)
   286 
   285 
   287       if not user:
   286       if not user:
   288         return http.HttpResponseRedirect('/')
   287         return http.HttpResponseRedirect('/')
   289         
   288         
   290       # redirect to new /site/user/profile/new_linkname?s=0
   289       # redirect to new /site/user/profile/new_link_name?s=0
   291       # (causes 'Profile saved' message to be displayed)
   290       # (causes 'Profile saved' message to be displayed)
   292       return helper.responses.redirectToChangedSuffix(
   291       return helper.responses.redirectToChangedSuffix(
   293           request, linkname, new_linkname,
   292           request, link_name, new_link_name,
   294           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
   293           params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
   295   else: # method == 'GET':
   294   else: # method == 'GET':
   296     # try to fetch User entity corresponding to link name if one exists
   295     # try to fetch User entity corresponding to link name if one exists
   297     if linkname:
   296     if link_name:
   298       if user:
   297       if user:
   299         # is 'Profile saved' parameter present, but referrer was not ourself?
   298         # is 'Profile saved' parameter present, but referrer was not ourself?
   300         # (e.g. someone bookmarked the GET that followed the POST submit) 
   299         # (e.g. someone bookmarked the GET that followed the POST submit) 
   301         if (request.GET.get(profile.SUBMIT_MSG_PARAM_NAME)
   300         if (request.GET.get(profile.SUBMIT_MSG_PARAM_NAME)
   302             and (not helper.requests.isReferrerSelf(request,
   301             and (not helper.requests.isReferrerSelf(request,
   303                                                     suffix=linkname))):
   302                                                     suffix=link_name))):
   304           # redirect to aggressively remove 'Profile saved' query parameter
   303           # redirect to aggressively remove 'Profile saved' query parameter
   305           return http.HttpResponseRedirect(request.path)
   304           return http.HttpResponseRedirect(request.path)
   306     
   305     
   307         # referrer was us, so select which submit message to display
   306         # referrer was us, so select which submit message to display
   308         # (may display no message if ?s=0 parameter is not present)
   307         # (may display no message if ?s=0 parameter is not present)
   320           # redirect to aggressively remove 'Profile saved' query parameter
   319           # redirect to aggressively remove 'Profile saved' query parameter
   321           return http.HttpResponseRedirect(request.path)
   320           return http.HttpResponseRedirect(request.path)
   322           
   321           
   323         context['lookup_error'] = ugettext_lazy(
   322         context['lookup_error'] = ugettext_lazy(
   324             'User with that link name not found.')
   323             'User with that link name not found.')
   325         form = EditForm(initial={'link_name': linkname})
   324         form = EditForm(initial={'link_name': link_name})
   326     else:  # no link name specified in the URL
   325     else:  # no link name specified in the URL
   327       if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   326       if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
   328         # redirect to aggressively remove 'Profile saved' query parameter
   327         # redirect to aggressively remove 'Profile saved' query parameter
   329         return http.HttpResponseRedirect(request.path)
   328         return http.HttpResponseRedirect(request.path)
   330 
   329