diff --git a/gnuviechadmin/dashboard/tests/test_views.py b/gnuviechadmin/dashboard/tests/test_views.py index dc1dde5..2ff835b 100644 --- a/gnuviechadmin/dashboard/tests/test_views.py +++ b/gnuviechadmin/dashboard/tests/test_views.py @@ -14,17 +14,7 @@ TEST_PASSWORD = "secret" class IndexViewTest(TestCase): - def test_index_view_anonymous(self): - response = self.client.get(reverse("dashboard")) - self.assertRedirects(response, "/accounts/login/?next=/") - def test_index_view(self): - user = User.objects.create(username=TEST_USER) - user.set_password(TEST_PASSWORD) - user.save() - - self.client.login(username=TEST_USER, password=TEST_PASSWORD) - response = self.client.get(reverse("dashboard")) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "dashboard/index.html") diff --git a/gnuviechadmin/dashboard/views.py b/gnuviechadmin/dashboard/views.py index 7f30316..af9d7e0 100644 --- a/gnuviechadmin/dashboard/views.py +++ b/gnuviechadmin/dashboard/views.py @@ -3,14 +3,13 @@ This module defines the views for the gnuviechadmin customer dashboard. """ from django.contrib.auth import get_user_model -from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic import DetailView, TemplateView from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin from hostingpackages.models import CustomerHostingPackage -class IndexView(LoginRequiredMixin, TemplateView): +class IndexView(TemplateView): """ This is the dashboard view. diff --git a/gnuviechadmin/domains/views.py b/gnuviechadmin/domains/views.py index ac387ec..c81e3b4 100644 --- a/gnuviechadmin/domains/views.py +++ b/gnuviechadmin/domains/views.py @@ -4,8 +4,8 @@ This module defines views related to domains. """ from __future__ import absolute_import +from braces.views import StaffuserRequiredMixin from django.contrib import messages -from django.contrib.auth.mixins import PermissionRequiredMixin from django.shortcuts import get_object_or_404, redirect from django.utils.translation import gettext as _ from django.views.generic.edit import CreateView @@ -16,7 +16,7 @@ from .forms import CreateHostingDomainForm from .models import HostingDomain -class CreateHostingDomain(PermissionRequiredMixin, CreateView): +class CreateHostingDomain(StaffuserRequiredMixin, CreateView): """ This view is used for creating a new HostingDomain instance for an existing hosting package. @@ -24,7 +24,6 @@ class CreateHostingDomain(PermissionRequiredMixin, CreateView): model = HostingDomain raise_exception = True - permission_required = 'domains.add_hostingdomain' template_name_suffix = "_create" form_class = CreateHostingDomainForm diff --git a/gnuviechadmin/gnuviechadmin/tests/test_contextprocessors.py b/gnuviechadmin/gnuviechadmin/tests/test_contextprocessors.py index 94578cd..664e8d2 100644 --- a/gnuviechadmin/gnuviechadmin/tests/test_contextprocessors.py +++ b/gnuviechadmin/gnuviechadmin/tests/test_contextprocessors.py @@ -18,16 +18,13 @@ from gnuviechadmin.context_processors import navigation User = get_user_model() -TEST_USER = "test" -TEST_PASSWORD = "secret" - class NavigationContextProcessorTest(TestCase): EXPECTED_ITEMS = ("webmail_url", "phpmyadmin_url", "phppgadmin_url", "active_item") def test_ajax_request(self): - response = self.client.get("/accounts/login/", HTTP_X_REQUESTED_WITH="XMLHttpRequest") + response = self.client.get("/", HTTP_X_REQUESTED_WITH="XMLHttpRequest") for item in self.EXPECTED_ITEMS: self.assertNotIn(item, response.context) @@ -37,12 +34,6 @@ class NavigationContextProcessorTest(TestCase): self.assertEqual(context["phppgadmin_url"], settings.GVA_LINK_PHPPGADMIN) def test_index_page_context(self): - user = User.objects.create(username=TEST_USER) - user.set_password(TEST_PASSWORD) - user.save() - - self.client.login(username=TEST_USER, password=TEST_PASSWORD) - response = self.client.get("/") for item in self.EXPECTED_ITEMS: self.assertIn(item, response.context) @@ -115,6 +106,6 @@ class NavigationContextProcessorTest(TestCase): class VersionInfoContextProcessorTest(TestCase): def test_version_info_in_context(self): - response = self.client.get("/accounts/login/") + response = self.client.get("/") self.assertIn("gnuviechadmin_version", response.context) self.assertEqual(response.context["gnuviechadmin_version"], gvaversion) diff --git a/gnuviechadmin/hostingpackages/views.py b/gnuviechadmin/hostingpackages/views.py index 038acb2..82dffac 100644 --- a/gnuviechadmin/hostingpackages/views.py +++ b/gnuviechadmin/hostingpackages/views.py @@ -4,10 +4,10 @@ This module defines views related to hosting packages. """ from __future__ import absolute_import +from braces.views import LoginRequiredMixin, StaffuserRequiredMixin from django.conf import settings from django.contrib import messages from django.contrib.auth import get_user_model -from django.contrib.auth.mixins import PermissionRequiredMixin, UserPassesTestMixin from django.http import Http404 from django.shortcuts import get_object_or_404, redirect from django.utils.translation import gettext as _ @@ -30,7 +30,7 @@ from .models import ( ) -class CreateHostingPackage(PermissionRequiredMixin, CreateView): +class CreateHostingPackage(LoginRequiredMixin, StaffuserRequiredMixin, CreateView): """ Create a hosting package. @@ -38,7 +38,6 @@ class CreateHostingPackage(PermissionRequiredMixin, CreateView): model = CustomerHostingPackage raise_exception = True - permission_required = 'domains.add_customerhostingpackage' template_name_suffix = "_create" form_class = CreateHostingPackageForm @@ -121,16 +120,9 @@ class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView): return context -class StaffUserRequiredMixin(UserPassesTestMixin): - """ - Mixin to make views available to staff members only. - - """ - def test_func(self): - return self.request.user.is_staff - - -class AllCustomerHostingPackageList(StaffUserRequiredMixin, ListView): +class AllCustomerHostingPackageList( + LoginRequiredMixin, StaffuserRequiredMixin, ListView +): """ This view is used for showing a list of all hosting packages. @@ -169,7 +161,7 @@ class CustomerHostingPackageList(StaffOrSelfLoginRequiredMixin, ListView): ) -class HostingOptionChoices(StaffUserRequiredMixin, DetailView): +class HostingOptionChoices(LoginRequiredMixin, StaffuserRequiredMixin, DetailView): """ This view displays choices of hosting options for a customer hosting package. @@ -213,7 +205,7 @@ class HostingOptionChoices(StaffUserRequiredMixin, DetailView): return context -class AddHostingOption(StaffUserRequiredMixin, FormView): +class AddHostingOption(LoginRequiredMixin, StaffuserRequiredMixin, FormView): template_name = "hostingpackages/add_hosting_option.html" def get_form_class(self): diff --git a/gnuviechadmin/dashboard/templates/dashboard/index.html b/gnuviechadmin/templates/dashboard/index.html similarity index 100% rename from gnuviechadmin/dashboard/templates/dashboard/index.html rename to gnuviechadmin/templates/dashboard/index.html diff --git a/gnuviechadmin/dashboard/templates/dashboard/user_dashboard.html b/gnuviechadmin/templates/dashboard/user_dashboard.html similarity index 100% rename from gnuviechadmin/dashboard/templates/dashboard/user_dashboard.html rename to gnuviechadmin/templates/dashboard/user_dashboard.html diff --git a/poetry.lock b/poetry.lock index 720f7ea..876312e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -638,6 +638,21 @@ python3-openid = ">=3.0.8" requests = "*" requests-oauthlib = ">=0.3.0" +[[package]] +name = "django-braces" +version = "1.15.0" +description = "Reusable, generic mixins for Django" +category = "main" +optional = false +python-versions = "*" +files = [ + {file = "django-braces-1.15.0.tar.gz", hash = "sha256:f451d08ffc1078d81209a2e17f2219bce20196928853c82405451b18a46875e0"}, + {file = "django_braces-1.15.0-py2.py3-none-any.whl", hash = "sha256:28f00b0f98368c9a37f30cce6087fc57127f0a24c5b8b449f9e1245bded6405d"}, +] + +[package.dependencies] +Django = ">=2.2" + [[package]] name = "django-crispy-forms" version = "1.14.0" @@ -1742,4 +1757,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.7" -content-hash = "6041c8bb49cd1df098f1948f8ad2cbd48fd8f42ff44e410f3fecb61be7e80a18" +content-hash = "c11eec493daca3a228f3c99300d0ebf0fa35060624c93649e2dce4c71cdf67f2" diff --git a/pyproject.toml b/pyproject.toml index 3f3b448..12fc1af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ django = "<4" psycopg2-binary = "^2.9" celery = "^5.2.7" django-allauth = "^0.52.0" +django-braces = "^1.15.0" django-crispy-forms = "<2" django-debug-toolbar = "^3.8" django-model-utils = "^4.1"