app/soc/logic/models/user.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 14 Jul 2009 16:46:42 +0200
changeset 2643 222be0eb4aab
parent 2361 40b0c25a5793
permissions -rw-r--r--
Added updateProjectsForGradingRecords to StudentProject Logic. This method takes a list of record_entities, processes them and batch puts the update StudentProjects.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""User (Model) query functions.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 316
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    25
from google.appengine.api import users
865
4f96580e1871 Fix missing import identified by pylint.
Todd Larsen <tlarsen@google.com>
parents: 862
diff changeset
    26
from google.appengine.ext import db
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    27
988
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
    28
from soc.cache import sidebar
1521
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    29
from soc.logic import accounts
640
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 533
diff changeset
    30
from soc.logic.helper import notifications
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
from soc.logic.models import base
862
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
    32
from soc.logic.models.site import logic as site_logic
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 308
diff changeset
    33
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
import soc.models.user
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
class Logic(base.Logic):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 496
diff changeset
    38
  """Logic methods for the User model.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
  """
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
  def __init__(self):
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
    """Defines the name, key_name and model for this entity.
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
    """
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 640
diff changeset
    44
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 640
diff changeset
    45
    super(Logic, self).__init__(soc.models.user.User,
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    46
                        skip_properties=['former_accounts'])
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    48
  def isFormerAccount(self, account):
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    49
    """Returns true if account is a former account of some User.
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    50
    """
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 640
diff changeset
    51
533
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
    52
    # TODO(pawel.solyga): replace 1000 with solution that works for any
ba3309b2fd30 Move LINK_ID and SCOPE_PATH regular expressions to soc/models/linkable.py.
Todd Larsen <tlarsen@google.com>
parents: 512
diff changeset
    53
    #   number of queries
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    54
    users_with_former_accounts = soc.models.user.User.gql(
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    55
        'WHERE former_accounts != :1', None).fetch(1000)
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 640
diff changeset
    56
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
    57
    for former_account_user in users_with_former_accounts: 
1242
c5ca70606850 Handle GAE account normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1193
diff changeset
    58
      for former_account in former_account_user.former_accounts:
c5ca70606850 Handle GAE account normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1193
diff changeset
    59
        if str(account) == str(former_account):
c5ca70606850 Handle GAE account normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1193
diff changeset
    60
          return True
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 640
diff changeset
    61
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: 434
diff changeset
    62
    return False
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: 434
diff changeset
    63
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    64
  def getForCurrentAccount(self):
744
cd9bf163473c Add missing dots, fix imports sorting and too long lines in different modules. Add TODO in soc.views.models.host module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 722
diff changeset
    65
    """Retrieves the user entity for the currently logged in account.
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    66
1193
8d3a7e8d88bd Fixed a typo in getForCurrentAccount comment.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
    67
    If there is no user logged in, or they have no valid associated User
8d3a7e8d88bd Fixed a typo in getForCurrentAccount comment.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
    68
    entity, None is returned.
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    69
    """
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    70
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1534
diff changeset
    71
    account = accounts.getCurrentAccount()
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    72
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    73
    if not account:
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    74
      return None
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    75
1521
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    76
    return self.getForAccount(account)
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    77
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    78
  def getForCurrentUserId(self):
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    79
    """Retrieves the user entity for the currently logged in user id.
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    80
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    81
    If there is no user logged in, or they have no valid associated User
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    82
    entity, None is returned.
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    83
    """
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    84
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    85
    user_id = accounts.getCurrentUserId()
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    86
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    87
    if not user_id:
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    88
      return None
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    89
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    90
    return self.getForUserId(user_id)
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
    91
1521
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    92
  def getForAccount(self, account):
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    93
    """Retrieves the user entity for the specified account.
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    94
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    95
    If there is no user logged in, or they have no valid associated User
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    96
    entity, None is returned.
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    97
    """
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    98
1521
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    99
    if not account:
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   100
      raise base.InvalidArgumentError
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   101
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   102
    account = accounts.normalizeAccount(account)
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   103
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   104
    fields = {
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   105
        'account': account,
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   106
        'status':'valid',
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   107
        }
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   108
bb086732bf7f Factor out the getForAccount logic in user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
   109
    return self.getForFields(filter=fields, unique=True)
722
a59eaa177562 Added getForCurrentAccount to user.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
   110
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   111
  def getForUserId(self, user_id):
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   112
    """Retrieves the user entity for the specified user id.
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   113
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   114
    If there is no user logged in, or they have no valid associated User
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   115
    entity, None is returned.
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   116
    """
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   117
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   118
    if not user_id:
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   119
      raise base.InvalidArgumentError
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   120
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   121
    fields = {
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   122
        'user_id': user_id,
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   123
        'status':'valid',
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   124
        }
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   125
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   126
    return self.getForFields(filter=fields, unique=True)
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1600
diff changeset
   127
1523
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   128
  def isDeveloper(self, account=None, user=None):
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   129
    """Returns true iff the specified user is a Developer.
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   130
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   131
    Args:
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   132
      account: if not supplied, defaults to the current account
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   133
      user: if not specified, defaults to the current user
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   134
    """
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   135
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1534
diff changeset
   136
    current = accounts.getCurrentAccount()
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1534
diff changeset
   137
1523
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   138
    if not account:
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   139
      # default account to the current logged in account
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1534
diff changeset
   140
      account = current
1523
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   141
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   142
    if account and (not user):
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   143
      # default user to the current logged in user
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   144
      user = self.getForAccount(account)
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   145
2361
40b0c25a5793 Some more pylint fixes in different Melange modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2345
diff changeset
   146
    # pylint: disable-msg=E1103
1534
0a8dbeccf0d9 Brown paper bag fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1523
diff changeset
   147
    if user and user.is_developer:
0a8dbeccf0d9 Brown paper bag fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1523
diff changeset
   148
      return True
1523
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   149
1600
0aa3de1b2acc Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1534
diff changeset
   150
    if account and (account == current):
1523
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   151
      return users.is_current_user_admin()
1e6ac8f61a97 Added an isDeveloper method to user logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1522
diff changeset
   152
862
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   153
  def agreesToSiteToS(self, entity):
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   154
    """Returns indication of User's answer to the site-wide Terms of Service.
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   155
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   156
    Args:
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   157
      entity: User entity to check for agreement to site-wide ToS
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   158
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   159
    Returns:
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   160
      True: no site-wide ToS is currently in effect on the site
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   161
      True: site-wide ToS is in effect *and* User agrees to it
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   162
        (User explicitly answered "Yes")
1076
063194eaf87b Added agreed_to_tos_on field in user and role model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 988
diff changeset
   163
      False: site-wide ToS is in effect but User did not agree to it yet
862
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   164
    """
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   165
    if not site_logic.getToS(site_logic.getSingleton()):
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   166
      # no site-wide ToS in effect, so let the User slide for now
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   167
      return True
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   168
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   169
    try:
1076
063194eaf87b Added agreed_to_tos_on field in user and role model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 988
diff changeset
   170
      agreed_on = entity.agreed_to_tos_on
862
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   171
    except db.Error:
1076
063194eaf87b Added agreed_to_tos_on field in user and role model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 988
diff changeset
   172
      # return False indicating that answer is missing
063194eaf87b Added agreed_to_tos_on field in user and role model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 988
diff changeset
   173
      return False
862
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   174
1076
063194eaf87b Added agreed_to_tos_on field in user and role model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 988
diff changeset
   175
    # user has not agreed yet
063194eaf87b Added agreed_to_tos_on field in user and role model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 988
diff changeset
   176
    if not agreed_on:
862
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   177
      return False
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   178
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   179
    return True
fac7cb803aaf Add agreesToSiteToS() logic method to indicate if User accepts site-wide ToS.
Todd Larsen <tlarsen@google.com>
parents: 744
diff changeset
   180
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   181
  def getKeyValuesFromEntity(self, entity):
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1106
diff changeset
   182
    """See base.Logic.getKeyValuesFromEntity.
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   183
    """
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 448
diff changeset
   184
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 505
diff changeset
   185
    return [entity.link_id]
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   186
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 448
diff changeset
   187
  def getSuffixValues(self, entity):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 496
diff changeset
   188
    """See base.Logic.getSuffixValues.
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 448
diff changeset
   189
    """
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 448
diff changeset
   190
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 505
diff changeset
   191
    return [entity.link_id]
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 448
diff changeset
   192
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   193
  def getKeyValuesFromFields(self, fields):
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   194
    """See base.Logic.getKeyValuesFromFields.
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   195
    """
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   196
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 505
diff changeset
   197
    return [fields['link_id']]
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   198
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   199
  def getKeyFieldNames(self):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 496
diff changeset
   200
    """See base.Logic.getKeyFieldNames.
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   201
    """
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   202
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 505
diff changeset
   203
    return ['link_id']
434
a4fc430fefac Added key field definitions to the User logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 432
diff changeset
   204
1522
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   205
  def _createField(self, entity_properties, name):
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   206
    """Normalize the account before storing it.
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   207
    """
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   208
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   209
    value = entity_properties[name]
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   210
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   211
    if (name == 'account'):
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   212
      # normalize all accounts before doing anything with the value
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   213
      value = accounts.normalizeAccount(value)
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   214
      entity_properties[name] = value
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   215
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1479
diff changeset
   216
  def _updateField(self, entity, entity_properties, name):
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   217
    """Special case logic for account.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   218
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   219
    When the account is changed, the former_accounts field should be appended
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 459
diff changeset
   220
    with the old account.
988
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   221
    Also, if either is_developer or agrees_to_tos change, the user's
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   222
    rights have changed, so we need to flush the sidebar.
1161
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   223
    Make sure once the user agreed ToS, the ToS fields can't be changed.
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   224
    """
988
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   225
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1479
diff changeset
   226
    value = entity_properties[name]
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1479
diff changeset
   227
1161
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   228
    # iff the agreed_to_tos is True and we want to set it to False 
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   229
    if (name == 'agreed_to_tos') and (not value) and entity.agreed_to_tos:
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   230
      return False
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   231
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   232
    # iff the agreed_to_tos_on has a value and we want to change it
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   233
    if (name == 'agreed_to_tos_on') and entity.agreed_to_tos_on and (
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   234
        value != entity.agreed_to_tos_on):
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   235
      return False
ad352f3a37d3 This patch prohibits changing of agreed_to_tos and agreed_to_tos_on by the Logic module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   236
988
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   237
    if (name == 'is_developer') and (entity.is_developer != value):
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   238
      sidebar.flush(entity.account)
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   239
1076
063194eaf87b Added agreed_to_tos_on field in user and role model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 988
diff changeset
   240
    if (name == 'agreed_to_tos') and (entity.agreed_to_tos != value):
988
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   241
      sidebar.flush(entity.account)
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   242
1479
bc738f92a4d8 Fix the user editing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   243
    if (name == 'account'):
1522
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   244
      # normalize all accounts before doing anything with the value
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   245
      value = accounts.normalizeAccount(value)
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   246
      entity_properties[name] = value
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   247
983b126f5aca Always normalize accounts, before even storing them
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1521
diff changeset
   248
      if entity.account != value:
1479
bc738f92a4d8 Fix the user editing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   249
        entity.former_accounts.append(entity.account)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   250
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   251
    return True
640
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 533
diff changeset
   252
  
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 533
diff changeset
   253
  def _onCreate(self, entity):
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 533
diff changeset
   254
    """Send out a message to welcome the new user.
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 533
diff changeset
   255
    """
988
e35b3d98d469 Flush the sidebar if a user's rights change
Sverre Rabbelier <srabbelier@gmail.com>
parents: 865
diff changeset
   256
640
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 533
diff changeset
   257
    notifications.sendWelcomeMessage(entity)
1106
e14b0995cf29 Flush sidebar when creating a new entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1076
diff changeset
   258
e14b0995cf29 Flush sidebar when creating a new entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1076
diff changeset
   259
    super(Logic, self)._onCreate(entity)
308
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   260
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   261
740090cd17c9 Added invididual model logic files
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
logic = Logic()