app/soc/views/user/profile.py
author Todd Larsen <tlarsen@google.com>
Wed, 01 Oct 2008 22:02:50 +0000
changeset 256 101c586ff5c8
parent 249 325fb70c61a9
child 267 0c008a43443b
permissions -rw-r--r--
Remove excluded field inheritance_line, since the User model does not inherit from PolyModel (at this time).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
118
d2e61a490969 Clean up User Model and User profile edit view now that soc.logic.site.id_user
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    17
"""Views for editing and examining User profiles.
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
__authors__ = [
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
  ]
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
from google.appengine.api import users
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
from django import http
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
from django import shortcuts
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
from django import newforms as forms
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    29
from django.utils.translation import ugettext_lazy
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
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
    31
from soc.logic import validate
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    32
from soc.logic import out_of_band
118
d2e61a490969 Clean up User Model and User profile edit view now that soc.logic.site.id_user
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    33
from soc.logic.site import id_user
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    34
from soc.views import simple
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
from soc.views.helpers import forms_helpers
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents: 184
diff changeset
    36
from soc.views.helpers import request_helpers
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
from soc.views.helpers import response_helpers
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    39
import soc.models.user
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    40
118
d2e61a490969 Clean up User Model and User profile edit view now that soc.logic.site.id_user
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    41
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    42
class UserForm(forms_helpers.DbModelForm):
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    43
  """Django form displayed when creating or editing a User.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    44
  """
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    45
  class Meta:
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
    """Inner Meta class that defines some behavior for the form.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
    """
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
    #: db.Model subclass for which the form will gather information
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    49
    model = soc.models.user.User
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
    50
    
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
    #: list of model fields which will *not* be gathered by the form
256
101c586ff5c8 Remove excluded field inheritance_line, since the User model does not inherit
Todd Larsen <tlarsen@google.com>
parents: 249
diff changeset
    52
    exclude = ['id', 'former_ids', 'is_developer']
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    53
  
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    54
  def clean_link_name(self):
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    55
    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
    56
    if not validate.isLinkNameFormatValid(link_name):
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    57
      raise forms.ValidationError("This link name is in wrong format.")
136
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
    58
    elif not id_user.isLinkNameAvailableForId(link_name):
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    59
      raise forms.ValidationError("This link name is already in use.")
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    60
    return link_name
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    63
DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/profile/edit.html'
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    64
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    65
SUBMIT_MSG_PARAM_NAME = 's'
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    66
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    67
SUBMIT_MESSAGES = (
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    68
  ugettext_lazy('Profile saved.'),
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    69
)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    70
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    71
SUBMIT_MSG_PROFILE_SAVED = 0
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    72
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    73
SUBMIT_PROFILE_SAVED_PARAMS = {
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    74
  SUBMIT_MSG_PARAM_NAME: SUBMIT_MSG_PROFILE_SAVED,
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    75
}
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    76
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    77
def edit(request, linkname=None, template=DEF_USER_PROFILE_EDIT_TMPL):
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    78
  """View for a User to modify the properties of a User Model entity.
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    79
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    80
  Args:
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    81
    request: the standard django request object
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    82
    linkname: the User's site-unique "linkname" extracted from the URL
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    83
    template: the template path to use for rendering the template
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    84
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    85
  Returns:
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    86
    A subclass of django.http.HttpResponse which either contains the form to
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    87
    be filled out, or a redirect to the correct view in the interface.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    88
  """
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    89
  id = users.get_current_user()
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    90
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    91
  # create default template context for use with any templates
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    92
  context = response_helpers.getUniversalContext(request)
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    93
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    94
  if (not id) and (not linkname):
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    95
    # not logged in, and no link name, so request that the user sign in 
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    96
    return simple.requestLogin(request, template, context,
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    97
        # TODO(tlarsen): /user/profile could be a link to a help page instead
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    98
        login_message_fmt='To create a new'
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
    99
                          ' <a href="/user/profile">User Profile</a>'
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   100
                          ' or modify an existing one, you must first'
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   101
                          ' <a href="%(sign_in)s">sign in</a>.')
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   102
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   103
  if (not id) and linkname:
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   104
    # not logged in, so show read-only public profile for linkname user
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   105
    return simple.public(request, template, linkname, context)
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   106
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   107
  # try to fetch User entity corresponding to linkname if one exists    
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   108
  try:
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   109
    linkname_user = id_user.getUserIfLinkName(linkname)
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   110
  except out_of_band.ErrorResponse, error:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   111
    # show custom 404 page when link name doesn't exist in Datastore
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   112
    return simple.errorResponse(request, error, template, context)
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   113
  
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   114
  # linkname_user will be None here if link name was already None...
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   115
  if linkname_user and (linkname_user.id != id):
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   116
    # linkname_user exists but is not the currently logged in Google Account,
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   117
    # so show public view for that (other) User entity
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   118
    return simple.public(request, template, linkname, context)
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   119
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   120
  if request.method == 'POST':
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   121
    form = UserForm(request.POST)
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   122
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   123
    if form.is_valid():
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   124
      new_linkname = form.cleaned_data.get('link_name')
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   125
      nickname = form.cleaned_data.get("nick_name")
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   126
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
   127
      user = id_user.updateOrCreateUserFromId(
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   128
          id, link_name=new_linkname, nick_name=nickname)
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
   129
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: 170
diff changeset
   130
      # redirect to new /user/profile/new_linkname?s=0
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   131
      # (causes 'Profile saved' message to be displayed)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   132
      return response_helpers.redirectToChangedSuffix(
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   133
          request, linkname, new_linkname, params=SUBMIT_PROFILE_SAVED_PARAMS)
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   134
  else: # request.method == 'GET'
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
   135
    # try to fetch User entity corresponding to Google Account if one exists    
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
   136
    user = id_user.getUserFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
   137
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   138
    if user:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   139
      # 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: 137
diff changeset
   140
      # (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: 137
diff changeset
   141
      if (request.GET.get(SUBMIT_MSG_PARAM_NAME)
191
80f08751f1e5 These changes should have been in r620, but somehow I did not save them, or
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   142
          and (not request_helpers.isReferrerSelf(request,
80f08751f1e5 These changes should have been in r620, but somehow I did not save them, or
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   143
                                                  suffix=linkname))):
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   144
        # 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: 137
diff changeset
   145
        return http.HttpResponseRedirect(request.path)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   146
    
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   147
      # 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: 137
diff changeset
   148
      # (may display no message if ?s=0 parameter is not present)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   149
      context['submit_message'] = (
191
80f08751f1e5 These changes should have been in r620, but somehow I did not save them, or
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   150
          request_helpers.getSingleIndexedParamValue(
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   151
              request, SUBMIT_MSG_PARAM_NAME, values=SUBMIT_MESSAGES))
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   152
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   153
      # populate form with the existing User entity
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   154
      form = UserForm(instance=user)
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   155
    else:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   156
      if request.GET.get(SUBMIT_MSG_PARAM_NAME):
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   157
        # 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: 137
diff changeset
   158
        return http.HttpResponseRedirect(request.path)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   159
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   160
      # no User entity exists for this Google Account, so show a blank form
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   161
      form = UserForm()
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
   162
124
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   163
  context.update({'form': form})
051afb721c22 Refactor soc/views/user/profile.py to make use of several of the new views
Todd Larsen <tlarsen@google.com>
parents: 118
diff changeset
   164
  return response_helpers.respond(request, template, context)