app/soc/views/site/user/profile.py
changeset 132 15d89c284106
child 134 1f64d7a4d82d
equal deleted inserted replaced
131:3db97cf7f2c7 132:15d89c284106
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Developer views for editing and examining User profiles.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 import re
       
    25 import logging
       
    26 
       
    27 from google.appengine.api import users
       
    28 from django import http
       
    29 from django import shortcuts
       
    30 from django import newforms as forms
       
    31 
       
    32 from soc.logic import out_of_band
       
    33 from soc.logic.site import id_user
       
    34 from soc.views import simple
       
    35 from soc.views.helpers import forms_helpers
       
    36 from soc.views.helpers import response_helpers
       
    37 from soc.views.helpers import template_helpers
       
    38 
       
    39 import soc.models.user
       
    40 
       
    41 
       
    42 class LookupForm(forms_helpers.DbModelForm):
       
    43   """Django form displayed for a Developer to look up a User.
       
    44   """
       
    45   id = forms.EmailField(required=False)
       
    46   link_name = forms.CharField(required=False)
       
    47 
       
    48   class Meta:
       
    49     model = None
       
    50 
       
    51   def clean_link_name(self):
       
    52     link_name = self.cleaned_data.get('link_name')
       
    53 
       
    54     if not link_name:
       
    55       # link name not supplied (which is OK), so do not try to validate it
       
    56       return None
       
    57 
       
    58     if not id_user.isLinkNameFormatValid(link_name):
       
    59       raise forms.ValidationError('This link name is in wrong format.')
       
    60     
       
    61     return link_name
       
    62 
       
    63   def clean_id(self):
       
    64     email = self.cleaned_data.get('id')
       
    65     
       
    66     if not email:
       
    67       # email not supplied (which is OK), so do not try to convert it
       
    68       return None
       
    69   
       
    70     try:
       
    71       return users.User(email=email)
       
    72     except users.UserNotFoundError:
       
    73       raise forms.ValidationError('Account not found.')
       
    74     
       
    75 
       
    76 DEF_SITE_USER_PROFILE_LOOKUP_TMPL = 'soc/site/user/profile/lookup.html'
       
    77 
       
    78 def lookup(request, template=DEF_SITE_USER_PROFILE_LOOKUP_TMPL):
       
    79   """View for a Developer to look up a User Model entity.
       
    80 
       
    81   Args:
       
    82     request: the standard django request object
       
    83     template: the "sibling" template (or a search list of such templates)
       
    84       from which to construct the public.html template name (or names)
       
    85 
       
    86   Returns:
       
    87     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.
       
    89   """
       
    90   # create default template context for use with any templates
       
    91   context = response_helpers.getUniversalContext(request)
       
    92 
       
    93   logged_in_id = users.get_current_user()
       
    94 
       
    95   if not logged_in_id:
       
    96     return simple.requestLogin(request, template, context,
       
    97         login_message_fmt='Please <a href="%(sign_in)s">sign in</a>'
       
    98                            ' as a site developer to view this page.')
       
    99 
       
   100   if not id_user.isIdDeveloper(id=logged_in_id):
       
   101     return simple.requestLogin(request, template, context,
       
   102         login_message_fmt='Please <a href="%(sign_out)s">sign out</a>'
       
   103                          ' and <a href="%(sign_in)s">sign in</a>'
       
   104                          ' again as a site developer to view this page.')
       
   105 
       
   106   user = None  # assume that no User entity will be found
       
   107   form = None  # assume blank form needs to be displayed
       
   108   lookup_message = 'Enter information to look up a User.'
       
   109   lookup_error = None  # assume no look-up errors
       
   110   edit_link = None  # assume no User entity found to be edited
       
   111 
       
   112   if request.method == 'POST':
       
   113     form = LookupForm(request.POST)
       
   114 
       
   115     if form.is_valid():
       
   116       form_id = form.cleaned_data.get('id')
       
   117       
       
   118       if form_id:
       
   119         # email provided, so attempt to look up user by email
       
   120         user = id_user.getUserFromId(form_id)
       
   121 
       
   122         if user:
       
   123           lookup_message = 'User found by email.'
       
   124         else:
       
   125           lookup_error = 'User with that email not found.'
       
   126 
       
   127       if not user:
       
   128         # user not found yet, so see if link name was provided
       
   129         linkname = form.cleaned_data.get('link_name')
       
   130         
       
   131         if linkname:
       
   132           # link name provided, so try to look up by link name 
       
   133           user = id_user.getUserFromLinkName(linkname)
       
   134         
       
   135           if user:
       
   136             lookup_message = 'User found by link name.'
       
   137             lookup_error = None  # clear previous error, now that User was found
       
   138           else:
       
   139             if form_id:
       
   140               # email was provided, so look up failure is due to both            
       
   141               lookup_error = 'User with that email or link name not found.'            
       
   142             else:
       
   143               # email was not provided, so look up failure is due to link name            
       
   144               lookup_error = 'User with that link name not found.'            
       
   145     # else: form was not valid
       
   146   # else:  # method == 'GET'
       
   147 
       
   148   if user:
       
   149     # User entity found, so populate form with existing User information            
       
   150     # context['found_user'] = user
       
   151     form = LookupForm(initial={'id': user.id,
       
   152                                'link_name': user.link_name})
       
   153 
       
   154     if request.path.endswith('lookup'):
       
   155       # convert /lookup path into /profile/link_name path
       
   156       edit_link = '%sprofile/%s' % (request.path[:-len('lookup')],
       
   157                                     user.link_name) 
       
   158     # else: URL is not one that was expected, so do not display edit link
       
   159   elif not form:
       
   160     # no pre-populated form was constructed, so show the empty look-up form
       
   161     form = DeveloperForm()
       
   162 
       
   163   context.update({'form': form,
       
   164                   'edit_link': edit_link,
       
   165                   'found_user': user,
       
   166                   'lookup_error': lookup_error,
       
   167                   'lookup_message': lookup_message})
       
   168 
       
   169   return response_helpers.respond(request, template, context)