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