mirror of https://gitlab.crans.org/bde/nk20
191 lines
6.6 KiB
Python
191 lines
6.6 KiB
Python
# Copyright (C) 2018-2022 by BDE ENS Paris-Saclay
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
from crispy_forms.helper import FormHelper
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.urls import reverse_lazy
|
|
from django.utils import timezone
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.views.generic import DetailView, UpdateView
|
|
from django_tables2 import SingleTableView
|
|
|
|
from permission.backends import PermissionBackend
|
|
from permission.views import ProtectQuerysetMixin, ProtectedCreateView
|
|
|
|
from .forms import FoodForm, MealForm, SheetForm, FoodOptionsFormSet, FoodOptionFormSetHelper
|
|
from .models import Sheet, Food, Meal
|
|
from .tables import SheetTable
|
|
|
|
|
|
class SheetListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
|
|
model = Sheet
|
|
table_class = SheetTable
|
|
ordering = '-date'
|
|
extra_context = {"title": _("Search note sheet")}
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["can_create_sheet"] = PermissionBackend.check_perm(self.request, "sheets.add_sheet", Sheet(
|
|
name="Test",
|
|
date=timezone.now(),
|
|
description="Test sheet",
|
|
))
|
|
return context
|
|
|
|
|
|
class SheetCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
|
model = Sheet
|
|
form_class = SheetForm
|
|
extra_context = {"title": _("Create note sheet")}
|
|
|
|
def get_sample_object(self):
|
|
return Sheet(
|
|
name="Test",
|
|
date=timezone.now(),
|
|
description="Test",
|
|
)
|
|
|
|
|
|
class SheetUpdateView(ProtectQuerysetMixin, UpdateView):
|
|
model = Sheet
|
|
form_class = SheetForm
|
|
extra_context = {"title": _("Update note sheet")}
|
|
|
|
|
|
class SheetDetailView(ProtectQuerysetMixin, DetailView):
|
|
model = Sheet
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data()
|
|
|
|
context['can_change_sheet'] = PermissionBackend.check_perm(self.request, 'sheets.change_sheet', self.object)
|
|
context['can_add_meal'] = PermissionBackend.check_perm(self.request,
|
|
'sheets.add_meal',
|
|
Meal(sheet=self.object, name="Test", price=500))
|
|
context['can_add_food'] = PermissionBackend.check_perm(self.request,
|
|
'sheets.add_food',
|
|
Food(sheet=self.object, name="Test", price=500))
|
|
|
|
return context
|
|
|
|
|
|
class FoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
|
model = Food
|
|
form_class = FoodForm
|
|
extra_context = {"title": _("Create new food")}
|
|
|
|
def get_sample_object(self):
|
|
return Food(
|
|
sheet_id=self.kwargs['pk'],
|
|
name="Test",
|
|
price=500,
|
|
)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
form = context['form']
|
|
form.helper = FormHelper()
|
|
# Remove form tag on the generation of the form in the template (already present on the template)
|
|
form.helper.form_tag = False
|
|
# The formset handles the set of the products
|
|
form_set = FoodOptionsFormSet(instance=form.instance)
|
|
context['formset'] = form_set
|
|
context['helper'] = FoodOptionFormSetHelper()
|
|
|
|
return context
|
|
|
|
def form_valid(self, form):
|
|
form.instance.sheet_id = self.kwargs['pk']
|
|
|
|
# For each product, we save it
|
|
formset = FoodOptionsFormSet(self.request.POST, instance=form.instance)
|
|
if formset.is_valid():
|
|
for f in formset:
|
|
# We don't save the product if the designation is not entered, ie. if the line is empty
|
|
if f.is_valid() and f.instance.name:
|
|
f.save()
|
|
f.instance.save()
|
|
else:
|
|
f.instance = None
|
|
|
|
return super().form_valid(form)
|
|
|
|
def get_success_url(self):
|
|
return reverse_lazy('sheets:sheet_detail', args=(self.kwargs['pk'],))
|
|
|
|
|
|
class FoodUpdateView(ProtectQuerysetMixin, UpdateView):
|
|
model = Food
|
|
form_class = FoodForm
|
|
extra_context = {"title": _("Update food")}
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
form = context['form']
|
|
form.helper = FormHelper()
|
|
# Remove form tag on the generation of the form in the template (already present on the template)
|
|
form.helper.form_tag = False
|
|
# The formset handles the set of the products
|
|
form_set = FoodOptionsFormSet(instance=form.instance)
|
|
context['formset'] = form_set
|
|
context['helper'] = FoodOptionFormSetHelper()
|
|
|
|
return context
|
|
|
|
def form_valid(self, form):
|
|
# For each product, we save it
|
|
formset = FoodOptionsFormSet(self.request.POST, instance=form.instance)
|
|
if formset.is_valid():
|
|
for f in formset:
|
|
# We don't save the product if the designation is not entered, ie. if the line is empty
|
|
if f.is_valid() and f.instance.name:
|
|
f.save()
|
|
f.instance.save()
|
|
else:
|
|
f.instance = None
|
|
|
|
return super().form_valid(form)
|
|
|
|
def get_success_url(self):
|
|
return reverse_lazy('sheets:sheet_detail', args=(self.object.sheet_id,))
|
|
|
|
|
|
class MealCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
|
model = Meal
|
|
form_class = MealForm
|
|
extra_context = {"title": _("Create new meal")}
|
|
|
|
def get_sample_object(self):
|
|
return Meal(
|
|
sheet_id=self.kwargs['pk'],
|
|
name="Test",
|
|
price=500,
|
|
)
|
|
|
|
def get_form(self, form_class=None):
|
|
form = super().get_form(form_class)
|
|
form.fields['content'].queryset = form.fields['content'].queryset.filter(sheet_id=self.kwargs['pk'])
|
|
return form
|
|
|
|
def form_valid(self, form):
|
|
form.instance.sheet_id = self.kwargs['pk']
|
|
return super().form_valid(form)
|
|
|
|
def get_success_url(self):
|
|
return reverse_lazy('sheets:sheet_detail', args=(self.object.sheet_id,))
|
|
|
|
|
|
class MealUpdateView(ProtectQuerysetMixin, UpdateView):
|
|
model = Meal
|
|
form_class = MealForm
|
|
extra_context = {"title": _("Update meal")}
|
|
|
|
def get_form(self, form_class=None):
|
|
form = super().get_form(form_class)
|
|
form.fields['content'].queryset = form.fields['content'].queryset.filter(sheet=self.object.sheet)
|
|
return form
|
|
|
|
def get_success_url(self):
|
|
return reverse_lazy('sheets:sheet_detail', args=(self.object.sheet_id,))
|