app/soc/logic/models/role.py
author Sverre Rabbelier <sverre@rabbelier.nl>
Fri, 15 May 2009 23:05:13 +0200
changeset 2319 3eee2308f1dd
parent 2072 eb4565a8e5b4
child 2664 06d4f4d06f64
permissions -rw-r--r--
Do not rely on notifiction module being imported This has worked so far mostly by accident, but it turned out to be brittle while writing tests. This makes sure that the notification module is always imported before use.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
f2a5ec8fb780 Initial definition of the Host Model.
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.
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
     8
#
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    10
#
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 530
diff changeset
    17
"""Role (Model) query functions.
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    18
"""
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
429
e50e18936f06 Fixed typo in e-mail address
Sverre Rabbelier <srabbelier@gmail.com>
parents: 410
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
715
51703b18ef2e Adds the removal of requests from the datastore when a new role entity is created.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 671
diff changeset
    22
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    23
  ]
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    24
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    25
991
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    26
from soc.cache import sidebar
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    27
from soc.logic.models import base
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    28
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 444
diff changeset
    29
import soc.models.role
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
207
8ecc2e4198cd Take advantage of the Model inheritance provided by polymodel.PolyModel to
Todd Larsen <tlarsen@google.com>
parents: 181
diff changeset
    31
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    32
DEF_LAST_RESIGN_ERROR_FMT = "This user can't be " \
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    33
    "resigned, please make sure it's not the last %(name)s."
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    34
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    35
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    36
class Logic(base.Logic):
535
9045b8888772 Refactor classes in soc/logic/models to make more use of inheritance. Add
Todd Larsen <tlarsen@google.com>
parents: 530
diff changeset
    37
  """Logic methods for the Role model.
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    38
  """
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    40
  def __init__(self, model=soc.models.role.Role,
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    41
               base_model=None, scope_logic=None, disallow_last_resign=False):
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    42
    """Defines the name, key_name and model for this entity.
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
    43
    """
655
9635cbaa2dcd Cleanups to the logic module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 535
diff changeset
    44
671
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    45
    super(Logic, self).__init__(model, base_model=base_model,
2c02178037ff Apply DI on the scope logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 655
diff changeset
    46
                                scope_logic=scope_logic)
410
2af7f84f4fc7 Moved all key_name related things to the logic modules
Sverre Rabbelier <srabbelier@gmail.com>
parents: 409
diff changeset
    47
1976
be994529deee Fixed typo in variable name.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1956
diff changeset
    48
    self.disallow_last_resign = disallow_last_resign
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    49
19
f2a5ec8fb780 Initial definition of the Host Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
978
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    51
  def getGroupEntityFromScopePath(self, group_logic, scope_path):
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    52
    """Returns a group entity by using the given scope_path.
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    53
    
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    54
    Args:
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    55
      group_logic: logic for the group which should be retrieved
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    56
      scope_path : the scope path of the entity
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    57
    """
1625
cd7174032b56 Add missing whitespace in soc.logic.models.role module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1517
diff changeset
    58
    group_key_fields = scope_path.rsplit('/', 1)
978
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    59
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    60
    if len(group_key_fields) == 1:
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    61
      # there is only a link_id
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    62
      fields = {'link_id' : group_key_fields[0]}
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    63
    else:
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    64
      # there is a scope_path and link_id
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    65
      fields = {'scope_path' : group_key_fields[0],
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    66
                'link_id' : group_key_fields[1]}
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    67
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    68
    group = group_logic.getForFields(fields, unique=True)
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    69
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    70
    return group
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
    71
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    72
  def _updateField(self, entity, entity_properties, name):
1085
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1001
diff changeset
    73
    """Special logic for role. If status changes to active we flush the sidebar.
991
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    74
    """
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    75
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    76
    value = entity_properties[name]
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    77
1085
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1001
diff changeset
    78
    if (name == 'status') and (entity.status != value) and value == 'active':
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1001
diff changeset
    79
      # in case the status of the role changes to active we flush the sidebar
991
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    80
      # cache. Other changes will be visible after the retention time expires.
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    81
      sidebar.flush(entity.user.account)
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    82
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    83
    return True
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    84
1001
2faa7f80e8b2 Flush the sidebar cache when a new active role entity has been created.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 991
diff changeset
    85
  def _onCreate(self, entity):
2faa7f80e8b2 Flush the sidebar cache when a new active role entity has been created.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 991
diff changeset
    86
    """Flush the sidebar cache when a new active role entity has been created.
2faa7f80e8b2 Flush the sidebar cache when a new active role entity has been created.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 991
diff changeset
    87
    """
2faa7f80e8b2 Flush the sidebar cache when a new active role entity has been created.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 991
diff changeset
    88
1085
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1001
diff changeset
    89
    if entity.status == 'active':
1001
2faa7f80e8b2 Flush the sidebar cache when a new active role entity has been created.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 991
diff changeset
    90
      sidebar.flush(entity.user.account)
991
becede26c37f Added cache flush for the user who creates a group and the user whose role has been activated.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 978
diff changeset
    91
1117
5785d9af5c5d Fixed a bug in role.py that made it call the wrong superclass.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1106
diff changeset
    92
    super(Logic, self)._onCreate(entity)
1106
e14b0995cf29 Flush sidebar when creating a new entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1085
diff changeset
    93
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    94
  def canResign(self, entity):
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    95
    """Checks if the current entity is allowed to be resigned.
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
    96
1956
1fcddf90eccc Fixed docstring for canResign method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    97
    Args:
1fcddf90eccc Fixed docstring for canResign method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    98
      entity: a Role entity
1fcddf90eccc Fixed docstring for canResign method.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1955
diff changeset
    99
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   100
    Returns:
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   101
      - None if the entity is allowed to resign.
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   102
      - Error message otherwise.
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   103
    """
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   104
1976
be994529deee Fixed typo in variable name.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1956
diff changeset
   105
    if self.disallow_last_resign:
2072
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1976
diff changeset
   106
      # check if this is the last active role for it's scope
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1976
diff changeset
   107
      fields = {'scope': entity.scope,
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   108
          'status': 'active'}
2072
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1976
diff changeset
   109
      roles = self.getForFields(fields, limit=2)
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   110
2072
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1976
diff changeset
   111
      # if this it the last one return error message
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1976
diff changeset
   112
      if len(roles) <= 1:
eb4565a8e5b4 Style fixes and removal of unused imports in soc.logic.models modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1976
diff changeset
   113
        return DEF_LAST_RESIGN_ERROR_FMT
1955
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   114
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   115
    # resignation is possible
06ed84dbb1ed Moved canResign check to role logic so subclasses can override.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1625
diff changeset
   116
    return None
978
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 916
diff changeset
   117
404
44223e50e1fc Added a Host profile and generalized some views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 244
diff changeset
   118
logic = Logic()