app/soc/views/site/user/profile.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 15 Nov 2008 16:17:11 +0000
changeset 482 839740b061ad
parent 481 94834a1e6c01
child 483 70f21691b0e1
permissions -rw-r--r--
Factor out direct use of the page object Instead of directly using the page object in the html, pass around page_name. This will make it easier to remove Page in favor of a simpler implementation.
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
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
from google.appengine.api import users
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    26
324
05e21c089be6 Add missing import in soc/views/site/sponsor/list.py which caused exception when app was deployed and first site you visited was "List Site Sponsors". Update files according to recent django update and django backwards incompatibility (for example newforms is changed to forms).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 321
diff changeset
    27
from django import forms
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
    28
from django import http
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    29
from django.utils.translation import ugettext_lazy
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
    30
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    31
from soc.logic import accounts
309
7190b224c701 Made the existing code use the new soc.logic.models
Sverre Rabbelier <srabbelier@gmail.com>
parents: 305
diff changeset
    32
from soc.logic import models
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    33
from soc.logic import out_of_band
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    34
from soc.logic import validate
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
    35
from soc.views import simple
268
af1d7f48b361 Move helpers/list.py to helper/lists.py to avoid conflict with built-in type
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
    36
from soc.views import helper
303
4f1bb54ddae5 Moved soc/logic/helper/access to soc/views/helper/access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
    37
from soc.views.helper import access
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
    38
from soc.views.helper import decorators
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    39
from soc.views.user import profile
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    40
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    41
import soc.logic
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    42
import soc.models.user
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 272
diff changeset
    43
import soc.views.helper.forms
268
af1d7f48b361 Move helpers/list.py to helper/lists.py to avoid conflict with built-in type
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
    44
import soc.views.helper.lists
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
    45
import soc.views.helper.requests
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
    46
import soc.views.helper.responses
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
    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
408
7cd6bdfbf95c Inherit from BaseForm in all forms instead of from DbModelForm. This change doesn't include changes to templates and usage of as_table everywhere (work in progress).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
    49
class LookupForm(helper.forms.BaseForm):
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
    50
  """Django form displayed for a Developer to look up a User.
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    51
  
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    52
  This form is manually specified, instead of using
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    53
    model = soc.models.user.User
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    54
  in the Meta class, because the form behavior is unusual and normally
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    55
  required Properties of the User model need to sometimes be omitted.
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    56
  
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    57
  Also, this form only permits entry and editing  of some of the User entity
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    58
  Properties, not all of them.
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
    59
  """
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    60
  account = forms.EmailField(required=False,
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    61
      label=soc.models.user.User.account.verbose_name,
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    62
      help_text=soc.models.user.User.account.help_text)
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    63
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    64
  link_name = forms.CharField(required=False,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    65
      label=soc.models.user.User.link_name.verbose_name,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
    66
      help_text=soc.models.user.User.link_name.help_text)
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
    67
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
  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
    69
    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
    70
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
  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
    72
    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
    73
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
    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
    75
      # 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
    76
      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
    77
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
    78
    if not validate.isLinkNameFormatValid(link_name):
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
    79
      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
    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
    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
    82
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    83
  def clean_account(self):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    84
    email = self.cleaned_data.get('account')
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
    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
    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
    87
      # 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
    88
      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
    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
    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
    91
      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
    92
    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
    93
      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
    94
    
15d89c284106 Add a /site/user/lookup view that provides Developers with a form to look up
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
445
31927f21970d Major site restructuring, also deleting of many unused or obsolete documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 435
diff changeset
    96
DEF_SITE_USER_PROFILE_LOOKUP_TMPL = 'soc/user/lookup.html'
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
    97
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
    98
@decorators.view
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    99
def lookup(request, page_name=None, template=DEF_SITE_USER_PROFILE_LOOKUP_TMPL):
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
   100
  """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
   101
15d89c284106 Add a /site/user/lookup view that provides Developers with a form to look up
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
  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
   103
    request: the standard django request object
358
843d83b87282 Add page=None parameter to all views. Information from page argument (which is soc.logic.site.page.Page object) is going to be used later in views code and for some context values. Fix some indentions in __doc__ strings. Add proper __doc__ string for all() function in sponsor/list.py module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 355
diff changeset
   104
    page: a soc.logic.site.page.Page object which is abstraction that combines 
843d83b87282 Add page=None parameter to all views. Information from page argument (which is soc.logic.site.page.Page object) is going to be used later in views code and for some context values. Fix some indentions in __doc__ strings. Add proper __doc__ string for all() function in sponsor/list.py module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 355
diff changeset
   105
      a Django view with sidebar menu info
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
    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
   107
      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
   108
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
  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
   110
    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
   111
    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
   112
  """
294
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   113
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   114
  try:
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   115
    access.checkIsDeveloper(request)
303
4f1bb54ddae5 Moved soc/logic/helper/access to soc/views/helper/access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
   116
  except  soc.views.out_of_band.AccessViolationResponse, alt_response:
294
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   117
    return alt_response.response()
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   118
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
   119
  # create default template context for use with any templates
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   120
  context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   121
  context['page_name'] = page_name
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
   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
  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
   124
  form = None  # assume blank form needs to be displayed
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   125
  lookup_message = ugettext_lazy('Enter information to look up a User.')
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   126
  email_error = None  # assume no email look-up errors
325
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   127
  context['lookup_link'] = None
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
   128
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
  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
   130
    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
   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 form.is_valid():
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   133
      form_account = form.cleaned_data.get('account')
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
   134
      
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   135
      if form_account:
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
   136
        # email provided, so attempt to look up user by email
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   137
        user = models.user.logic.getForFields(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   138
            {'account': form_account}, unique=True)
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
   139
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 user:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   141
          lookup_message = ugettext_lazy('User found by email.')
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
   142
        else:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   143
          email_error = ugettext_lazy('User with that email not found.')
268
af1d7f48b361 Move helpers/list.py to helper/lists.py to avoid conflict with built-in type
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
   144
          range_width = helper.lists.getPreferredListPagination()
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   145
          nearest_user_range_start = (
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   146
              models.user.logic.findNearestEntitiesOffset(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   147
                  width, [('account', form_account)]))
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   148
            
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   149
          if nearest_user_range_start is not None:            
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   150
            context['lookup_link'] = './list?offset=%s&limit=%s' % (
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   151
                nearest_user_range_start, range_width)
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
   152
      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
   153
        # user not found yet, so see if link name was provided
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   154
        link_name = form.cleaned_data.get('link_name')
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
   155
        
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   156
        if link_name:
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
   157
          # link name provided, so try to look up by link name 
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   158
          user = models.user.logic.getForFields({'link_name': link_name},
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   159
                                                unique=True)        
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
   160
          if user:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   161
            lookup_message = ugettext_lazy('User found by link name.')
325
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   162
            # clear previous error, since User was found
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   163
            email_error = None
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   164
            # clear previous lookup_link, since User was found, the lookup_link
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   165
            # is not needed to display.
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   166
            context['lookup_link'] = None
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
   167
          else:
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   168
            context['link_name_error'] = ugettext_lazy(
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   169
                'User with that link name not found.')
325
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   170
            if context['lookup_link'] is None:
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   171
              range_width = helper.lists.getPreferredListPagination()
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   172
              nearest_user_range_start = (
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   173
                models.user.logic.findNearestEntitiesOffset(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   174
                    width, [('link_name', link_name)]))
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   175
            
325
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   176
              if nearest_user_range_start is not None:
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   177
                context['lookup_link'] = './list?offset=%s&limit=%s' % (
1469eff8f59e Fix some defects for lookup user view. More information about the patch can be found under the link below.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   178
                    nearest_user_range_start, range_width)
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
   179
    # 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
   180
  # 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
   181
15d89c284106 Add a /site/user/lookup view that provides Developers with a form to look up
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
  if user:
215
d020c95b17b1 Clean up too long lines in profile.py. Add ReadOnlyInput widget to custom_widgets module, it can be used to display read-only form fields. Display read-only "Id" field in LookUp view if user has been found and in User Edit Developer view.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 210
diff changeset
   183
    # User entity found, so populate form with existing User information
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
   184
    # context['found_user'] = user
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   185
    form = LookupForm(initial={'account': user.account.email(),
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
   186
                               '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
   187
15d89c284106 Add a /site/user/lookup view that provides Developers with a form to look up
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   188
    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
   189
      # convert /lookup path into /profile/link_name path
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   190
      context['edit_link'] = helper.requests.replaceSuffix(
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   191
          request.path, 'lookup', 'profile/%s' % user.link_name)
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
   192
    # 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
   193
  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
   194
    # 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
   195
    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
   196
15d89c284106 Add a /site/user/lookup view that provides Developers with a form to look up
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   197
  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
   198
                  'found_user': user,
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   199
                  'email_error': email_error,
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
   200
                  '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
   201
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   202
  return helper.responses.respond(request, template, context)
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   203
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   204
408
7cd6bdfbf95c Inherit from BaseForm in all forms instead of from DbModelForm. This change doesn't include changes to templates and usage of as_table everywhere (work in progress).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
   205
class EditForm(helper.forms.BaseForm):
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   206
  """Django form displayed when Developer edits a User.
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   207
  
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   208
  This form is manually specified, instead of using
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   209
    model = soc.models.user.User
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   210
  in the Meta class, because the form behavior is unusual and normally
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   211
  required Properties of the User model need to sometimes be omitted.
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   212
  """
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   213
  account = forms.EmailField(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   214
      label=soc.models.user.User.account.verbose_name,
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   215
      help_text=soc.models.user.User.account.help_text)
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   216
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   217
  link_name = forms.CharField(
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   218
      label=soc.models.user.User.link_name.verbose_name,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   219
      help_text=soc.models.user.User.link_name.help_text)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   220
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   221
  nick_name = forms.CharField(
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   222
      label=soc.models.user.User.nick_name.verbose_name)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   223
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   224
  is_developer = forms.BooleanField(required=False,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   225
      label=soc.models.user.User.is_developer.verbose_name,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   226
      help_text=soc.models.user.User.is_developer.help_text)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   227
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 179
diff changeset
   228
  key_name = forms.CharField(widget=forms.HiddenInput)
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 179
diff changeset
   229
  
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   230
  class Meta:
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   231
    model = None
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   232
 
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   233
  def clean_link_name(self):
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   234
    link_name = self.cleaned_data.get('link_name')
249
325fb70c61a9 Replace common module with validate module. Move validation function from feed module to validate module and remove feed.py. Correct any usage of validation functions from common and feed modules into validate module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 245
diff changeset
   235
    if not validate.isLinkNameFormatValid(link_name):
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   236
      raise forms.ValidationError("This link name is in wrong format.")
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   237
305
972d28056d9d Minor style and import fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 303
diff changeset
   238
    key_name = self.data.get('key_name')
309
7190b224c701 Made the existing code use the new soc.logic.models
Sverre Rabbelier <srabbelier@gmail.com>
parents: 305
diff changeset
   239
    user = models.user.logic.getFromKeyName(key_name)
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 445
diff changeset
   240
    
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   241
    linkname_user_exist = models.user.logic.getForFields(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   242
        {'link_name': link_name}, unique=True)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   243
        
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 445
diff changeset
   244
    if (user and user.link_name != link_name) and linkname_user_exist:
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   245
      raise forms.ValidationError("This link name is already in use.")
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   246
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   247
    return link_name
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   248
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   249
  def clean_account(self):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   250
    form_account = users.User(email=self.cleaned_data.get('account'))
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   251
    if not accounts.isAccountAvailable(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   252
        form_account, existing_key_name=self.data.get('key_name')):
262
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 260
diff changeset
   253
      raise forms.ValidationError("This account is already in use.")
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   254
    if models.user.logic.isFormerAccount(form_account):
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 445
diff changeset
   255
      raise forms.ValidationError("This account is invalid. "
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   256
          "It exists as a former account.")
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   257
    return form_account
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 179
diff changeset
   258
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   259
445
31927f21970d Major site restructuring, also deleting of many unused or obsolete documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 435
diff changeset
   260
DEF_SITE_USER_PROFILE_EDIT_TMPL = 'soc/user/edit.html'
179
4882d6c5630d Add custom 404 error message with link to "Create a New User" which is displayed when linkname in User Profile Developer view URL (/site/user/profile/<linkname>) doesn't exist. Add default value (None) to new_suffix parameter of template_helpers replaceSuffix and redirectToChangedSuffix functions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 171
diff changeset
   261
DEF_CREATE_NEW_USER_MSG = ' You can create a new user by visiting' \
4882d6c5630d Add custom 404 error message with link to "Create a New User" which is displayed when linkname in User Profile Developer view URL (/site/user/profile/<linkname>) doesn't exist. Add default value (None) to new_suffix parameter of template_helpers replaceSuffix and redirectToChangedSuffix functions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 171
diff changeset
   262
                          ' <a href="/site/user/profile">Create ' \
4882d6c5630d Add custom 404 error message with link to "Create a New User" which is displayed when linkname in User Profile Developer view URL (/site/user/profile/<linkname>) doesn't exist. Add default value (None) to new_suffix parameter of template_helpers replaceSuffix and redirectToChangedSuffix functions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 171
diff changeset
   263
                          'a New User</a> page.'
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   264
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
   265
@decorators.view
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   266
def edit(request, page_name=None, link_name=None,
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
   267
         template=DEF_SITE_USER_PROFILE_EDIT_TMPL):
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   268
  """View for a Developer to modify the properties of a User Model entity.
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   269
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   270
  Args:
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   271
    request: the standard django request object
358
843d83b87282 Add page=None parameter to all views. Information from page argument (which is soc.logic.site.page.Page object) is going to be used later in views code and for some context values. Fix some indentions in __doc__ strings. Add proper __doc__ string for all() function in sponsor/list.py module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 355
diff changeset
   272
    page: a soc.logic.site.page.Page object which is abstraction that combines 
843d83b87282 Add page=None parameter to all views. Information from page argument (which is soc.logic.site.page.Page object) is going to be used later in views code and for some context values. Fix some indentions in __doc__ strings. Add proper __doc__ string for all() function in sponsor/list.py module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 355
diff changeset
   273
      a Django view with sidebar menu info
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   274
    link_name: the User's site-unique "link_name" extracted from the URL
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   275
    template: the "sibling" template (or a search list of such templates)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   276
      from which to construct the public.html template name (or names)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   277
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   278
  Returns:
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   279
    A subclass of django.http.HttpResponse which either contains the form to
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   280
    be filled out, or a redirect to the correct view in the interface.
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   281
  """
294
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   282
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   283
  try:
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   284
    access.checkIsDeveloper(request)
303
4f1bb54ddae5 Moved soc/logic/helper/access to soc/views/helper/access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
   285
  except  soc.views.out_of_band.AccessViolationResponse, alt_response:
294
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   286
    return alt_response.response()
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   287
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   288
  # create default template context for use with any templates
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   289
  context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   290
  context['page_name'] = page_name
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   291
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   292
  user = None  # assume that no User entity will be found
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   293
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   294
  # try to fetch User entity corresponding to link_name if one exists
331
24edc648ad3f Add deleted in r732 (don't know why) functionality that displays custom Error page when linkname in Developer User Profile url doesn't exist (/site/user/profile/<not_existing_linkname>).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 325
diff changeset
   295
  try:
24edc648ad3f Add deleted in r732 (don't know why) functionality that displays custom Error page when linkname in Developer User Profile url doesn't exist (/site/user/profile/<not_existing_linkname>).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 325
diff changeset
   296
    if link_name:
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   297
      user = accounts.getUserFromLinkNameOr404(link_name)
331
24edc648ad3f Add deleted in r732 (don't know why) functionality that displays custom Error page when linkname in Developer User Profile url doesn't exist (/site/user/profile/<not_existing_linkname>).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 325
diff changeset
   298
  except out_of_band.ErrorResponse, error:
24edc648ad3f Add deleted in r732 (don't know why) functionality that displays custom Error page when linkname in Developer User Profile url doesn't exist (/site/user/profile/<not_existing_linkname>).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 325
diff changeset
   299
    # show custom 404 page when link name doesn't exist in Datastore
24edc648ad3f Add deleted in r732 (don't know why) functionality that displays custom Error page when linkname in Developer User Profile url doesn't exist (/site/user/profile/<not_existing_linkname>).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 325
diff changeset
   300
    error.message = error.message + DEF_CREATE_NEW_USER_MSG
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   301
    return simple.errorResponse(request, page_name, error, template, context)
331
24edc648ad3f Add deleted in r732 (don't know why) functionality that displays custom Error page when linkname in Developer User Profile url doesn't exist (/site/user/profile/<not_existing_linkname>).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 325
diff changeset
   302
171
b62f1cf5e878 Bug fixes to revision r596. User Account field in Developer User Profile view needs to be an email not a login name. Header title of User Profile Developer view, when form validation failes is now showing correct value ("Modify existing ..." instead of "Create new user ...").
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 170
diff changeset
   303
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   304
  if request.method == 'POST':
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   305
    form = EditForm(request.POST)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   306
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   307
    if form.is_valid():
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 179
diff changeset
   308
      key_name = form.cleaned_data.get('key_name')
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   309
      new_link_name = form.cleaned_data.get('link_name')
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   310
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   311
      properties = {}
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   312
      properties['account'] = form.cleaned_data.get('account')
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   313
      properties['link_name']  = new_link_name
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   314
      properties['nick_name']  = form.cleaned_data.get('nick_name')
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   315
      properties['is_developer'] = form.cleaned_data.get('is_developer')
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   316
      
309
7190b224c701 Made the existing code use the new soc.logic.models
Sverre Rabbelier <srabbelier@gmail.com>
parents: 305
diff changeset
   317
      user = models.user.logic.updateOrCreateFromKeyName(properties, key_name)
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   318
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 179
diff changeset
   319
      if not user:
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 179
diff changeset
   320
        return http.HttpResponseRedirect('/')
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 179
diff changeset
   321
        
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   322
      # redirect to new /site/user/profile/new_link_name?s=0
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   323
      # (causes 'Profile saved' message to be displayed)
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   324
      return helper.responses.redirectToChangedSuffix(
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   325
          request, link_name, new_link_name,
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   326
          params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   327
  else: # method == 'GET':
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   328
    # try to fetch User entity corresponding to link name if one exists
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   329
    if link_name:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   330
      if user:
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   331
        # is 'Profile saved' parameter present, but referrer was not ourself?
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   332
        # (e.g. someone bookmarked the GET that followed the POST submit) 
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   333
        if (request.GET.get(profile.SUBMIT_MSG_PARAM_NAME)
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   334
            and (not helper.requests.isReferrerSelf(request,
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   335
                                                    suffix=link_name))):
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   336
          # redirect to aggressively remove 'Profile saved' query parameter
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   337
          return http.HttpResponseRedirect(request.path)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   338
    
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   339
        # referrer was us, so select which submit message to display
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   340
        # (may display no message if ?s=0 parameter is not present)
290
2a92b866ed40 Create a submit_buttons block in group edit profile template so that submit buttons can be customized. Replace submit_message usage with new notice block (still work in progress in Lookup User views). Add customized edit profile template for Sponsor with delete button (not used yet, upcoming commit). Add delete Sponsor request handler.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 274
diff changeset
   341
        context['notice'] = (
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 268
diff changeset
   342
            helper.requests.getSingleIndexedParamValue(
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   343
                request, profile.SUBMIT_MSG_PARAM_NAME,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   344
                values=profile.SUBMIT_MESSAGES))
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   345
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   346
        # populate form with the existing User entity
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   347
        form = EditForm(initial={'key_name': user.key().name(),
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   348
            'account': user.account.email(), 'link_name': user.link_name,
215
d020c95b17b1 Clean up too long lines in profile.py. Add ReadOnlyInput widget to custom_widgets module, it can be used to display read-only form fields. Display read-only "Id" field in LookUp view if user has been found and in User Edit Developer view.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 210
diff changeset
   349
            'nick_name': user.nick_name, 'is_developer': user.is_developer})
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   350
      else:
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   351
        if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   352
          # redirect to aggressively remove 'Profile saved' query parameter
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   353
          return http.HttpResponseRedirect(request.path)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   354
          
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   355
        context['lookup_error'] = ugettext_lazy(
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   356
            'User with that link name not found.')
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 294
diff changeset
   357
        form = EditForm(initial={'link_name': link_name})
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   358
    else:  # no link name specified in the URL
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   359
      if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME):
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   360
        # redirect to aggressively remove 'Profile saved' query parameter
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   361
        return http.HttpResponseRedirect(request.path)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   362
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   363
      # no link name specified, so start with an empty form
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   364
      form = EditForm()
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   365
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   366
  context.update({'form': form,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   367
                  'existing_user': user})
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 141
diff changeset
   368
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   369
  return helper.responses.respond(request, template, context)
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   370
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   371
408
7cd6bdfbf95c Inherit from BaseForm in all forms instead of from DbModelForm. This change doesn't include changes to templates and usage of as_table everywhere (work in progress).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
   372
class CreateForm(helper.forms.BaseForm):
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   373
  """Django form displayed when Developer creates a User.
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   374
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   375
  This form is manually specified, instead of using
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   376
    model = soc.models.user.User
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   377
  in the Meta class, because the form behavior is unusual and normally
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   378
  required Properties of the User model need to sometimes be omitted.
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   379
  """
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   380
  account = forms.EmailField(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   381
      label=soc.models.user.User.account.verbose_name,
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   382
      help_text=soc.models.user.User.account.help_text)
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   383
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   384
  link_name = forms.CharField(
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   385
      label=soc.models.user.User.link_name.verbose_name,
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   386
      help_text=soc.models.user.User.link_name.help_text)
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   387
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   388
  nick_name = forms.CharField(
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   389
      label=soc.models.user.User.nick_name.verbose_name)
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   390
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   391
  is_developer = forms.BooleanField(required=False,
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   392
      label=soc.models.user.User.is_developer.verbose_name,
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   393
      help_text=soc.models.user.User.is_developer.help_text)
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   394
  
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   395
  class Meta:
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   396
    model = None
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   397
  
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   398
  def clean_link_name(self):
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   399
    link_name = self.cleaned_data.get('link_name')
260
8b393bef717a Fix typo in recent validate.py changes (r682).
Todd Larsen <tlarsen@google.com>
parents: 249
diff changeset
   400
    if not validate.isLinkNameFormatValid(link_name):
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   401
      raise forms.ValidationError("This link name is in wrong format.")
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   402
    else:
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   403
      if models.user.logic.getForFields({'link_name': link_name},
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   404
                                        unique=True):
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   405
        raise forms.ValidationError("This link name is already in use.")
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   406
    return link_name
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   407
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   408
  def clean_account(self):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   409
    new_email = self.cleaned_data.get('account')
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   410
    form_account = users.User(email=new_email)
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   411
    if models.user.logic.getForFields({'account': form_account}, unique=True):
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 374
diff changeset
   412
      raise forms.ValidationError("This account is already in use.")
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   413
    if models.user.logic.isFormerAccount(form_account):
448
075360be6743 Fix not working former_ids. Add support for "Invalid accounts". Now when id from former_ids tries to create a profile "This account is invalid." error message is displayed. Compare emails in lower cases to prevent changing User email to the same email with different character casing (needs some more testing).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 445
diff changeset
   414
      raise forms.ValidationError("This account is invalid. "
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   415
          "It exists as a former account.")
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   416
    return form_account
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   417
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   418
445
31927f21970d Major site restructuring, also deleting of many unused or obsolete documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 435
diff changeset
   419
DEF_SITE_CREATE_USER_PROFILE_TMPL = 'soc/user/edit.html'
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   420
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
   421
@decorators.view
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   422
def create(request, page_name=None, template=DEF_SITE_CREATE_USER_PROFILE_TMPL):
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 215
diff changeset
   423
  """View for a Developer to create a new User Model entity.
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   424
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   425
  Args:
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   426
    request: the standard django request object
358
843d83b87282 Add page=None parameter to all views. Information from page argument (which is soc.logic.site.page.Page object) is going to be used later in views code and for some context values. Fix some indentions in __doc__ strings. Add proper __doc__ string for all() function in sponsor/list.py module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 355
diff changeset
   427
    page: a soc.logic.site.page.Page object which is abstraction that combines 
843d83b87282 Add page=None parameter to all views. Information from page argument (which is soc.logic.site.page.Page object) is going to be used later in views code and for some context values. Fix some indentions in __doc__ strings. Add proper __doc__ string for all() function in sponsor/list.py module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 355
diff changeset
   428
      a Django view with sidebar menu info
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   429
    template: the "sibling" template (or a search list of such templates)
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   430
      from which to construct the public.html template name (or names)
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   431
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   432
  Returns:
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   433
    A subclass of django.http.HttpResponse which either contains the form to
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   434
    be filled out, or a redirect to the correct view in the interface.
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   435
  """
294
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   436
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   437
  try:
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   438
    access.checkIsDeveloper(request)
303
4f1bb54ddae5 Moved soc/logic/helper/access to soc/views/helper/access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
   439
  except  soc.views.out_of_band.AccessViolationResponse, alt_response:
294
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   440
    return alt_response.response()
1fdaab4a6ef2 Refactor existing code to use the new access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   441
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   442
  # create default template context for use with any templates
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   443
  context = helper.responses.getUniversalContext(request)
482
839740b061ad Factor out direct use of the page object
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   444
  context['page_name'] = page_name
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   445
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   446
  if request.method == 'POST':
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   447
    form = CreateForm(request.POST)
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   448
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   449
    if form.is_valid():
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   450
      form_account = form.cleaned_data.get('account')
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   451
      link_name = form.cleaned_data.get('link_name')
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   452
305
972d28056d9d Minor style and import fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 303
diff changeset
   453
      properties = {
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   454
        'account': form_account,
317
87215136c268 Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
   455
        'link_name': link_name,
87215136c268 Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
   456
        'nick_name': form.cleaned_data.get('nick_name'),
87215136c268 Remove spaces between dict key name and colon. Fix some typos based on comments http://code.google.com/p/soc/source/detail?r=739.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 316
diff changeset
   457
        'is_developer': form.cleaned_data.get('is_developer'),
305
972d28056d9d Minor style and import fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 303
diff changeset
   458
      }
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   459
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   460
      key_fields = {'email': form_account.email()}
435
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 408
diff changeset
   461
      user = models.user.logic.updateOrCreateFromFields(properties,
829fe8302a8b Refactor out the abundance of dictionary (un)packing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 408
diff changeset
   462
                                                        key_fields)
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   463
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   464
      if not user:
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   465
        return http.HttpResponseRedirect('/')
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   466
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   467
      # redirect to new /site/user/profile/new_link_name?s=0
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   468
      # (causes 'Profile saved' message to be displayed)
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   469
      return helper.responses.redirectToChangedSuffix(
445
31927f21970d Major site restructuring, also deleting of many unused or obsolete documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 435
diff changeset
   470
          request, 'create', 'edit/' + link_name,
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   471
          params=profile.SUBMIT_PROFILE_SAVED_PARAMS)
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   472
  else: # method == 'GET':
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   473
    # no link name specified, so start with an empty form
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   474
    form = CreateForm()
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   475
210
d05444cf4641 Replaced context.update call to simple value assignment in site.user.profile.create view function.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 205
diff changeset
   476
  context['form'] = form
205
4a86df751222 Fixed not working "Create New User" view which was caused by adding key_name into User Edit Developer view. This commit divides edit() view function in soc.views.site.user.profile into edit() and create() request handlers. New create() request handler is used only in "Create New User" view and has specific form validation functions and uses updateOrCreateUserFromId() instead of updateUserForKeyName().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
   477
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
   478
  return helper.responses.respond(request, template, context)