Moved canResign check to role logic so subclasses can override.
Also changed the variable name in the templates to be more generic.
Patch by: Lennard de Rijk
Reviewed by: to-be-reviewed
--- a/app/soc/logic/models/club_admin.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/logic/models/club_admin.py Fri Mar 20 18:53:37 2009 +0000
@@ -35,12 +35,14 @@
"""
def __init__(self, model=soc.models.club_admin.ClubAdmin,
- base_model=soc.models.role.Role, scope_logic=club_logic):
+ base_model=soc.models.role.Role, scope_logic=club_logic,
+ disallow_last_resign=True):
"""Defines the name, key_name and model for this entity.
"""
super(Logic, self).__init__(model=model, base_model=base_model,
- scope_logic=scope_logic)
+ scope_logic=scope_logic,
+ disallow_last_resign=disallow_last_resign)
def _onCreate(self, entity):
"""Marks the Club for this Club Admin as active it's status is new.
--- a/app/soc/logic/models/club_member.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/logic/models/club_member.py Fri Mar 20 18:53:37 2009 +0000
@@ -34,12 +34,14 @@
"""
def __init__(self, model=soc.models.club_member.ClubMember,
- base_model=soc.models.role.Role, scope_logic=club_logic):
+ base_model=soc.models.role.Role, scope_logic=club_logic,
+ disallow_last_resign=False):
"""Defines the name, key_name and model for this entity.
"""
super(Logic, self).__init__(model=model, base_model=base_model,
- scope_logic=scope_logic)
+ scope_logic=scope_logic,
+ disallow_last_resign=disallow_last_resign)
logic = Logic()
--- a/app/soc/logic/models/host.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/logic/models/host.py Fri Mar 20 18:53:37 2009 +0000
@@ -35,12 +35,14 @@
"""
def __init__(self, model=soc.models.host.Host,
- base_model=soc.models.role.Role, scope_logic=sponsor_logic):
+ base_model=soc.models.role.Role, scope_logic=sponsor_logic,
+ disallow_last_resign=True):
"""Defines the name, key_name and model for this entity.
"""
super(Logic, self).__init__(model=model, base_model=base_model,
- scope_logic=scope_logic)
+ scope_logic=scope_logic,
+ disallow_last_resign=disallow_last_resign)
def _onCreate(self, entity):
--- a/app/soc/logic/models/mentor.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/logic/models/mentor.py Fri Mar 20 18:53:37 2009 +0000
@@ -34,12 +34,14 @@
"""
def __init__(self, model=soc.models.mentor.Mentor,
- base_model=soc.models.role.Role, scope_logic=org_logic):
+ base_model=soc.models.role.Role, scope_logic=org_logic,
+ disallow_last_resign=False):
"""Defines the name, key_name and model for this entity.
"""
super(Logic, self).__init__(model=model, base_model=base_model,
- scope_logic=scope_logic)
+ scope_logic=scope_logic,
+ disallow_last_resign=disallow_last_resign)
logic = Logic()
--- a/app/soc/logic/models/org_admin.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/logic/models/org_admin.py Fri Mar 20 18:53:37 2009 +0000
@@ -34,12 +34,14 @@
"""
def __init__(self, model=soc.models.org_admin.OrgAdmin,
- base_model=soc.models.role.Role, scope_logic=org_logic):
+ base_model=soc.models.role.Role, scope_logic=org_logic,
+ disallow_last_resign=True):
"""Defines the name, key_name and model for this entity.
"""
super(Logic, self).__init__(model=model, base_model=base_model,
- scope_logic=scope_logic)
+ scope_logic=scope_logic,
+ disallow_last_resign=disallow_last_resign)
def _onCreate(self, entity):
"""Marks the Organization for this Org Admin as active
--- a/app/soc/logic/models/role.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/logic/models/role.py Fri Mar 20 18:53:37 2009 +0000
@@ -29,18 +29,24 @@
import soc.models.role
+DEF_LAST_RESIGN_ERROR_FMT = "This user can't be " \
+ "resigned, please make sure it's not the last %(name)s."
+
+
class Logic(base.Logic):
"""Logic methods for the Role model.
"""
def __init__(self, model=soc.models.role.Role,
- base_model=None, scope_logic=None):
+ base_model=None, scope_logic=None, disallow_last_resign=False):
"""Defines the name, key_name and model for this entity.
"""
super(Logic, self).__init__(model, base_model=base_model,
scope_logic=scope_logic)
+ self.dissalow_last_resign = disallow_last_resign
+
def getGroupEntityFromScopePath(self, group_logic, scope_path):
"""Returns a group entity by using the given scope_path.
@@ -75,7 +81,7 @@
sidebar.flush(entity.user.account)
return True
-
+
def _onCreate(self, entity):
"""Flush the sidebar cache when a new active role entity has been created.
"""
@@ -85,5 +91,25 @@
super(Logic, self)._onCreate(entity)
+ def canResign(self, entity):
+ """Checks if the current entity is allowed to be resigned.
+
+ Returns:
+ - None if the entity is allowed to resign.
+ - Error message otherwise.
+ """
+
+ if self.dissalow_last_resign:
+ # check if this is the last active role for it's scope
+ fields = {'scope': entity.scope,
+ 'status': 'active'}
+ roles = self.getForFields(fields, limit=2)
+
+ # if this it the last one return error message
+ if len(roles) <= 1:
+ return DEF_LAST_RESIGN_ERROR_FMT
+
+ # resignation is possible
+ return None
logic = Logic()
--- a/app/soc/logic/models/student.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/logic/models/student.py Fri Mar 20 18:53:37 2009 +0000
@@ -34,12 +34,14 @@
"""
def __init__(self, model=soc.models.student.Student,
- base_model=soc.models.role.Role, scope_logic=program_logic):
+ base_model=soc.models.role.Role, scope_logic=program_logic,
+ disallow_last_resign=False):
"""Defines the name, key_name and model for this entity.
"""
super(Logic, self).__init__(model=model, base_model=base_model,
- scope_logic=scope_logic)
+ scope_logic=scope_logic,
+ disallow_last_resign=disallow_last_resign)
logic = Logic()
--- a/app/soc/templates/soc/club_admin/manage.html Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/templates/soc/club_admin/manage.html Fri Mar 20 18:53:37 2009 +0000
@@ -18,7 +18,7 @@
{% endblock %}
{% block manage %}
-<div class="error">{{ not_allowed_to_resign }}</div>
+<div class="error">{{ resign_error }}</div>
<tr>
<td>
Please select the appropriate action:</br>
--- a/app/soc/templates/soc/club_member/manage.html Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/templates/soc/club_member/manage.html Fri Mar 20 18:53:37 2009 +0000
@@ -18,7 +18,7 @@
{% endblock %}
{% block manage %}
-<div class="error">{{ not_allowed_to_resign }}</div>
+<div class="error">{{ resign_error }}</div>
<tr>
<td>
Please select the appropriate action:</br>
--- a/app/soc/templates/soc/host/manage.html Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/templates/soc/host/manage.html Fri Mar 20 18:53:37 2009 +0000
@@ -18,6 +18,7 @@
{% endblock %}
{% block manage %}
+<div class="error">{{ resign_error }}</div>
<tr>
<td>
Please select the appropriate action:</br>
--- a/app/soc/templates/soc/mentor/manage.html Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/templates/soc/mentor/manage.html Fri Mar 20 18:53:37 2009 +0000
@@ -18,7 +18,7 @@
{% endblock %}
{% block manage %}
-<div class="error">{{ not_allowed_to_resign }}</div>
+<div class="error">{{ resign_error }}</div>
<tr>
<td>
Please select the appropriate action:</br>
--- a/app/soc/templates/soc/org_admin/manage.html Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/templates/soc/org_admin/manage.html Fri Mar 20 18:53:37 2009 +0000
@@ -18,7 +18,7 @@
{% endblock %}
{% block manage %}
-<div class="error">{{ not_allowed_to_resign }}</div>
+<div class="error">{{ resign_error }}</div>
<tr>
<td>
Please select the appropriate action:</br>
--- a/app/soc/templates/soc/student/manage.html Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/templates/soc/student/manage.html Fri Mar 20 18:53:37 2009 +0000
@@ -18,7 +18,7 @@
{% endblock %}
{% block manage %}
-<div class="error">{{ not_allowed_to_resign }}</div>
+<div class="error">{{ resign_error }}</div>
<tr>
<td>
Please select the appropriate action:</br>
--- a/app/soc/views/models/club_admin.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/views/models/club_admin.py Fri Mar 20 18:53:37 2009 +0000
@@ -76,7 +76,6 @@
new_params['extra_dynaexclude'] = ['agreed_to_tos']
- new_params['disallow_last_resign'] = False
new_params['allow_invites'] = True
new_params['show_in_roles_overview'] = False
--- a/app/soc/views/models/host.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/views/models/host.py Fri Mar 20 18:53:37 2009 +0000
@@ -88,7 +88,6 @@
'clean_blog': cleaning.clean_url('blog'),
'clean_photo_url': cleaning.clean_url('photo_url')}
- new_params['disallow_last_resign'] = False
new_params['allow_invites'] = True
new_params['show_in_roles_overview'] = True
--- a/app/soc/views/models/org_admin.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/views/models/org_admin.py Fri Mar 20 18:53:37 2009 +0000
@@ -108,7 +108,6 @@
},
]
- new_params['disallow_last_resign'] = True
new_params['allow_invites'] = True
new_params['show_in_roles_overview'] = True
--- a/app/soc/views/models/role.py Fri Mar 20 16:54:01 2009 +0000
+++ b/app/soc/views/models/role.py Fri Mar 20 18:53:37 2009 +0000
@@ -156,8 +156,6 @@
new_params['extra_dynaexclude'] = ['user', 'status', 'agreed_to_tos_on']
- new_params['disallow_last_resign'] = False
-
params = dicts.merge(params, new_params, sub_merge=True)
super(View, self).__init__(params=params)
@@ -434,18 +432,9 @@
if resign == 'true':
- if params.get('disallow_last_resign'):
- # check if the current role is the last for this scope
- fields = {'scope': role_entity.scope,
- 'status': 'active'}
- roles = logic.getForFields(fields, limit=2)
+ resign_error = params['logic'].canResign(role_entity)
- # if there is more then one left we can safely resign
- resign = len(roles) > 1
- else:
- resign = True
-
- if resign:
+ if not resign_error:
# change the status of this role_entity to invalid
fields = {'status': 'invalid'}
logic.updateEntityProperties(role_entity, fields)
@@ -454,8 +443,7 @@
return http.HttpResponseRedirect(redirect)
else:
# show error to the user
- context['not_allowed_to_resign'] = ugettext("This user can't be "
- "resigned, please make sure it's not the last %(name)s." % params)
+ context['resign_error'] = ugettext(resign_error %params)
# set the appropriate context
context['entity'] = role_entity
@@ -468,7 +456,6 @@
# return a proper response
return responses.respond(request, template, context=context)
-
@decorators.merge_params
@decorators.check_access
def request(self, request, access_type,