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.
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.
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.
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.
55 group_logic: logic for the group which should be retrieved
56 scope_path : the scope path of the entity
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]}
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)
72 def _updateField(self
, entity
, entity_properties
, name
):
73 """Special logic for role. If status changes to active we flush the sidebar.
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
)
85 def _onCreate(self
, entity
):
86 """Flush the sidebar cache when a new active role entity has been created.
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.
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
,
109 roles
= self
.getForFields(fields
, limit
=2)
111 # if this it the last one return error message
113 return DEF_LAST_RESIGN_ERROR_FMT
115 # resignation is possible