mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-10-31 17:13:08 +00:00 
			
		
		
		
	Move core views mixins to their own file
This commit is contained in:
		| @@ -45,14 +45,9 @@ from accounting.widgets.select import ( | |||||||
| from club.models import Club | from club.models import Club | ||||||
| from club.widgets.select import AutoCompleteSelectClub | from club.widgets.select import AutoCompleteSelectClub | ||||||
| from core.models import User | from core.models import User | ||||||
| from core.views import ( | from core.views import CanCreateMixin, CanEditMixin, CanEditPropMixin, CanViewMixin | ||||||
|     CanCreateMixin, |  | ||||||
|     CanEditMixin, |  | ||||||
|     CanEditPropMixin, |  | ||||||
|     CanViewMixin, |  | ||||||
|     TabedViewMixin, |  | ||||||
| ) |  | ||||||
| from core.views.forms import SelectDate, SelectFile | from core.views.forms import SelectDate, SelectFile | ||||||
|  | from core.views.mixins import TabedViewMixin | ||||||
| from core.views.widgets.select import AutoCompleteSelectUser | from core.views.widgets.select import AutoCompleteSelectUser | ||||||
| from counter.models import Counter, Product, Selling | from counter.models import Counter, Product, Selling | ||||||
|  |  | ||||||
|   | |||||||
| @@ -58,8 +58,8 @@ from core.views import ( | |||||||
|     CanViewMixin, |     CanViewMixin, | ||||||
|     DetailFormView, |     DetailFormView, | ||||||
|     PageEditViewBase, |     PageEditViewBase, | ||||||
|     TabedViewMixin, |  | ||||||
| ) | ) | ||||||
|  | from core.views.mixins import TabedViewMixin | ||||||
| from counter.models import Selling | from counter.models import Selling | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								com/views.py
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								com/views.py
									
									
									
									
									
								
							| @@ -27,10 +27,7 @@ from smtplib import SMTPRecipientsRefused | |||||||
| from typing import Any | from typing import Any | ||||||
|  |  | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
| from django.contrib.auth.mixins import ( | from django.contrib.auth.mixins import AccessMixin, PermissionRequiredMixin | ||||||
|     AccessMixin, |  | ||||||
|     PermissionRequiredMixin, |  | ||||||
| ) |  | ||||||
| from django.core.exceptions import PermissionDenied, ValidationError | from django.core.exceptions import PermissionDenied, ValidationError | ||||||
| from django.db.models import Max | from django.db.models import Max | ||||||
| from django.forms.models import modelform_factory | 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.forms import NewsDateForm, NewsForm, PosterForm | ||||||
| from com.models import News, NewsDate, Poster, Screen, Sith, Weekmail, WeekmailArticle | from com.models import News, NewsDate, Poster, Screen, Sith, Weekmail, WeekmailArticle | ||||||
| from core.models import User | from core.models import User | ||||||
| from core.views import ( | from core.views import CanEditPropMixin, CanViewMixin, QuickNotifMixin, TabedViewMixin | ||||||
|     CanEditPropMixin, | from core.views.mixins import QuickNotifMixin, TabedViewMixin | ||||||
|     CanViewMixin, |  | ||||||
|     QuickNotifMixin, |  | ||||||
|     TabedViewMixin, |  | ||||||
| ) |  | ||||||
| from core.views.widgets.markdown import MarkdownInput | from core.views.widgets.markdown import MarkdownInput | ||||||
|  |  | ||||||
| # Sith object | # Sith object | ||||||
|   | |||||||
| @@ -25,18 +25,13 @@ | |||||||
| import types | import types | ||||||
| from typing import Any | from typing import Any | ||||||
|  |  | ||||||
| from django.conf import settings |  | ||||||
| from django.contrib.auth.mixins import AccessMixin | from django.contrib.auth.mixins import AccessMixin | ||||||
| from django.core.exceptions import ( | from django.core.exceptions import PermissionDenied | ||||||
|     ImproperlyConfigured, |  | ||||||
|     PermissionDenied, |  | ||||||
| ) |  | ||||||
| from django.http import ( | from django.http import ( | ||||||
|     HttpResponseForbidden, |     HttpResponseForbidden, | ||||||
|     HttpResponseNotFound, |     HttpResponseNotFound, | ||||||
|     HttpResponseServerError, |     HttpResponseServerError, | ||||||
| ) | ) | ||||||
| from django.shortcuts import render |  | ||||||
| from django.utils.functional import cached_property | from django.utils.functional import cached_property | ||||||
| from django.views.generic.base import View | from django.views.generic.base import View | ||||||
| from django.views.generic.detail import SingleObjectMixin | from django.views.generic.detail import SingleObjectMixin | ||||||
| @@ -245,62 +240,6 @@ class SubscriberMixin(AccessMixin): | |||||||
|         return super().dispatch(request, *args, **kwargs) |         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 DetailFormView(SingleObjectMixin, FormView): | ||||||
|     """Class that allow both a detail view and a form view.""" |     """Class that allow both a detail view and a form view.""" | ||||||
|  |  | ||||||
| @@ -314,14 +253,6 @@ class DetailFormView(SingleObjectMixin, FormView): | |||||||
|         return super().get_object() |         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 | # F403: those star-imports would be hellish to refactor | ||||||
| # E402: putting those import at the top of the file would also be difficult | # E402: putting those import at the top of the file would also be difficult | ||||||
| from .files import *  # noqa: F403 E402 | from .files import *  # noqa: F403 E402 | ||||||
|   | |||||||
| @@ -35,12 +35,12 @@ from django.views.generic.edit import DeleteView, FormMixin, UpdateView | |||||||
|  |  | ||||||
| from core.models import Notification, SithFile, User | from core.models import Notification, SithFile, User | ||||||
| from core.views import ( | from core.views import ( | ||||||
|     AllowFragment, |  | ||||||
|     CanEditMixin, |     CanEditMixin, | ||||||
|     CanEditPropMixin, |     CanEditPropMixin, | ||||||
|     CanViewMixin, |     CanViewMixin, | ||||||
|     can_view, |     can_view, | ||||||
| ) | ) | ||||||
|  | from core.views.mixins import AllowFragment | ||||||
| from core.views.widgets.select import ( | from core.views.widgets.select import ( | ||||||
|     AutoCompleteSelectMultipleGroup, |     AutoCompleteSelectMultipleGroup, | ||||||
|     AutoCompleteSelectSithFile, |     AutoCompleteSelectSithFile, | ||||||
|   | |||||||
							
								
								
									
										67
									
								
								core/views/mixins.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								core/views/mixins.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||||
| @@ -55,13 +55,7 @@ from django.views.generic.edit import FormView, UpdateView | |||||||
| from honeypot.decorators import check_honeypot | from honeypot.decorators import check_honeypot | ||||||
|  |  | ||||||
| from core.models import Gift, Preferences, User | from core.models import Gift, Preferences, User | ||||||
| from core.views import ( | from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin | ||||||
|     CanEditMixin, |  | ||||||
|     CanEditPropMixin, |  | ||||||
|     CanViewMixin, |  | ||||||
|     QuickNotifMixin, |  | ||||||
|     TabedViewMixin, |  | ||||||
| ) |  | ||||||
| from core.views.forms import ( | from core.views.forms import ( | ||||||
|     GiftForm, |     GiftForm, | ||||||
|     LoginForm, |     LoginForm, | ||||||
| @@ -70,6 +64,7 @@ from core.views.forms import ( | |||||||
|     UserGroupsForm, |     UserGroupsForm, | ||||||
|     UserProfileForm, |     UserProfileForm, | ||||||
| ) | ) | ||||||
|  | from core.views.mixins import QuickNotifMixin, TabedViewMixin | ||||||
| from counter.models import Refilling, Selling | from counter.models import Refilling, Selling | ||||||
| from counter.views.student_card import StudentCardFormView | from counter.views.student_card import StudentCardFormView | ||||||
| from eboutic.models import Invoice | from eboutic.models import Invoice | ||||||
|   | |||||||
| @@ -19,7 +19,7 @@ from django.urls import reverse_lazy | |||||||
| from django.utils.translation import gettext_lazy as _ | from django.utils.translation import gettext_lazy as _ | ||||||
| from django.views.generic.base import View | from django.views.generic.base import View | ||||||
|  |  | ||||||
| from core.views import TabedViewMixin | from core.views.mixins import TabedViewMixin | ||||||
|  |  | ||||||
|  |  | ||||||
| class CounterAdminMixin(View): | class CounterAdminMixin(View): | ||||||
|   | |||||||
| @@ -39,7 +39,6 @@ from core.models import Notification, User | |||||||
| from core.views import ( | from core.views import ( | ||||||
|     CanCreateMixin, |     CanCreateMixin, | ||||||
|     CanEditPropMixin, |     CanEditPropMixin, | ||||||
|     CanViewMixin, |  | ||||||
|     DetailFormView, |     DetailFormView, | ||||||
|     FormerSubscriberMixin, |     FormerSubscriberMixin, | ||||||
| ) | ) | ||||||
|   | |||||||
| @@ -39,15 +39,9 @@ from django.views.generic.edit import CreateView, DeleteView, UpdateView | |||||||
|  |  | ||||||
| from club.models import Club | from club.models import Club | ||||||
| from core.models import User | from core.models import User | ||||||
| from core.views import ( | from core.views import CanCreateMixin, CanEditMixin, CanEditPropMixin, CanViewMixin | ||||||
|     CanCreateMixin, |  | ||||||
|     CanEditMixin, |  | ||||||
|     CanEditPropMixin, |  | ||||||
|     CanViewMixin, |  | ||||||
|     QuickNotifMixin, |  | ||||||
|     TabedViewMixin, |  | ||||||
| ) |  | ||||||
| from core.views.forms import SelectDate | from core.views.forms import SelectDate | ||||||
|  | from core.views.mixins import QuickNotifMixin, TabedViewMixin | ||||||
| from core.views.widgets.select import AutoCompleteSelectUser | from core.views.widgets.select import AutoCompleteSelectUser | ||||||
| from trombi.models import Trombi, TrombiClubMembership, TrombiComment, TrombiUser | from trombi.models import Trombi, TrombiClubMembership, TrombiComment, TrombiUser | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user