Add a /site/user/lookup view that provides Developers with a form to look up
authorTodd Larsen <tlarsen@google.com>
Fri, 12 Sep 2008 02:37:15 +0000
changeset 132 15d89c284106
parent 131 3db97cf7f2c7
child 133 84ae145bcef3
Add a /site/user/lookup view that provides Developers with a form to look up User entities given email addresses or link names.
app/soc/templates/soc/site/user/profile/create.html
app/soc/templates/soc/site/user/profile/edit.html
app/soc/templates/soc/site/user/profile/lookup.html
app/soc/views/site/user/__init__.py
app/soc/views/site/user/profile.py
app/urls.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/site/user/profile/create.html	Fri Sep 12 02:37:15 2008 +0000
@@ -0,0 +1,68 @@
+{% extends "soc/base.html" %}
+{% comment %}
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+{% endcomment %}
+{% load forms_helpers %}
+{% block page_title %}
+ {% if linkname_user %}
+  Developer: Modify Existing User Profile
+ {% else %}
+  Developer: Create New User Profile
+ {% endif %}
+{% endblock %}
+{% block header_title %}
+ {% if linkname_user %}
+  Modify Existing User Profile for {{ linkname_user.nick_name }}
+   <a href="mailto:{{ linkname_user.id }} ">&lt;{{ linkname_user.id }}&gt;</a>
+ {% else %}
+  Create a New User Profile
+ {% endif %}
+{% endblock %}
+{% block body %}
+<p>
+<p>
+{% block instructions %}
+Please use this form to alter basic site-wide settings for a User in Google Open Source Programs.
+{% endblock %}
+</p>
+<form method="POST">
+ <table>
+  {% field_as_table_row form.id %}
+  {% field_as_table_row form.link_name %}
+  <tr>
+   <td> 
+    <input type="submit" style="font-weight: bold" name="lookup" value="Look up User"/></span>
+   </td>
+   <td>&nbsp;</td>
+   <td>&nbsp;</td>
+   <td>
+    {% if lookup_message %}<b><i>{{ lookup_message }}</i></b>{% endif %}
+   </td>
+  <tr><td colspan="4">&nbsp;</td></tr>
+  </tr>
+  {% field_as_table_row form.nick_name %}
+  <tr>
+   <td> 
+    <input type="submit" style="font-weight: bold" name="save" value="Save Changes"/></span>
+   </td>
+   <td>
+    <input type="button" onclick="location.href='/'" value="Cancel"/>
+   </td>
+   <td>
+    {% if submit_message %}<b><i>{{ submit_message }}</i></b>{% endif %}
+   </td>
+  </tr>
+ </table>
+</form>
+</p>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/site/user/profile/edit.html	Fri Sep 12 02:37:15 2008 +0000
@@ -0,0 +1,68 @@
+{% extends "soc/base.html" %}
+{% comment %}
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+{% endcomment %}
+{% load forms_helpers %}
+{% block page_title %}
+ {% if linkname_user %}
+  Developer: Modify Existing User Profile
+ {% else %}
+  Developer: Create New User Profile
+ {% endif %}
+{% endblock %}
+{% block header_title %}
+ {% if linkname_user %}
+  Modify Existing User Profile for {{ linkname_user.nick_name }}
+   <a href="mailto:{{ linkname_user.id }} ">&lt;{{ linkname_user.id }}&gt;</a>
+ {% else %}
+  Create a New User Profile
+ {% endif %}
+{% endblock %}
+{% block body %}
+<p>
+<p>
+{% block instructions %}
+Please use this form to alter basic site-wide settings for a User in Google Open Source Programs.
+{% endblock %}
+</p>
+<form method="POST">
+ <table>
+  {% field_as_table_row form.id %}
+  {% field_as_table_row form.link_name %}
+  <tr>
+   <td> 
+    <input type="submit" style="font-weight: bold" name="lookup" value="Look up User"/></span>
+   </td>
+   <td>&nbsp;</td>
+   <td>&nbsp;</td>
+   <td>
+    {% if lookup_message %}<b><i>{{ lookup_message }}</i></b>{% endif %}
+   </td>
+  <tr><td colspan="4">&nbsp;</td></tr>
+  </tr>
+  {% field_as_table_row form.nick_name %}
+  <tr>
+   <td> 
+    <input type="submit" style="font-weight: bold" name="save" value="Save Changes"/></span>
+   </td>
+   <td>
+    <input type="button" onclick="location.href='/'" value="Cancel"/>
+   </td>
+   <td>
+    {% if submit_message %}<b><i>{{ submit_message }}</i></b>{% endif %}
+   </td>
+  </tr>
+ </table>
+</form>
+</p>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/site/user/profile/lookup.html	Fri Sep 12 02:37:15 2008 +0000
@@ -0,0 +1,72 @@
+{% extends "soc/base.html" %}
+{% comment %}
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+{% endcomment %}
+{% load forms_helpers %}
+{% block page_title %}
+Developer: Look Up an Existing User Profile
+{% endblock %}
+{% block header_title %}
+Look Up an Existing User Profile
+{% endblock %}
+{% block body %}
+<p>
+<p>
+{% block instructions %}
+Please use this form to look up an existing User in Google Open Source Programs.
+{% endblock %}
+</p>
+<form method="POST">
+ <table>
+  {% field_as_table_row form.id %}
+  {% field_as_table_row form.link_name %}
+{% if found_user %}
+ <tr>
+  <td class="formfieldlabel">Nick name</td>
+  <td class="formfieldrequired">&nbsp;</td>
+  <td>{{ found_user.nick_name }}</td>
+  <td class="formfieldhelptext">&nbsp;</td>
+ </tr>
+{% endif %}
+  <tr>
+   <td colspan="4">&nbsp;</td>
+  </tr>
+  <tr>
+   <td> 
+    <input type="submit" style="font-weight: bold" name="lookup" value="Look up User"/></span>
+   </td>
+   <td>&nbsp;</td>
+{% if lookup_error %}
+   <td colspan="2" class="formfielderror">
+    {{ lookup_error }}
+   </td>
+{% else %}
+   <td colspan="2">
+    {% if lookup_message %}<b><i>{{ lookup_message }}</i></b>{% else %}%nbsp;{% endif %}
+   </td>
+{% endif %}
+  </tr>
+{% if edit_link %}
+  <tr>
+   <td colspan="4">&nbsp;</td>
+  </tr>
+  <tr>
+   <td colspan="4">
+    <a href="{{ edit_link }}">Edit the User profile of {{ user.nick_name }}.</a>
+   </td>
+  </tr>
+{% endif %}
+ </table>
+</form>
+</p>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/views/site/user/profile.py	Fri Sep 12 02:37:15 2008 +0000
@@ -0,0 +1,169 @@
+#!/usr/bin/python2.5
+#
+# Copyright 2008 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Developer views for editing and examining User profiles.
+"""
+
+__authors__ = [
+  '"Todd Larsen" <tlarsen@google.com>',
+  ]
+
+import re
+import logging
+
+from google.appengine.api import users
+from django import http
+from django import shortcuts
+from django import newforms as forms
+
+from soc.logic import out_of_band
+from soc.logic.site import id_user
+from soc.views import simple
+from soc.views.helpers import forms_helpers
+from soc.views.helpers import response_helpers
+from soc.views.helpers import template_helpers
+
+import soc.models.user
+
+
+class LookupForm(forms_helpers.DbModelForm):
+  """Django form displayed for a Developer to look up a User.
+  """
+  id = forms.EmailField(required=False)
+  link_name = forms.CharField(required=False)
+
+  class Meta:
+    model = None
+
+  def clean_link_name(self):
+    link_name = self.cleaned_data.get('link_name')
+
+    if not link_name:
+      # link name not supplied (which is OK), so do not try to validate it
+      return None
+
+    if not id_user.isLinkNameFormatValid(link_name):
+      raise forms.ValidationError('This link name is in wrong format.')
+    
+    return link_name
+
+  def clean_id(self):
+    email = self.cleaned_data.get('id')
+    
+    if not email:
+      # email not supplied (which is OK), so do not try to convert it
+      return None
+  
+    try:
+      return users.User(email=email)
+    except users.UserNotFoundError:
+      raise forms.ValidationError('Account not found.')
+    
+
+DEF_SITE_USER_PROFILE_LOOKUP_TMPL = 'soc/site/user/profile/lookup.html'
+
+def lookup(request, template=DEF_SITE_USER_PROFILE_LOOKUP_TMPL):
+  """View for a Developer to look up a User Model entity.
+
+  Args:
+    request: the standard django request object
+    template: the "sibling" template (or a search list of such templates)
+      from which to construct the public.html template name (or names)
+
+  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.
+  """
+  # create default template context for use with any templates
+  context = response_helpers.getUniversalContext(request)
+
+  logged_in_id = users.get_current_user()
+
+  if not logged_in_id:
+    return simple.requestLogin(request, template, context,
+        login_message_fmt='Please <a href="%(sign_in)s">sign in</a>'
+                           ' as a site developer to view this page.')
+
+  if not id_user.isIdDeveloper(id=logged_in_id):
+    return simple.requestLogin(request, template, context,
+        login_message_fmt='Please <a href="%(sign_out)s">sign out</a>'
+                         ' and <a href="%(sign_in)s">sign in</a>'
+                         ' again as a site developer to view this page.')
+
+  user = None  # assume that no User entity will be found
+  form = None  # assume blank form needs to be displayed
+  lookup_message = 'Enter information to look up a User.'
+  lookup_error = None  # assume no look-up errors
+  edit_link = None  # assume no User entity found to be edited
+
+  if request.method == 'POST':
+    form = LookupForm(request.POST)
+
+    if form.is_valid():
+      form_id = form.cleaned_data.get('id')
+      
+      if form_id:
+        # email provided, so attempt to look up user by email
+        user = id_user.getUserFromId(form_id)
+
+        if user:
+          lookup_message = 'User found by email.'
+        else:
+          lookup_error = 'User with that email not found.'
+
+      if not user:
+        # user not found yet, so see if link name was provided
+        linkname = form.cleaned_data.get('link_name')
+        
+        if linkname:
+          # link name provided, so try to look up by link name 
+          user = id_user.getUserFromLinkName(linkname)
+        
+          if user:
+            lookup_message = 'User found by link name.'
+            lookup_error = None  # clear previous error, now that User was found
+          else:
+            if form_id:
+              # email was provided, so look up failure is due to both            
+              lookup_error = 'User with that email or link name not found.'            
+            else:
+              # email was not provided, so look up failure is due to link name            
+              lookup_error = 'User with that link name not found.'            
+    # else: form was not valid
+  # else:  # method == 'GET'
+
+  if user:
+    # User entity found, so populate form with existing User information            
+    # context['found_user'] = user
+    form = LookupForm(initial={'id': user.id,
+                               'link_name': user.link_name})
+
+    if request.path.endswith('lookup'):
+      # convert /lookup path into /profile/link_name path
+      edit_link = '%sprofile/%s' % (request.path[:-len('lookup')],
+                                    user.link_name) 
+    # else: URL is not one that was expected, so do not display edit link
+  elif not form:
+    # no pre-populated form was constructed, so show the empty look-up form
+    form = DeveloperForm()
+
+  context.update({'form': form,
+                  'edit_link': edit_link,
+                  'found_user': user,
+                  'lookup_error': lookup_error,
+                  'lookup_message': lookup_message})
+
+  return response_helpers.respond(request, template, context)
--- a/app/urls.py	Fri Sep 12 02:12:38 2008 +0000
+++ b/app/urls.py	Fri Sep 12 02:37:15 2008 +0000
@@ -25,11 +25,21 @@
     '',
     (r'^$', 'soc.views.site.home.public'),
 
+    # TODO(tlarsen): uncomment these when the view functions are committed
     # attempt to send User to their dashboard
     # (will display soc.views.user.roles.public() if "linkname" is not
     # current logged-in User)    
-    (r'^user/roles/(?P<linkname>[_0-9a-z]+)$',
-     'soc.views.user.roles.dashboard'),
+    # (r'^user/roles$',
+    #  'soc.views.user.roles.dashboard'),
+    # (r'^user/roles/(?P<linkname>[_0-9a-z]+)$',
+    #  'soc.views.user.roles.dashboard'),
+
+    (r'^site/user/lookup$', 'soc.views.site.user.profile.lookup'),
+
+    # TODO(tlarsen): uncomment these when the view functions are committed
+    # (r'^site/user/profile$', 'soc.views.site.user.profile.create'),
+    # (r'^site/user/profile/(?P<linkname>[_0-9a-z]+)$',
+    #  'soc.views.site.user.profile.edit'),
 
     (r'^user/profile$', 'soc.views.user.profile.edit'),
     (r'^user/profile/(?P<linkname>[_0-9a-z]+)$',