nk20/apps/sheets/views.py

137 lines
4.4 KiB
Python
Raw Normal View History

# Copyright (C) 2018-2022 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
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
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 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.kwargs['pk'],))
class FoodUpdateView(ProtectQuerysetMixin, UpdateView):
model = Food
form_class = FoodForm
extra_context = {"title": _("Update food")}
def get_success_url(self):
return reverse_lazy('sheets:sheet_detail', args=(self.kwargs['pk'],))
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,))