fix: Move mixin to dedicated module
This commit is contained in:
parent
a6638cda2c
commit
d8c5a513fa
2 changed files with 37 additions and 32 deletions
36
cof_clubs/mixins.py
Normal file
36
cof_clubs/mixins.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
from django.contrib.auth.mixins import AccessMixin
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
|
||||
from .models import Club, ClubBudgetAccountingPeriod
|
||||
|
||||
|
||||
class BudgetLineAccessMixin(AccessMixin):
|
||||
def get_club(self):
|
||||
return get_object_or_404(Club, pk=self.kwargs["club"])
|
||||
|
||||
def get_accounting_period(self):
|
||||
return get_object_or_404(
|
||||
ClubBudgetAccountingPeriod, pk=self.kwargs["acct_period"]
|
||||
)
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.club = self.get_club()
|
||||
if not self.request.user.is_authenticated or (
|
||||
not self.request.user.profile.is_buro
|
||||
and self.request.user not in self.club.respos.all()
|
||||
and self.request.user not in self.club.budget_managers.all()
|
||||
):
|
||||
return self.handle_no_permission()
|
||||
self.accounting_period = self.get_accounting_period()
|
||||
if self.accounting_period.is_archived:
|
||||
return self.handle_no_permission()
|
||||
if (
|
||||
not self.request.user.profile.is_buro
|
||||
and hasattr(self, "object")
|
||||
and self.object
|
||||
and self.object.posted
|
||||
):
|
||||
return self.handle_no_permission()
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
|
@ -3,7 +3,6 @@ from django.contrib.messages.views import SuccessMessageMixin
|
|||
from django.db import transaction
|
||||
from django.db.models import Sum
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.views.generic import DetailView, TemplateView
|
||||
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
|
||||
|
@ -16,6 +15,7 @@ from .forms import (
|
|||
ClubBudgetLineFormSet,
|
||||
ClubBudgetLineFullForm,
|
||||
)
|
||||
from .mixins import BudgetLineAccessMixin
|
||||
from .models import Club, ClubBudgetAccountingPeriod, ClubBudgetLine
|
||||
|
||||
|
||||
|
@ -144,37 +144,6 @@ class ClubDetailView(AccessMixin, DetailView):
|
|||
return ctx
|
||||
|
||||
|
||||
class BudgetLineAccessMixin(AccessMixin):
|
||||
def get_club(self):
|
||||
return get_object_or_404(Club, pk=self.kwargs["club"])
|
||||
|
||||
def get_accounting_period(self):
|
||||
return get_object_or_404(
|
||||
ClubBudgetAccountingPeriod, pk=self.kwargs["acct_period"]
|
||||
)
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.club = self.get_club()
|
||||
if (
|
||||
not self.request.user.profile.is_buro
|
||||
and self.request.user not in self.club.respos.all()
|
||||
and self.request.user not in self.club.budget_managers.all()
|
||||
):
|
||||
return self.handle_no_permission()
|
||||
self.accounting_period = self.get_accounting_period()
|
||||
if self.accounting_period.is_archived:
|
||||
return self.handle_no_permission()
|
||||
if (
|
||||
not self.request.user.profile.is_buro
|
||||
and hasattr(self, "object")
|
||||
and self.object
|
||||
and self.object.posted
|
||||
):
|
||||
return self.handle_no_permission()
|
||||
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
|
||||
class BudgetLineCreate(BudgetLineAccessMixin, CreateView):
|
||||
|
||||
model = ClubBudgetLine
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue