From 581e43a3b037279c46a7ad292cb71352abe95fb0 Mon Sep 17 00:00:00 2001 From: Stefano Brentegani Date: Tue, 29 Jul 2014 20:55:25 +0200 Subject: [PATCH] separate authorisation function from decorator --- helpdesk/decorators.py | 10 ++++------ helpdesk/tests/navigation.py | 7 ++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/helpdesk/decorators.py b/helpdesk/decorators.py index 00624fc6..1226bf0d 100644 --- a/helpdesk/decorators.py +++ b/helpdesk/decorators.py @@ -4,14 +4,12 @@ from helpdesk import settings as helpdesk_settings if helpdesk_settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK: # apply a custom user validation condition - helpdesk_staff_member_required = user_passes_test(helpdesk_settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK) + is_helpdesk_staff = helpdesk_settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK elif helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE: # treat 'normal' users like 'staff' - helpdesk_staff_member_required = user_passes_test(lambda u: u.is_authenticated() and u.is_active) + is_helpdesk_staff = lambda u: u.is_authenticated() and u.is_active else: - try: - from django.contrib.admin.views.decorators import staff_member_required as helpdesk_staff_member_required - except ImportError: - helpdesk_staff_member_required = user_passes_test(lambda u: u.is_authenticated() and u.is_active and u.is_staff) + is_helpdesk_staff = lambda u: u.is_authenticated() and u.is_active and u.is_staff +helpdesk_staff_member_required = user_passes_test(is_helpdesk_staff) helpdesk_superuser_required = user_passes_test(lambda u: u.is_authenticated() and u.is_active and u.is_superuser) diff --git a/helpdesk/tests/navigation.py b/helpdesk/tests/navigation.py index ab3454db..32bbdb5c 100644 --- a/helpdesk/tests/navigation.py +++ b/helpdesk/tests/navigation.py @@ -39,7 +39,6 @@ class KBDisabledTestCase(TestCase): class StaffUserTestCaseMixin(object): HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK = None - expected_login_template = 'admin/login.html' def setUp(self): self.old_settings = settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE, settings.HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK @@ -62,13 +61,12 @@ class StaffUserTestCaseMixin(object): def test_anonymous_user(self): """Access to the dashboard always requires a login""" response = self.client.get(reverse('helpdesk_dashboard'), follow=True) - self.assertTemplateUsed(response, self.expected_login_template) + self.assertTemplateUsed(response, 'helpdesk/registration/login.html') class NonStaffUsersAllowedTestCase(StaffUserTestCaseMixin, TestCase): HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = True HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK = None - expected_login_template = 'helpdesk/registration/login.html' def test_non_staff_allowed(self): """If HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE is True, @@ -100,7 +98,6 @@ class StaffUsersOnlyTestCase(StaffUserTestCaseMixin, TestCase): class CustomStaffUserTestCase(StaffUserTestCaseMixin, TestCase): HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False - expected_login_template = 'helpdesk/registration/login.html' @staticmethod def HELPDESK_CUSTOM_STAFF_FILTER_CALLBACK(user): @@ -122,4 +119,4 @@ class CustomStaffUserTestCase(StaffUserTestCaseMixin, TestCase): self.client.login(username=user.username, password='frog') response = self.client.get(reverse('helpdesk_dashboard'), follow=True) - self.assertTemplateUsed(response, self.expected_login_template) \ No newline at end of file + self.assertTemplateUsed(response, 'helpdesk/registration/login.html')