diff --git a/accounting/views.py b/accounting/views.py index ce0ae45b..dd5d2e09 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -45,14 +45,9 @@ from accounting.widgets.select import ( from club.models import Club from club.widgets.select import AutoCompleteSelectClub from core.models import User -from core.views import ( - CanCreateMixin, - CanEditMixin, - CanEditPropMixin, - CanViewMixin, - TabedViewMixin, -) +from core.views import CanCreateMixin, CanEditMixin, CanEditPropMixin, CanViewMixin from core.views.forms import SelectDate, SelectFile +from core.views.mixins import TabedViewMixin from core.views.widgets.select import AutoCompleteSelectUser from counter.models import Counter, Product, Selling diff --git a/club/views.py b/club/views.py index eda5ac48..a6b6d009 100644 --- a/club/views.py +++ b/club/views.py @@ -58,8 +58,8 @@ from core.views import ( CanViewMixin, DetailFormView, PageEditViewBase, - TabedViewMixin, ) +from core.views.mixins import TabedViewMixin from counter.models import Selling diff --git a/com/views.py b/com/views.py index 54e37578..9216cb00 100644 --- a/com/views.py +++ b/com/views.py @@ -27,10 +27,7 @@ from smtplib import SMTPRecipientsRefused from typing import Any from django.conf import settings -from django.contrib.auth.mixins import ( - AccessMixin, - PermissionRequiredMixin, -) +from django.contrib.auth.mixins import AccessMixin, PermissionRequiredMixin from django.core.exceptions import PermissionDenied, ValidationError from django.db.models import Max from django.forms.models import modelform_factory @@ -48,12 +45,8 @@ from com.calendar import IcsCalendar from com.forms import NewsDateForm, NewsForm, PosterForm from com.models import News, NewsDate, Poster, Screen, Sith, Weekmail, WeekmailArticle from core.models import User -from core.views import ( - CanEditPropMixin, - CanViewMixin, - QuickNotifMixin, - TabedViewMixin, -) +from core.views import CanEditPropMixin, CanViewMixin, QuickNotifMixin, TabedViewMixin +from core.views.mixins import QuickNotifMixin, TabedViewMixin from core.views.widgets.markdown import MarkdownInput # Sith object diff --git a/core/views/__init__.py b/core/views/__init__.py index debbc810..0cff8afe 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -25,18 +25,13 @@ import types from typing import Any -from django.conf import settings from django.contrib.auth.mixins import AccessMixin -from django.core.exceptions import ( - ImproperlyConfigured, - PermissionDenied, -) +from django.core.exceptions import PermissionDenied from django.http import ( HttpResponseForbidden, HttpResponseNotFound, HttpResponseServerError, ) -from django.shortcuts import render from django.utils.functional import cached_property from django.views.generic.base import View from django.views.generic.detail import SingleObjectMixin @@ -245,62 +240,6 @@ class SubscriberMixin(AccessMixin): return super().dispatch(request, *args, **kwargs) -class TabedViewMixin(View): - """Basic functions for displaying tabs in the template.""" - - def get_tabs_title(self): - if hasattr(self, "tabs_title"): - return self.tabs_title - raise ImproperlyConfigured("tabs_title is required") - - def get_current_tab(self): - if hasattr(self, "current_tab"): - return self.current_tab - raise ImproperlyConfigured("current_tab is required") - - def get_list_of_tabs(self): - if hasattr(self, "list_of_tabs"): - return self.list_of_tabs - raise ImproperlyConfigured("list_of_tabs is required") - - def get_context_data(self, **kwargs): - kwargs = super().get_context_data(**kwargs) - kwargs["tabs_title"] = self.get_tabs_title() - kwargs["current_tab"] = self.get_current_tab() - kwargs["list_of_tabs"] = self.get_list_of_tabs() - return kwargs - - -class QuickNotifMixin: - quick_notif_list = [] - - def dispatch(self, request, *arg, **kwargs): - # In some cases, the class can stay instanciated, so we need to reset the list - self.quick_notif_list = [] - return super().dispatch(request, *arg, **kwargs) - - def get_success_url(self): - ret = super().get_success_url() - if hasattr(self, "quick_notif_url_arg"): - if "?" in ret: - ret += "&" + self.quick_notif_url_arg - else: - ret += "?" + self.quick_notif_url_arg - return ret - - def get_context_data(self, **kwargs): - """Add quick notifications to context.""" - kwargs = super().get_context_data(**kwargs) - kwargs["quick_notifs"] = [] - for n in self.quick_notif_list: - kwargs["quick_notifs"].append(settings.SITH_QUICK_NOTIF[n]) - for key, val in settings.SITH_QUICK_NOTIF.items(): - for gk in self.request.GET: - if key == gk: - kwargs["quick_notifs"].append(val) - return kwargs - - class DetailFormView(SingleObjectMixin, FormView): """Class that allow both a detail view and a form view.""" @@ -314,14 +253,6 @@ class DetailFormView(SingleObjectMixin, FormView): return super().get_object() -class AllowFragment: - """Add `is_fragment` to templates. It's only True if the request is emitted by htmx""" - - def get_context_data(self, **kwargs): - kwargs["is_fragment"] = self.request.headers.get("HX-Request", False) - return super().get_context_data(**kwargs) - - # F403: those star-imports would be hellish to refactor # E402: putting those import at the top of the file would also be difficult from .files import * # noqa: F403 E402 diff --git a/core/views/files.py b/core/views/files.py index d5ffabb6..e2ccaa6b 100644 --- a/core/views/files.py +++ b/core/views/files.py @@ -35,12 +35,12 @@ from django.views.generic.edit import DeleteView, FormMixin, UpdateView from core.models import Notification, SithFile, User from core.views import ( - AllowFragment, CanEditMixin, CanEditPropMixin, CanViewMixin, can_view, ) +from core.views.mixins import AllowFragment from core.views.widgets.select import ( AutoCompleteSelectMultipleGroup, AutoCompleteSelectSithFile, diff --git a/core/views/mixins.py b/core/views/mixins.py new file mode 100644 index 00000000..9687f5d9 --- /dev/null +++ b/core/views/mixins.py @@ -0,0 +1,67 @@ +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured +from django.views import View + + +class TabedViewMixin(View): + """Basic functions for displaying tabs in the template.""" + + def get_tabs_title(self): + if hasattr(self, "tabs_title"): + return self.tabs_title + raise ImproperlyConfigured("tabs_title is required") + + def get_current_tab(self): + if hasattr(self, "current_tab"): + return self.current_tab + raise ImproperlyConfigured("current_tab is required") + + def get_list_of_tabs(self): + if hasattr(self, "list_of_tabs"): + return self.list_of_tabs + raise ImproperlyConfigured("list_of_tabs is required") + + def get_context_data(self, **kwargs): + kwargs = super().get_context_data(**kwargs) + kwargs["tabs_title"] = self.get_tabs_title() + kwargs["current_tab"] = self.get_current_tab() + kwargs["list_of_tabs"] = self.get_list_of_tabs() + return kwargs + + +class QuickNotifMixin: + quick_notif_list = [] + + def dispatch(self, request, *arg, **kwargs): + # In some cases, the class can stay instanciated, so we need to reset the list + self.quick_notif_list = [] + return super().dispatch(request, *arg, **kwargs) + + def get_success_url(self): + ret = super().get_success_url() + if hasattr(self, "quick_notif_url_arg"): + if "?" in ret: + ret += "&" + self.quick_notif_url_arg + else: + ret += "?" + self.quick_notif_url_arg + return ret + + def get_context_data(self, **kwargs): + """Add quick notifications to context.""" + kwargs = super().get_context_data(**kwargs) + kwargs["quick_notifs"] = [] + for n in self.quick_notif_list: + kwargs["quick_notifs"].append(settings.SITH_QUICK_NOTIF[n]) + for key, val in settings.SITH_QUICK_NOTIF.items(): + for gk in self.request.GET: + if key == gk: + kwargs["quick_notifs"].append(val) + return kwargs + + +class AllowFragment: + """Add `is_fragment` to templates. It's only True if the request is emitted by htmx""" + + def get_context_data(self, **kwargs): + kwargs["is_fragment"] = self.request.headers.get("HX-Request", False) + return super().get_context_data(**kwargs) diff --git a/core/views/user.py b/core/views/user.py index 264a8dd6..38966900 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -55,13 +55,7 @@ from django.views.generic.edit import FormView, UpdateView from honeypot.decorators import check_honeypot from core.models import Gift, Preferences, User -from core.views import ( - CanEditMixin, - CanEditPropMixin, - CanViewMixin, - QuickNotifMixin, - TabedViewMixin, -) +from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin from core.views.forms import ( GiftForm, LoginForm, @@ -70,6 +64,7 @@ from core.views.forms import ( UserGroupsForm, UserProfileForm, ) +from core.views.mixins import QuickNotifMixin, TabedViewMixin from counter.models import Refilling, Selling from counter.views.student_card import StudentCardFormView from eboutic.models import Invoice diff --git a/counter/views/mixins.py b/counter/views/mixins.py index b72d6694..5c01392a 100644 --- a/counter/views/mixins.py +++ b/counter/views/mixins.py @@ -19,7 +19,7 @@ from django.urls import reverse_lazy from django.utils.translation import gettext_lazy as _ from django.views.generic.base import View -from core.views import TabedViewMixin +from core.views.mixins import TabedViewMixin class CounterAdminMixin(View): diff --git a/pedagogy/views.py b/pedagogy/views.py index 99dd8168..c7ef0297 100644 --- a/pedagogy/views.py +++ b/pedagogy/views.py @@ -39,7 +39,6 @@ from core.models import Notification, User from core.views import ( CanCreateMixin, CanEditPropMixin, - CanViewMixin, DetailFormView, FormerSubscriberMixin, ) diff --git a/trombi/views.py b/trombi/views.py index c5a1d205..398b4dee 100644 --- a/trombi/views.py +++ b/trombi/views.py @@ -39,15 +39,9 @@ from django.views.generic.edit import CreateView, DeleteView, UpdateView from club.models import Club from core.models import User -from core.views import ( - CanCreateMixin, - CanEditMixin, - CanEditPropMixin, - CanViewMixin, - QuickNotifMixin, - TabedViewMixin, -) +from core.views import CanCreateMixin, CanEditMixin, CanEditPropMixin, CanViewMixin from core.views.forms import SelectDate +from core.views.mixins import QuickNotifMixin, TabedViewMixin from core.views.widgets.select import AutoCompleteSelectUser from trombi.models import Trombi, TrombiClubMembership, TrombiComment, TrombiUser