# HG changeset patch
# User Pawel Solyga
# Date 1219269540 0
# Node ID b2b823466a8b7a2cd7a1cbece8261a4bc6a4c54e
# Parent d01d0ae7d6c8639ee53f2dce4ce2d18700c65112
User Profile view (without controller yet).
Patch by: Pawel Solyga
Review by: Todd Larsen
Review issue: 386
Review URL: http://codereviews.googleopensourceprograms.com/386
diff -r d01d0ae7d6c8 -r b2b823466a8b app/soc/models/user.py
--- a/app/soc/models/user.py Wed Aug 20 18:36:55 2008 +0000
+++ b/app/soc/models/user.py Wed Aug 20 21:59:00 2008 +0000
@@ -63,3 +63,36 @@
link_name.help_text = ugettext_lazy(
'Field used in URLs to identify user. '
'Lower ASCII characters only.')
+
+ @staticmethod
+ def doesUserExist(user=None):
+ """Returns if user already exists in the Datastore.
+
+ Args:
+ user: a Google Account object,
+ """
+ #: let's do a gql query and check if user exists in datastore
+ data = self.getUser(user)
+ if data:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def getUser(user=None):
+ """Returns User entity from datastore, or None if not found.
+
+ Args:
+ user: a Google Account object,
+ """
+ return User.gql('WHERE id = :1', user).get()
+
+ @staticmethod
+ def getUserForLinkname(link_name=None):
+ """Returns User entity for linkname or None if not found.
+
+ Args:
+ link_name: linkname used in URLs to identify user,
+ """
+ return User.gql('WHERE link_name = :1', link_name).get()
+
\ No newline at end of file
diff -r d01d0ae7d6c8 -r b2b823466a8b app/soc/templates/soc/user/profile/edit.html
--- a/app/soc/templates/soc/user/profile/edit.html Wed Aug 20 18:36:55 2008 +0000
+++ b/app/soc/templates/soc/user/profile/edit.html Wed Aug 20 21:59:00 2008 +0000
@@ -14,10 +14,19 @@
{% endcomment %}
{% load forms_helpers %}
{% block page_title %}User Profile{% endblock %}
-{% block header_title %}User Profile{% endblock %}
+{% block header_title %}
+{% if soc_nick_name %}
+Modify Existing User Profile for {{ soc_nick_name }} <{{ user.email }}>
+{% else %}
+Create a New User Profile for <{{ user.email }}>
+{% endif %}
+{% endblock %}
{% block body %}
+{% if submit_message %}
{{ submit_message }}
{% endif %}
+
+
{% block instructions %}
Please use this form to set basic site-wide settings for your participation in Google Open Source Programs.
{% endblock %}
diff -r d01d0ae7d6c8 -r b2b823466a8b app/soc/views/user/profile.py
--- 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" ',
]
+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})