app/soc/views/user/profile.py
author Todd Larsen <tlarsen@google.com>
Sat, 15 Nov 2008 03:12:33 +0000
changeset 481 94834a1e6c01
parent 473 d01d5e3b0b5c
child 482 839740b061ad
permissions -rw-r--r--
Attempt to rename User.id to User.account, in preparation for making User be derived from Linkable, which will have a property named 'id'. Patch by: Todd Larsen
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
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
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
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
    28
from django import http
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
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
    31
from soc.logic import accounts
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    32
from soc.logic import models
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
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    34
from soc.logic import validate
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    35
from soc.views import helper
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    36
from soc.views import simple
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
    37
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
    38
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
    39
import soc.logic
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 311
diff changeset
    40
import soc.models.user
274
56e1c1721299 Move helpers/forms_helpers.py to helper/forms.py.
Todd Larsen <tlarsen@google.com>
parents: 272
diff changeset
    41
import soc.views.helper.forms
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
    42
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
    43
import soc.views.helper.responses
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
    44
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
    45
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: 391
diff changeset
    46
class UserForm(helper.forms.BaseForm):
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
    47
  """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
    48
  """
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
    49
  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
    50
    """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
    51
    """
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
    52
    #: 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
    53
    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
    54
    
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
    55
    #: list of model fields which will *not* be gathered by the form
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
    56
    exclude = ['account', 'former_accounts', 'is_developer']
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    57
  
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    58
  def clean_link_name(self):
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    59
    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
    60
    if not validate.isLinkNameFormatValid(link_name):
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    61
      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
    62
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
    63
    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: 473
diff changeset
    64
                                          unique=True)
473
d01d5e3b0b5c Fix broken User link name (it was possible to change link name to one that was already used, validation was not working correctly).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 448
diff changeset
    65
    if user:
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
    66
      raise forms.ValidationError("This link name is already in use.")
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
    67
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    68
    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
    69
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
    70
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 445
diff changeset
    71
DEF_USER_PROFILE_EDIT_TMPL = 'soc/user/edit_self.html'
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: 446
diff changeset
    72
DEF_USER_ACCOUNT_INVALID_MSG = 'This account is invalid.'
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
    73
170
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 = 's'
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
SUBMIT_MESSAGES = (
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    77
  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
    78
)
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    79
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    80
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
    81
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    82
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
    83
  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
    84
}
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    85
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
    86
@decorators.view
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
    87
def edit(request, page=None, link_name=None, 
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
    88
         template=DEF_USER_PROFILE_EDIT_TMPL):
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
  """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
    90
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
    91
  Args:
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    92
    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
    93
    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
    94
      a Django view with sidebar menu info
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
    95
    link_name: the User's site-unique "link_name" extracted from the URL
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
    96
    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
    97
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
    98
  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
    99
    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
   100
    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
   101
  """
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   102
  account = users.get_current_user()
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: 446
diff changeset
   103
  
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
   104
  # 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
   105
  context = helper.responses.getUniversalContext(request)
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
   106
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   107
  if (not account) and (not link_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
   108
    # not logged in, and no link name, so request that the user sign in 
361
465e4df617de Make page argument required for errorResponse and requestLogin functions in simple.py and update affected files. Some keyword argument changes to in user/profile.py. All the changes are based on comments to r817.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 358
diff changeset
   109
    return simple.requestLogin(request, page, template, context,
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
   110
        # TODO(tlarsen): /user/profile could be a link to a help page instead
391
849aa913e9c8 Address comments to r844 and r845.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
   111
        login_message_fmt=ugettext_lazy(
849aa913e9c8 Address comments to r844 and r845.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
   112
            'To create a new <a href="/user/profile">User Profile</a>'
849aa913e9c8 Address comments to r844 and r845.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
   113
            ' or modify an existing one, you must first'
849aa913e9c8 Address comments to r844 and r845.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 389
diff changeset
   114
            ' <a href="%(sign_in)s">sign in</a>.'))
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
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   116
  if (not account) and link_name:
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   117
    # not logged in, so show read-only public profile for link_name user
361
465e4df617de Make page argument required for errorResponse and requestLogin functions in simple.py and update affected files. Some keyword argument changes to in user/profile.py. All the changes are based on comments to r817.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 358
diff changeset
   118
    return simple.public(request, page=page, template=template, 
465e4df617de Make page argument required for errorResponse and requestLogin functions in simple.py and update affected files. Some keyword argument changes to in user/profile.py. All the changes are based on comments to r817.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 358
diff changeset
   119
                         link_name=link_name, context=context)
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
   120
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   121
  link_name_user = None
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   122
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   123
  # try to fetch User entity corresponding to link_name if one exists
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
   124
  try:
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 384
diff changeset
   125
    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: 473
diff changeset
   126
      link_name_user = accounts.getUserFromLinkNameOr404(link_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
   127
  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
   128
    # show custom 404 page when link name doesn't exist in Datastore
361
465e4df617de Make page argument required for errorResponse and requestLogin functions in simple.py and update affected files. Some keyword argument changes to in user/profile.py. All the changes are based on comments to r817.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 358
diff changeset
   129
    return simple.errorResponse(request, page, error, template, context)
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   130
  
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   131
  # link_name_user will be None here if link name was already None...
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   132
  if link_name_user and (link_name_user.account != account):
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   133
    # link_name_user exists but is not the currently logged in Google Account,
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
    # so show public view for that (other) User entity
361
465e4df617de Make page argument required for errorResponse and requestLogin functions in simple.py and update affected files. Some keyword argument changes to in user/profile.py. All the changes are based on comments to r817.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 358
diff changeset
   135
    return simple.public(request, page=page, template=template, 
465e4df617de Make page argument required for errorResponse and requestLogin functions in simple.py and update affected files. Some keyword argument changes to in user/profile.py. All the changes are based on comments to r817.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 358
diff changeset
   136
                         link_name=link_name, context=context)
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   137
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   138
  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
   139
    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
   140
98
b2b823466a8b User Profile view (without controller yet).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 85
diff changeset
   141
    if form.is_valid():
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   142
      new_link_name = form.cleaned_data.get('link_name')
305
972d28056d9d Minor style and import fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
   143
      properties = {
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
   144
        'link_name': new_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
   145
        'nick_name': form.cleaned_data.get("nick_name"),
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   146
        'account': account,
305
972d28056d9d Minor style and import fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 299
diff changeset
   147
      }
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
   148
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   149
      # check if user account is not in former_accounts
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: 446
diff changeset
   150
      # if it is show error message that 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: 473
diff changeset
   151
      if models.user.logic.isFormerAccount(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: 446
diff changeset
   152
        msg = DEF_USER_ACCOUNT_INVALID_MSG
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: 446
diff changeset
   153
        error = out_of_band.ErrorResponse(msg)
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: 446
diff changeset
   154
        return simple.errorResponse(request, page, error, template, context)
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: 446
diff changeset
   155
      
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   156
      user = models.user.logic.updateOrCreateFromAccount(properties, 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: 446
diff changeset
   157
      
445
31927f21970d Major site restructuring, also deleting of many unused or obsolete documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   158
      # redirect to /user/profile?s=0
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   159
      # (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
   160
      return helper.responses.redirectToChangedSuffix(
445
31927f21970d Major site restructuring, also deleting of many unused or obsolete documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 438
diff changeset
   161
          request, None, 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
   162
  else: # request.method == 'GET'
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 298
diff changeset
   163
    # try to fetch User entity corresponding to Google Account if one exists
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 473
diff changeset
   164
    user = models.user.logic.getForFields({'account': account}, unique=True)
131
Todd Larsen <tlarsen@google.com>
parents: 124
diff changeset
   165
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
   166
    if user:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   167
      # 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
   168
      # (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
   169
      if (request.GET.get(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: 267
diff changeset
   170
          and (not helper.requests.isReferrerSelf(request,
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   171
                                                  suffix=link_name))):
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   172
        # 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
   173
        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
   174
    
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   175
      # 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
   176
      # (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: 278
diff changeset
   177
      context['notice'] = (
269
0f1acc4c3e1e Move helpers/request.py to helper/requests.py to avoid conflict with common
Todd Larsen <tlarsen@google.com>
parents: 267
diff changeset
   178
          helper.requests.getSingleIndexedParamValue(
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   179
              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
   180
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
   181
      # 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
   182
      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
   183
    else:
170
1fadf6e0348d Add /site/user/profile Developer view for editing arbitrary User entities.
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   184
      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
   185
        # 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
   186
        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
   187
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
   188
      # 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
   189
      form = UserForm()
99
8c38b546a3cf Added public view support (not using controller yet)
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 98
diff changeset
   190
272
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   191
  context['form'] = form
00cea07656c0 Move helpers/response_helpers.py to helper/responses.py.
Todd Larsen <tlarsen@google.com>
parents: 269
diff changeset
   192
  return helper.responses.respond(request, template, context)
278
b0e2d509b5c3 Add create() wrapper views that simply call the existing edit() views, to
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   193
b0e2d509b5c3 Add create() wrapper views that simply call the existing edit() views, to
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   194
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
   195
@decorators.view
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
   196
def create(request, page=None, template=DEF_USER_PROFILE_EDIT_TMPL):
298
c76a366c7ab4 Replace almost all occurences of linkname with link_name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 290
diff changeset
   197
  """create() view is same as edit() view, but with no link_name supplied.
278
b0e2d509b5c3 Add create() wrapper views that simply call the existing edit() views, to
Todd Larsen <tlarsen@google.com>
parents: 274
diff changeset
   198
  """
383
8a8820544caa Fix missing keyword argument in soc.views.user.profile.create() function when calling edit().
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 365
diff changeset
   199
  return edit(request, page=page, link_name=None, template=template)