app/soc/views/user/profile.py
changeset 299 a1cc853a56e5
parent 298 c76a366c7ab4
child 305 972d28056d9d
equal deleted inserted replaced
298:c76a366c7ab4 299:a1cc853a56e5
    26 from django import http
    26 from django import http
    27 from django import shortcuts
    27 from django import shortcuts
    28 from django import newforms as forms
    28 from django import newforms as forms
    29 from django.utils.translation import ugettext_lazy
    29 from django.utils.translation import ugettext_lazy
    30 
    30 
       
    31 import soc.logic
    31 from soc.logic import validate
    32 from soc.logic import validate
    32 from soc.logic import out_of_band
    33 from soc.logic import out_of_band
    33 from soc.logic.site import id_user
    34 from soc.logic.site import id_user
    34 from soc.views import helper
    35 from soc.views import helper
    35 import soc.views.helper.forms
    36 import soc.views.helper.forms
    54   
    55   
    55   def clean_link_name(self):
    56   def clean_link_name(self):
    56     link_name = self.cleaned_data.get('link_name')
    57     link_name = self.cleaned_data.get('link_name')
    57     if not validate.isLinkNameFormatValid(link_name):
    58     if not validate.isLinkNameFormatValid(link_name):
    58       raise forms.ValidationError("This link name is in wrong format.")
    59       raise forms.ValidationError("This link name is in wrong format.")
    59     elif not id_user.isLinkNameAvailableForId(link_name):
    60 
       
    61     user = id_user.getUserFromLinkName(link_name)
       
    62 
       
    63     if user and not id_user.doesLinkNameBelongToId(link_name, user.id):
    60       raise forms.ValidationError("This link name is already in use.")
    64       raise forms.ValidationError("This link name is already in use.")
       
    65 
    61     return link_name
    66     return link_name
    62 
    67 
    63 
    68 
    64 DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/profile/edit.html'
    69 DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/profile/edit.html'
    65 
    70 
   107 
   112 
   108   link_name_user = None
   113   link_name_user = None
   109 
   114 
   110   # try to fetch User entity corresponding to link_name if one exists
   115   # try to fetch User entity corresponding to link_name if one exists
   111   try:
   116   try:
   112     linkname_user = id_user.getUserIfLinkName(linkname)
   117       if link_name:
       
   118         link_name_user = id_user.getUserFromLinkNameOrDie(link_name)
   113   except out_of_band.ErrorResponse, error:
   119   except out_of_band.ErrorResponse, error:
   114     # show custom 404 page when link name doesn't exist in Datastore
   120     # show custom 404 page when link name doesn't exist in Datastore
   115     return simple.errorResponse(request, error, template, context)
   121     return simple.errorResponse(request, error, template, context)
   116   
   122   
   117   # link_name_user will be None here if link name was already None...
   123   # link_name_user will be None here if link name was already None...
   122 
   128 
   123   if request.method == 'POST':
   129   if request.method == 'POST':
   124     form = UserForm(request.POST)
   130     form = UserForm(request.POST)
   125 
   131 
   126     if form.is_valid():
   132     if form.is_valid():
   127       new_linkname = form.cleaned_data.get('link_name')
   133       new_link_name = form.cleaned_data.get('link_name')
   128       nickname = form.cleaned_data.get("nick_name")
   134       properties = {}
       
   135       properties['link_name'] = new_link_name
       
   136       properties['nick_name'] = form.cleaned_data.get("nick_name")
       
   137       properties['id'] = id
   129 
   138 
   130       user = id_user.updateOrCreateUserFromId(
   139       user = soc.logic.user_logic.updateOrCreateFromFields(properties, email=id)
   131           id, link_name=new_linkname, nick_name=nickname)
       
   132 
   140 
   133       # redirect to new /user/profile/new_link_name?s=0
   141       # redirect to new /user/profile/new_link_name?s=0
   134       # (causes 'Profile saved' message to be displayed)
   142       # (causes 'Profile saved' message to be displayed)
   135       return helper.responses.redirectToChangedSuffix(
   143       return helper.responses.redirectToChangedSuffix(
   136           request, link_name, new_link_name, params=SUBMIT_PROFILE_SAVED_PARAMS)
   144           request, link_name, new_link_name, params=SUBMIT_PROFILE_SAVED_PARAMS)
   137   else: # request.method == 'GET'
   145   else: # request.method == 'GET'
   138     # try to fetch User entity corresponding to Google Account if one exists    
   146     # try to fetch User entity corresponding to Google Account if one exists
   139     user = id_user.getUserFromId(id)
   147     user = soc.logic.user_logic.getFromFields(email=id)
   140 
   148 
   141     if user:
   149     if user:
   142       # is 'Profile saved' parameter present, but referrer was not ourself?
   150       # is 'Profile saved' parameter present, but referrer was not ourself?
   143       # (e.g. someone bookmarked the GET that followed the POST submit) 
   151       # (e.g. someone bookmarked the GET that followed the POST submit) 
   144       if (request.GET.get(SUBMIT_MSG_PARAM_NAME)
   152       if (request.GET.get(SUBMIT_MSG_PARAM_NAME)