app/soc/views/user/profile.py
changeset 131 3db97cf7f2c7
parent 124 051afb721c22
child 135 a7ccde9d9eed
equal deleted inserted replaced
130:63248d9db484 131:3db97cf7f2c7
    20 __authors__ = [
    20 __authors__ = [
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    22   ]
    22   ]
    23 
    23 
    24 import re
    24 import re
       
    25 import logging
    25 
    26 
    26 from google.appengine.api import users
    27 from google.appengine.api import users
    27 from django import http
    28 from django import http
    28 from django import shortcuts
    29 from django import shortcuts
    29 from django import newforms as forms
    30 from django import newforms as forms
    39 
    40 
    40 
    41 
    41 class UserForm(forms_helpers.DbModelForm):
    42 class UserForm(forms_helpers.DbModelForm):
    42   """Django form displayed when creating or editing a User.
    43   """Django form displayed when creating or editing a User.
    43   """
    44   """
    44   LINKNAME_PATTERN = r'''(?x)
       
    45       ^
       
    46       [0-9a-z]  # start with ASCII digit or lowercase
       
    47       (
       
    48       [0-9a-z]  # additional ASCII digit or lowercase
       
    49       |         # -OR-
       
    50       _[0-9a-z] # underscore and ASCII digit or lowercase
       
    51       )*        # zero or more of OR group
       
    52       $'''
       
    53   LINKNAME_REGEX = re.compile(LINKNAME_PATTERN)
       
    54   
       
    55   class Meta:
    45   class Meta:
    56     """Inner Meta class that defines some behavior for the form.
    46     """Inner Meta class that defines some behavior for the form.
    57     """
    47     """
    58     #: db.Model subclass for which the form will gather information
    48     #: db.Model subclass for which the form will gather information
    59     model = soc.models.user.User
    49     model = soc.models.user.User
    60     
    50     
    61     #: list of model fields which will *not* be gathered by the form
    51     #: list of model fields which will *not* be gathered by the form
    62     exclude = ['id']
    52     exclude = ['id', 'former_ids']
    63   
    53   
    64   def clean_link_name(self):
    54   def clean_link_name(self):
    65     linkname = self.cleaned_data.get('link_name')
    55     link_name = self.cleaned_data.get('link_name')
    66     linkname_user = id_user.getUserFromLinkName(linkname)
    56     if not id_user.isLinkNameFormatValid(link_name):
    67     id = users.get_current_user()
    57       raise forms.ValidationError("This link name is in wrong format.")
    68     # if linkname exist in datastore and doesn't belong to current user
    58     elif not id_user.doesLinkNameBelongToId(link_name):
    69     if linkname_user and (linkname_user.id != id):
    59       # link_name exists in Datastore but doesn't belong to current user
    70       raise forms.ValidationError("This link name is already in use.")
    60       raise forms.ValidationError("This link name is already in use.")
    71     elif not self.LINKNAME_REGEX.match(linkname):
    61     return link_name
    72       raise forms.ValidationError("This link name is in wrong format.")
       
    73     return linkname
       
    74 
    62 
    75 
    63 
    76 DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/profile/edit.html'
    64 DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/profile/edit.html'
    77 
    65 
    78 def edit(request, linkname=None, template=DEF_USER_PROFILE_EDIT_TMPL):
    66 def edit(request, linkname=None, template=DEF_USER_PROFILE_EDIT_TMPL):
    79   """View for a User to modify the properties of a User Model entity.
    67   """View for a User to modify the properties of a User Model entity.
    80 
    68 
    81   Args:
    69   Args:
    82     request: the standard django request object.
    70     request: the standard django request object
    83     linkname: the User's site-unique "linkname" extracted from the URL
    71     linkname: the User's site-unique "linkname" extracted from the URL
    84     template: the template path to use for rendering the template.
    72     template: the template path to use for rendering the template
    85 
    73 
    86   Returns:
    74   Returns:
    87     A subclass of django.http.HttpResponse which either contains the form to
    75     A subclass of django.http.HttpResponse which either contains the form to
    88     be filled out, or a redirect to the correct view in the interface.
    76     be filled out, or a redirect to the correct view in the interface.
    89   """
    77   """
   116   if linkname_user and (linkname_user.id != id):
   104   if linkname_user and (linkname_user.id != id):
   117     # linkname_user exists but is not the currently logged in Google Account,
   105     # linkname_user exists but is not the currently logged in Google Account,
   118     # so show public view for that (other) User entity
   106     # so show public view for that (other) User entity
   119     return simple.public(request, template, linkname, context)
   107     return simple.public(request, template, linkname, context)
   120 
   108 
   121   user = id_user.getUserFromId(id)
       
   122   
       
   123   if request.method == 'POST':
   109   if request.method == 'POST':
   124     form = UserForm(request.POST)
   110     form = UserForm(request.POST)
   125 
   111 
   126     if form.is_valid():
   112     if form.is_valid():
   127       linkname = form.cleaned_data.get('link_name')
   113       linkname = form.cleaned_data.get('link_name')
   128       nickname = form.cleaned_data.get("nick_name")
   114       nickname = form.cleaned_data.get("nick_name")
   129 
   115 
   130       if not user:
   116       user = id_user.updateOrCreateUserFromId(
   131         user = soc.models.user.User(id=id, link_name=linkname,
   117           id, link_name=linkname, nick_name=nickname)
   132                                     nick_name=nickname)
       
   133       else:
       
   134         user.nick_name = nickname
       
   135         user.link_name = linkname
       
   136 
   118 
   137       user.put()
       
   138       # TODO(tlarsen):
   119       # TODO(tlarsen):
   139       # if old_linkname:  redirect to new /user/profile/new_linkname
   120       # if old_linkname:  redirect to new /user/profile/new_linkname
   140       #   (how to preserve displaying the "Profile saved" message?)
   121       #   (how to preserve displaying the "Profile saved" message?)
   141       context.update({'submit_message': 'Profile saved.'})
   122       context.update({'submit_message': 'Profile saved.'})
   142   else: # request.method == 'GET'
   123   else: # request.method == 'GET'
       
   124     # try to fetch User entity corresponding to Google Account if one exists    
       
   125     user = id_user.getUserFromId(id)
       
   126 
   143     if user:
   127     if user:
   144       # populate form with the existing User entity
   128       # populate form with the existing User entity
   145       form = UserForm(instance=user)
   129       form = UserForm(instance=user)
   146     else:
   130     else:
   147       # no User entity exists for this Google Account, so show a blank form
   131       # no User entity exists for this Google Account, so show a blank form