Sith/counter/views/mixins.py

122 lines
3.6 KiB
Python
Raw Normal View History

#
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
2024-09-23 08:25:27 +00:00
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
2024-06-24 11:07:36 +00:00
from django.conf import settings
from django.core.exceptions import PermissionDenied
2024-11-27 17:48:06 +00:00
from django.urls import reverse_lazy
2024-06-24 11:07:36 +00:00
from django.utils.translation import gettext_lazy as _
2017-04-03 09:57:28 +00:00
from django.views.generic.base import View
2016-03-28 12:54:35 +00:00
2024-11-27 17:48:06 +00:00
from core.views import TabedViewMixin
2024-10-06 11:22:09 +00:00
2017-06-12 07:47:24 +00:00
2017-04-04 13:45:02 +00:00
class CounterAdminMixin(View):
2024-07-12 07:34:16 +00:00
"""Protect counter admin section."""
2018-10-04 19:29:19 +00:00
2017-04-03 13:00:13 +00:00
edit_group = [settings.SITH_GROUP_COUNTER_ADMIN_ID]
edit_club = []
2017-04-03 11:50:28 +00:00
def _test_group(self, user):
return any(user.is_in_group(pk=grp_id) for grp_id in self.edit_group)
2017-04-03 11:50:28 +00:00
2017-04-03 13:00:13 +00:00
def _test_club(self, user):
return any(c.can_be_edited_by(user) for c in self.edit_club)
2017-04-03 13:00:13 +00:00
2017-04-03 09:57:28 +00:00
def dispatch(self, request, *args, **kwargs):
2018-10-04 19:29:19 +00:00
if not (
request.user.is_root
or self._test_group(request.user)
or self._test_club(request.user)
):
2017-04-03 09:57:28 +00:00
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2017-04-03 09:57:28 +00:00
2017-06-12 07:47:24 +00:00
2016-09-28 09:07:32 +00:00
class CounterTabsMixin(TabedViewMixin):
def get_tabs_title(self):
2024-09-18 12:49:40 +00:00
return self.object
2017-06-12 07:47:24 +00:00
2016-09-28 09:07:32 +00:00
def get_list_of_tabs(self):
2024-09-18 12:49:40 +00:00
tab_list = [
2018-10-04 19:29:19 +00:00
{
"url": reverse_lazy(
2024-09-18 12:49:40 +00:00
"counter:details", kwargs={"counter_id": self.object.id}
2018-10-04 19:29:19 +00:00
),
"slug": "counter",
"name": _("Counter"),
}
2024-09-18 12:49:40 +00:00
]
if self.object.type == "BAR":
2018-10-04 19:29:19 +00:00
tab_list.append(
{
"url": reverse_lazy(
2024-09-18 12:49:40 +00:00
"counter:cash_summary", kwargs={"counter_id": self.object.id}
2018-10-04 19:29:19 +00:00
),
"slug": "cash_summary",
"name": _("Cash summary"),
}
)
tab_list.append(
{
"url": reverse_lazy(
2024-09-18 12:49:40 +00:00
"counter:last_ops", kwargs={"counter_id": self.object.id}
2018-10-04 19:29:19 +00:00
),
"slug": "last_ops",
"name": _("Last operations"),
}
)
2016-09-28 09:07:32 +00:00
return tab_list
2017-06-12 07:47:24 +00:00
2016-09-28 09:07:32 +00:00
class CounterAdminTabsMixin(TabedViewMixin):
tabs_title = _("Counter administration")
list_of_tabs = [
2017-06-12 07:47:24 +00:00
{
2018-10-04 19:29:19 +00:00
"url": reverse_lazy("counter:admin_list"),
"slug": "counters",
"name": _("Counters"),
2017-06-12 07:47:24 +00:00
},
{
2018-10-04 19:29:19 +00:00
"url": reverse_lazy("counter:product_list"),
"slug": "products",
"name": _("Products"),
2017-06-12 07:47:24 +00:00
},
{
2018-10-04 19:29:19 +00:00
"url": reverse_lazy("counter:product_list_archived"),
"slug": "archive",
"name": _("Archived products"),
2017-06-12 07:47:24 +00:00
},
{
2024-12-18 11:16:24 +00:00
"url": reverse_lazy("counter:product_type_list"),
2018-10-04 19:29:19 +00:00
"slug": "product_types",
"name": _("Product types"),
2017-06-12 07:47:24 +00:00
},
{
2018-10-04 19:29:19 +00:00
"url": reverse_lazy("counter:cash_summary_list"),
"slug": "cash_summary",
"name": _("Cash register summaries"),
2017-06-12 07:47:24 +00:00
},
{
2018-10-04 19:29:19 +00:00
"url": reverse_lazy("counter:invoices_call"),
"slug": "invoices_call",
"name": _("Invoices call"),
2017-06-12 07:47:24 +00:00
},
{
2018-10-04 19:29:19 +00:00
"url": reverse_lazy("counter:eticket_list"),
"slug": "etickets",
"name": _("Etickets"),
2017-06-12 07:47:24 +00:00
},
]