app/soc/views/user/profile.py
changeset 98 b2b823466a8b
parent 85 426b4ca2a72a
child 99 8c38b546a3cf
--- a/app/soc/views/user/profile.py	Wed Aug 20 18:36:55 2008 +0000
+++ b/app/soc/views/user/profile.py	Wed Aug 20 21:59:00 2008 +0000
@@ -21,29 +21,52 @@
   '"Pawel Solyga" <pawel.solyga@gmail.com>',
   ]
 
+import re
 
 from google.appengine.api import users
 from django import http
 from django import shortcuts
 from django import newforms as forms
 
-from soc.models import user
 from soc.views.helpers import forms_helpers
 from soc.views.helpers import response_helpers
 
+import soc.models.user
 
 class UserForm(forms_helpers.DbModelForm):
   """Django form displayed when creating or editing a User.
   """
+  LINKNAME_PATTERN = r'''(?x)
+      ^
+      [0-9a-z]  # start with ASCII digit or lowercase
+      (
+      [0-9a-z]  # additional ASCII digit or lowercase
+      |         # -OR-
+      _[0-9a-z] # underscore and ASCII digit or lowercase
+      )*        # zero or more of OR group
+      $'''
+  LINKNAME_REGEX = re.compile(LINKNAME_PATTERN)
 
+  
   class Meta:
     """Inner Meta class that defines some behavior for the form.
     """
     #: db.Model subclass for which the form will gather information
-    model = user.User
+    model = soc.models.user.User
 
     #: list of model fields which will *not* be gathered by the form
     exclude = ['id']
+  
+  def clean_link_name(self):
+    linkname = self.cleaned_data.get('link_name')
+    linkname_user = soc.models.user.User.getUserForLinkname(linkname)
+    user = users.get_current_user()
+    # if linkname exist in datastore and doesn't belong to current user
+    if linkname_user and (linkname_user.id != user):
+      raise forms.ValidationError("This link name is already in use.")
+    elif not self.LINKNAME_REGEX.match(linkname):
+      raise forms.ValidationError("This link name is in wrong format.")
+    return linkname
 
 
 def edit(request, linkname=None, template='soc/user/profile/edit.html'):
@@ -51,22 +74,58 @@
 
   Args:
     request: the standard django request object.
+    linkname: the User's site-unique "linkname" extracted from the URL
     template: the template path to use for rendering the template.
 
   Returns:
     A subclass of django.http.HttpResponse which either contains the form to
     be filled out, or a redirect to the correct view in the interface.
   """
+  #TODO(solydzajs): create controller for User and cleanup code in this handler
+  
+  #: If user not signed in redirect to sign-in page
   user = users.get_current_user()
   if not user:
     return http.HttpResponseRedirect(users.create_login_url(request.path))
 
+  soc_user = soc.models.user.User.getUser(user)
+  
+  #: Show custom 404 page when linkname in url doesn't match current user
+  if linkname:
+    linkname_user = soc.models.user.User.getUserForLinkname(linkname)
+    if (linkname_user and linkname_user.id != user) or not linkname_user:
+      return http.HttpResponseNotFound('No user exists with that link name "%s"' %
+                                linkname)
+
+  #: GET method
+  if (request.method != 'POST') and soc_user:
+    form = UserForm(initial={'nick_name': soc_user.nick_name,
+                             'link_name': soc_user.link_name})
+    return response_helpers.respond(request,
+        template, {'template': template, 
+                   'form': form, 
+                   'soc_nick_name': soc_user.nick_name})
+  
+  #: POST method
   form = UserForm()
-  if request.method=='POST':
+  if request.method == 'POST':
     form = UserForm(request.POST)
 
-    if not form.errors:
-      return http.HttpResponse('This would update the model')
+    if form.is_valid():
+      linkname = form.cleaned_data.get('link_name')
+      nickname = form.cleaned_data.get("nick_name")
+      if not soc_user:
+        soc_user = soc.models.user.User(id = user,link_name = linkname,
+                                        nick_name = nickname)
+      else:
+        soc_user.nick_name = nickname
+        soc_user.link_name = linkname
+      soc_user.put()
+      return response_helpers.respond(request,
+              template, {'template': template, 
+                        'form': form, 
+                        'soc_nick_name': nickname,
+                        'submit_message': 'Profile saved.'})
 
   return response_helpers.respond(request,
       template, {'template': template, 'form': form})