Style fixes and removal of unused imports in soc.logic.models modules.
[Melange.git] / app / soc / logic / models / role.py
blob477667609a9d9c6f19cfb1a16c7a8aee319249ee
1 #!/usr/bin/python2.5
3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Role (Model) query functions.
18 """
20 __authors__ = [
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
22 '"Lennard de Rijk" <ljvderijk@gmail.com>',
26 from soc.cache import sidebar
27 from soc.logic.models import base
29 import soc.models.role
32 DEF_LAST_RESIGN_ERROR_FMT = "This user can't be " \
33 "resigned, please make sure it's not the last %(name)s."
36 class Logic(base.Logic):
37 """Logic methods for the Role model.
38 """
40 def __init__(self, model=soc.models.role.Role,
41 base_model=None, scope_logic=None, disallow_last_resign=False):
42 """Defines the name, key_name and model for this entity.
43 """
45 super(Logic, self).__init__(model, base_model=base_model,
46 scope_logic=scope_logic)
48 self.disallow_last_resign = disallow_last_resign
51 def getGroupEntityFromScopePath(self, group_logic, scope_path):
52 """Returns a group entity by using the given scope_path.
54 Args:
55 group_logic: logic for the group which should be retrieved
56 scope_path : the scope path of the entity
57 """
58 group_key_fields = scope_path.rsplit('/', 1)
60 if len(group_key_fields) == 1:
61 # there is only a link_id
62 fields = {'link_id' : group_key_fields[0]}
63 else:
64 # there is a scope_path and link_id
65 fields = {'scope_path' : group_key_fields[0],
66 'link_id' : group_key_fields[1]}
68 group = group_logic.getForFields(fields, unique=True)
70 return group
72 def _updateField(self, entity, entity_properties, name):
73 """Special logic for role. If status changes to active we flush the sidebar.
74 """
76 value = entity_properties[name]
78 if (name == 'status') and (entity.status != value) and value == 'active':
79 # in case the status of the role changes to active we flush the sidebar
80 # cache. Other changes will be visible after the retention time expires.
81 sidebar.flush(entity.user.account)
83 return True
85 def _onCreate(self, entity):
86 """Flush the sidebar cache when a new active role entity has been created.
87 """
89 if entity.status == 'active':
90 sidebar.flush(entity.user.account)
92 super(Logic, self)._onCreate(entity)
94 def canResign(self, entity):
95 """Checks if the current entity is allowed to be resigned.
97 Args:
98 entity: a Role entity
100 Returns:
101 - None if the entity is allowed to resign.
102 - Error message otherwise.
105 if self.disallow_last_resign:
106 # check if this is the last active role for it's scope
107 fields = {'scope': entity.scope,
108 'status': 'active'}
109 roles = self.getForFields(fields, limit=2)
111 # if this it the last one return error message
112 if len(roles) <= 1:
113 return DEF_LAST_RESIGN_ERROR_FMT
115 # resignation is possible
116 return None
118 logic = Logic()