diff --git a/jarbas/dashboard/admin.py b/jarbas/dashboard/admin.py index 4b0dd92..a70c461 100644 --- a/jarbas/dashboard/admin.py +++ b/jarbas/dashboard/admin.py @@ -1,5 +1,4 @@ import json -import re from brazilnum.cnpj import format_cnpj from brazilnum.cpf import format_cpf @@ -7,10 +6,10 @@ from django.contrib.postgres.search import SearchQuery, SearchRank from django.db.models import F from django.forms.widgets import Widget -from simple_history.admin import SimpleHistoryAdmin 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) @@ -254,7 +253,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', @@ -361,26 +360,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: @@ -412,4 +391,4 @@ def get_search_results(self, request, queryset, search_term): return queryset, distinct -dashboard.register(Reimbursement, ReimbursementModelAdmin) +public_admin.register(Reimbursement, ReimbursementModelAdmin) diff --git a/jarbas/dashboard/urls.py b/jarbas/dashboard/urls.py index 46638e4..9f06c15 100644 --- a/jarbas/dashboard/urls.py +++ b/jarbas/dashboard/urls.py @@ -1,6 +1,8 @@ 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..1eb0d17 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 DummyUser, PublicAdminSite, 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)