From f8857deb5a7ffaa6ca7f94a7e4dfa9386f471f65 Mon Sep 17 00:00:00 2001 From: Eduardo Cuducos Date: Sat, 19 Aug 2017 13:17:13 -0300 Subject: [PATCH 1/2] Separate logic: public admin and dashboard Public Admin app: logic to make Django Adminpublic and loginless Dashboard app: logic for the views of Jarbas Admin --- jarbas/dashboard/admin.py | 31 +++---------------- jarbas/dashboard/urls.py | 4 +-- jarbas/public_admin/__init__.py | 0 jarbas/public_admin/admin.py | 30 ++++++++++++++++++ jarbas/{dashboard => public_admin}/sites.py | 10 ++---- jarbas/public_admin/tests/__init__.py | 0 .../tests/test_dummy_user.py | 2 +- .../tests/test_public_admin_site.py} | 10 +++--- 8 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 jarbas/public_admin/__init__.py create mode 100644 jarbas/public_admin/admin.py rename jarbas/{dashboard => public_admin}/sites.py (86%) create mode 100644 jarbas/public_admin/tests/__init__.py rename jarbas/{dashboard => public_admin}/tests/test_dummy_user.py (93%) rename jarbas/{dashboard/tests/test_dashboard_site.py => public_admin/tests/test_public_admin_site.py} (84%) diff --git a/jarbas/dashboard/admin.py b/jarbas/dashboard/admin.py index f9adfba..4e1e9e2 100644 --- a/jarbas/dashboard/admin.py +++ b/jarbas/dashboard/admin.py @@ -1,14 +1,13 @@ import json -import re from brazilnum.cnpj import format_cnpj from brazilnum.cpf import format_cpf -from django.contrib.admin import SimpleListFilter from django.forms.widgets import Widget -from simple_history.admin import SimpleHistoryAdmin +from django.contrib.admin import SimpleListFilter from jarbas.core.models import Reimbursement -from jarbas.dashboard.sites import dashboard +from jarbas.public_admin.admin import PublicAdminModelAdmin +from jarbas.public_admin.sites import public_admin ALL_FIELDS = sorted(Reimbursement._meta.fields, key=lambda f: f.verbose_name) @@ -231,7 +230,7 @@ def queryset(self, request, queryset): return queryset.filter(subquota_description=self.en_us(subquota)) -class ReimbursementModelAdmin(SimpleHistoryAdmin): +class ReimbursementModelAdmin(PublicAdminModelAdmin): list_display = ( 'short_document_id', @@ -331,26 +330,6 @@ def short_document_id(self, obj): def subquota_translated(self, obj): return Subquotas.pt_br(obj.subquota_description) - def has_add_permission(self, request): - return False - - def has_change_permission(self, request, obj=None): - return request.method == 'GET' - - def has_delete_permission(self, request, obj=None): - return False - - @staticmethod - def rename_change_url(url): - if 'change' in url.regex.pattern: - new_re = url.regex.pattern.replace('change', 'details') - url.regex = re.compile(new_re, re.UNICODE) - return url - - def get_urls(self): - urls = filter(dashboard.valid_url, super().get_urls()) - return list(map(self.rename_change_url, urls)) - def get_object(self, request, object_id, from_field=None): obj = super().get_object(request, object_id, from_field) if obj and not obj.receipt_fetched: @@ -368,4 +347,4 @@ def formfield_for_dbfield(self, db_field, **kwargs): return super().formfield_for_dbfield(db_field, **kwargs) -dashboard.register(Reimbursement, ReimbursementModelAdmin) +public_admin.register(Reimbursement, ReimbursementModelAdmin) diff --git a/jarbas/dashboard/urls.py b/jarbas/dashboard/urls.py index 46638e4..79542a0 100644 --- a/jarbas/dashboard/urls.py +++ b/jarbas/dashboard/urls.py @@ -1,6 +1,6 @@ from django.conf.urls import url -from jarbas.dashboard.sites import dashboard +from jarbas.public_admin.sites import public_admin urlpatterns = [ - url(r'', dashboard.urls) + url(r'', public_admin.urls) ] diff --git a/jarbas/public_admin/__init__.py b/jarbas/public_admin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jarbas/public_admin/admin.py b/jarbas/public_admin/admin.py new file mode 100644 index 0000000..e94465d --- /dev/null +++ b/jarbas/public_admin/admin.py @@ -0,0 +1,30 @@ +import re + +from simple_history.admin import SimpleHistoryAdmin + +from jarbas.public_admin.sites import public_admin + + +class PublicAdminModelAdmin(SimpleHistoryAdmin): + + def has_add_permission(self, request): + return False + + def has_change_permission(self, request, obj=None): + return request.method == 'GET' + + def has_delete_permission(self, request, obj=None): + return False + + @staticmethod + def rename_change_url(url): + if 'change' in url.regex.pattern: + new_re = url.regex.pattern.replace('change', 'details') + url.regex = re.compile(new_re, re.UNICODE) + return url + + def get_urls(self): + return [ + self.rename_change_url(url) for url in super().get_urls() + if public_admin.valid_url(url) + ] diff --git a/jarbas/dashboard/sites.py b/jarbas/public_admin/sites.py similarity index 86% rename from jarbas/dashboard/sites.py rename to jarbas/public_admin/sites.py index f78fd37..583c045 100644 --- a/jarbas/dashboard/sites.py +++ b/jarbas/public_admin/sites.py @@ -3,7 +3,6 @@ from django.contrib.admin.sites import AdminSite from django.contrib.auth.models import AnonymousUser from django.http import HttpResponseForbidden -from django.views.decorators.cache import never_cache from django.views.decorators.csrf import csrf_protect @@ -16,7 +15,7 @@ def has_perm(self, permission, obj=None): return permission == 'core.change_reimbursement' -class DashboardSite(AdminSite): +class PublicAdminSite(AdminSite): site_title = 'Dashboard' site_header = 'Jarbas Dashboard' @@ -40,7 +39,7 @@ def valid_url(url): @property def urls(self): - urls = filter(self.valid_url, self.get_urls()) + urls = (url for url in self.get_urls() if self.valid_url(url)) return list(urls), 'admin', self.name def has_permission(self, request): @@ -53,13 +52,10 @@ def inner(request, *args, **kwargs): return HttpResponseForbidden() return view(request, *args, **kwargs) - if not cacheable: - inner = never_cache(inner) - if not getattr(view, 'csrf_exempt', False): inner = csrf_protect(inner) return update_wrapper(inner, view) -dashboard = DashboardSite() +public_admin = PublicAdminSite() diff --git a/jarbas/public_admin/tests/__init__.py b/jarbas/public_admin/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jarbas/dashboard/tests/test_dummy_user.py b/jarbas/public_admin/tests/test_dummy_user.py similarity index 93% rename from jarbas/dashboard/tests/test_dummy_user.py rename to jarbas/public_admin/tests/test_dummy_user.py index b7fa120..56f7d3d 100644 --- a/jarbas/dashboard/tests/test_dummy_user.py +++ b/jarbas/public_admin/tests/test_dummy_user.py @@ -1,6 +1,6 @@ from django.test import TestCase -from jarbas.dashboard.sites import DummyUser +from jarbas.public_admin.sites import DummyUser class TestDummyUser(TestCase): diff --git a/jarbas/dashboard/tests/test_dashboard_site.py b/jarbas/public_admin/tests/test_public_admin_site.py similarity index 84% rename from jarbas/dashboard/tests/test_dashboard_site.py rename to jarbas/public_admin/tests/test_public_admin_site.py index 7b2399f..be01e3d 100644 --- a/jarbas/dashboard/tests/test_dashboard_site.py +++ b/jarbas/public_admin/tests/test_public_admin_site.py @@ -5,15 +5,15 @@ from django.contrib.contenttypes.models import ContentType from django.test import TestCase -from jarbas.dashboard.sites import DashboardSite, DummyUser, dashboard +from jarbas.public_admin.sites import PublicAdminSite, DummyUser, public_admin User = get_user_model() -class TestDashboardSite(TestCase): +class TestPublicAdminSite(TestCase): def setUp(self): - self.site = dashboard + self.site = public_admin def test_init(self): self.assertEqual({}, dict(self.site.actions)) @@ -27,8 +27,8 @@ def test_valid_url(self): self.assertTrue(self.site.valid_url(valid)) self.assertFalse(self.site.valid_url(invalid)) - @patch.object(DashboardSite, 'get_urls') - @patch.object(DashboardSite, 'valid_url') + @patch.object(PublicAdminSite, 'get_urls') + @patch.object(PublicAdminSite, 'valid_url') def test_urls(self, valid_url, get_urls): valid_url.side_effect = (True, False, True) get_urls.return_value = range(3) From 94d8f94d362d4306f5466c3990958693943c20ed Mon Sep 17 00:00:00 2001 From: Eduardo Cuducos Date: Tue, 29 Aug 2017 08:13:35 -0300 Subject: [PATCH 2/2] Sort imports --- jarbas/dashboard/urls.py | 2 ++ jarbas/public_admin/tests/test_public_admin_site.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/jarbas/dashboard/urls.py b/jarbas/dashboard/urls.py index 79542a0..9f06c15 100644 --- a/jarbas/dashboard/urls.py +++ b/jarbas/dashboard/urls.py @@ -1,6 +1,8 @@ from django.conf.urls import url + from jarbas.public_admin.sites import public_admin + urlpatterns = [ url(r'', public_admin.urls) ] diff --git a/jarbas/public_admin/tests/test_public_admin_site.py b/jarbas/public_admin/tests/test_public_admin_site.py index be01e3d..1eb0d17 100644 --- a/jarbas/public_admin/tests/test_public_admin_site.py +++ b/jarbas/public_admin/tests/test_public_admin_site.py @@ -5,7 +5,7 @@ from django.contrib.contenttypes.models import ContentType from django.test import TestCase -from jarbas.public_admin.sites import PublicAdminSite, DummyUser, public_admin +from jarbas.public_admin.sites import DummyUser, PublicAdminSite, public_admin User = get_user_model()