hackens-orga/hackens_orga/frontend/views.py

79 lines
2.8 KiB
Python
Raw Normal View History

2023-02-16 19:11:33 +01:00
"""
Frontend views for the app
"""
2023-03-06 11:58:53 +01:00
from agent.models import Agent
2023-02-16 19:11:33 +01:00
from budget.models import BudgetGroup, BudgetLine
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.db.models import Model
from django.urls import reverse
2023-02-08 03:30:11 +01:00
from django.views.generic import (CreateView, DeleteView, DetailView, ListView,
TemplateView, UpdateView)
2023-02-08 02:04:34 +01:00
2023-02-08 03:30:11 +01:00
class BudgetListView(ListView):
model = BudgetGroup
template_name = "frontend/budget_list.html"
2023-02-16 19:11:33 +01:00
ordering = "-name"
class BudgetGroupCreateView(SuccessMessageMixin, PermissionRequiredMixin, CreateView):
model = BudgetGroup
fields = ["name", "description"]
template_name = "frontend/budgetgroup_create.html"
success_message = "Le budget %(name)s a été créé avec succès"
permission_required = "budget.add_budgetgroup"
def get_success_url(self):
return reverse("frontend:budget")
class BudgetGroupUpdateView(SuccessMessageMixin, PermissionRequiredMixin, UpdateView):
model = BudgetGroup
fields = ["name", "description"]
template_name = "frontend/budgetgroup_create.html"
success_message = "Le budget %(name)s a été mis à jour avec succès"
permission_required = "budget.change_budgetgroup"
def get_success_url(self):
return reverse("frontend:budget")
class BudgetLineCreateView(SuccessMessageMixin, PermissionRequiredMixin, CreateView):
model = BudgetLine
2023-03-06 11:58:53 +01:00
fields = ["title", "comment", "amount", "author", "date", "group"]
2023-02-16 19:11:33 +01:00
template_name = "frontend/budgetline_create.html"
success_message = 'La dépense/recette "%(title)s" a été créée avec succès'
permission_required = "budget.add_budgetline"
def get_initial(self):
if "groupid" in self.request.GET:
try:
gid = int(self.request.GET["groupid"])
except ValueError:
gid = None
if gid is not None:
try:
grp = BudgetGroup.objects.get(pk=gid)
except Model.DoesNotExist:
grp = None
else:
grp = None
agents = Agent.objects.filter(user=self.user)
2023-03-06 11:58:53 +01:00
return {"author": agents[0] if len(agents) >= 1 else None, "group": grp}
2023-02-16 19:11:33 +01:00
def get_success_url(self):
return reverse("frontend:budget")
class BudgetLineUpdateView(SuccessMessageMixin, UpdateView, PermissionRequiredMixin):
model = BudgetLine
2023-03-06 11:58:53 +01:00
fields = ["title", "comment", "amount", "author", "date", "group"]
2023-02-16 19:11:33 +01:00
template_name = "frontend/budgetline_update.html"
success_message = 'La dépense/recette "%(title)s" a été mise à jour avec succès'
permission_required = "budget.change_budgetline"
def get_success_url(self):
return reverse("frontend:budget")