27 from soc.logic.models import base |
27 from soc.logic.models import base |
28 |
28 |
29 import soc.models.role |
29 import soc.models.role |
30 |
30 |
31 |
31 |
|
32 DEF_LAST_RESIGN_ERROR_FMT = "This user can't be " \ |
|
33 "resigned, please make sure it's not the last %(name)s." |
|
34 |
|
35 |
32 class Logic(base.Logic): |
36 class Logic(base.Logic): |
33 """Logic methods for the Role model. |
37 """Logic methods for the Role model. |
34 """ |
38 """ |
35 |
39 |
36 def __init__(self, model=soc.models.role.Role, |
40 def __init__(self, model=soc.models.role.Role, |
37 base_model=None, scope_logic=None): |
41 base_model=None, scope_logic=None, disallow_last_resign=False): |
38 """Defines the name, key_name and model for this entity. |
42 """Defines the name, key_name and model for this entity. |
39 """ |
43 """ |
40 |
44 |
41 super(Logic, self).__init__(model, base_model=base_model, |
45 super(Logic, self).__init__(model, base_model=base_model, |
42 scope_logic=scope_logic) |
46 scope_logic=scope_logic) |
|
47 |
|
48 self.dissalow_last_resign = disallow_last_resign |
43 |
49 |
44 |
50 |
45 def getGroupEntityFromScopePath(self, group_logic, scope_path): |
51 def getGroupEntityFromScopePath(self, group_logic, scope_path): |
46 """Returns a group entity by using the given scope_path. |
52 """Returns a group entity by using the given scope_path. |
47 |
53 |
73 # in case the status of the role changes to active we flush the sidebar |
79 # in case the status of the role changes to active we flush the sidebar |
74 # cache. Other changes will be visible after the retention time expires. |
80 # cache. Other changes will be visible after the retention time expires. |
75 sidebar.flush(entity.user.account) |
81 sidebar.flush(entity.user.account) |
76 |
82 |
77 return True |
83 return True |
78 |
84 |
79 def _onCreate(self, entity): |
85 def _onCreate(self, entity): |
80 """Flush the sidebar cache when a new active role entity has been created. |
86 """Flush the sidebar cache when a new active role entity has been created. |
81 """ |
87 """ |
82 |
88 |
83 if entity.status == 'active': |
89 if entity.status == 'active': |
84 sidebar.flush(entity.user.account) |
90 sidebar.flush(entity.user.account) |
85 |
91 |
86 super(Logic, self)._onCreate(entity) |
92 super(Logic, self)._onCreate(entity) |
87 |
93 |
|
94 def canResign(self, entity): |
|
95 """Checks if the current entity is allowed to be resigned. |
|
96 |
|
97 Returns: |
|
98 - None if the entity is allowed to resign. |
|
99 - Error message otherwise. |
|
100 """ |
|
101 |
|
102 if self.dissalow_last_resign: |
|
103 # check if this is the last active role for it's scope |
|
104 fields = {'scope': entity.scope, |
|
105 'status': 'active'} |
|
106 roles = self.getForFields(fields, limit=2) |
|
107 |
|
108 # if this it the last one return error message |
|
109 if len(roles) <= 1: |
|
110 return DEF_LAST_RESIGN_ERROR_FMT |
|
111 |
|
112 # resignation is possible |
|
113 return None |
88 |
114 |
89 logic = Logic() |
115 logic = Logic() |