From 196df1e77565b07a1f7480e01b8b0dc43923cd0a Mon Sep 17 00:00:00 2001 From: quark Date: Tue, 13 Aug 2024 02:07:32 +0200 Subject: [PATCH] Remove initial.json (food) mandatory allergen are directly created in migration. Edit tables.py and views.py transformedfoodlist.html to improve/change the view. Edit base.html, urls.py to correct little mistakes. Edit initial.json (permission) to begin permission for food apps and create a new role (Respo Bouffe). --- apps/food/fixtures/initial.json | 107 ------------------ apps/food/tables.py | 2 +- .../templates/food/transformedfood_list.html | 46 +++++++- apps/food/urls.py | 2 +- apps/food/views.py | 52 ++++++--- apps/permission/fixtures/initial.json | 81 +++++++++++-- note_kfet/templates/base.html | 2 +- 7 files changed, 154 insertions(+), 138 deletions(-) delete mode 100644 apps/food/fixtures/initial.json diff --git a/apps/food/fixtures/initial.json b/apps/food/fixtures/initial.json deleted file mode 100644 index c91a2dec..00000000 --- a/apps/food/fixtures/initial.json +++ /dev/null @@ -1,107 +0,0 @@ -[ - { - "model": "food.allergen", - "pk": 1, - "fields": { - "name": "alcohol" - } - }, - { - "model": "food.allergen", - "pk": 2, - "fields": { - "name": "celery" - } - }, - { - "model": "food.allergen", - "pk": 3, - "fields": { - "name": "crustecean" - } - }, - { - "model": "food.allergen", - "pk": 4, - "fields": { - "name": "egg" - } - }, - { - "model": "food.allergen", - "pk": 5, - "fields": { - "name": "fish" - } - }, - { - "model": "food.allergen", - "pk": 6, - "fields": { - "name": "gluten" - } - }, - { - "model": "food.allergen", - "pk": 7, - "fields": { - "name": "groundnut" - } - }, - { - "model": "food.allergen", - "pk": 8, - "fields": { - "name": "lupine" - } - }, - { - "model": "food.allergen", - "pk": 9, - "fields": { - "name": "milk" - } - }, - { - "model": "food.allergen", - "pk": 10, - "fields": { - "name": "mollusc" - } - }, - { - "model": "food.allergen", - "pk": 11, - "fields": { - "name": "mustard" - } - }, - { - "model": "food.allergen", - "pk": 12, - "fields": { - "name": "nut" - } - }, - { - "model": "food.allergen", - "pk": 13, - "fields": { - "name": "sesame" - } - }, - { - "model": "food.allergen", - "pk": 14, - "fields": { - "name": "soy" - } - }, - { - "model": "food.allergen", - "pk": 15, - "fields": { - "name": "sulphite" - } - } -] diff --git a/apps/food/tables.py b/apps/food/tables.py index c824e42e..4a180c76 100644 --- a/apps/food/tables.py +++ b/apps/food/tables.py @@ -16,4 +16,4 @@ class TransformedFoodTable(tables.Table): class Meta: model = TransformedFood template_name = 'django_tables2/bootstrap4.html' - fields = ('name', ) + fields = ('name', "owner", "allergens", "expiry_date") diff --git a/apps/food/templates/food/transformedfood_list.html b/apps/food/templates/food/transformedfood_list.html index 2c4cb93d..fea2dd30 100644 --- a/apps/food/templates/food/transformedfood_list.html +++ b/apps/food/templates/food/transformedfood_list.html @@ -7,18 +7,54 @@ SPDX-License-Identifier: GPL-3.0-or-later {% block content %}
+

+ {% trans "Meal served" %} +

+ {% if can_create_meal %} + {% endif %} + {% if served.data %} + {% render_table served %} + {% else %} +
+
+ {% trans "There is no meal served." %} +
+
+ {% endif %} +
+ +

- {% trans 'In preparation' %} + {% trans "Open" %}

+ {% if open.data %} + {% render_table open %} + {% else %} +
class="card-body"> +
+ {% trans "There is no free meal." %} +
+
+ {% endif %} +
+ +
+

+ {% trans "All meals" %} +

+ {% if table.data %} {% render_table table %} -

- {% trans 'Free' %} -

- {% render_table open_table %} + {% else %} +
class="card-body"> +
+ {% trans "There is no meal." %} +
+
+ {% endif %}
{% endblock %} diff --git a/apps/food/urls.py b/apps/food/urls.py index 76527033..59640bae 100644 --- a/apps/food/urls.py +++ b/apps/food/urls.py @@ -8,7 +8,7 @@ from . import views app_name = 'food' urlpatterns = [ - path('', views.TransfomedListView.as_view(), name='food_list'), + path('', views.TransformedListView.as_view(), name='food_list'), path('', views.QRCodeView.as_view(), name='qrcode_view'), path('detail/', views.FoodView.as_view(), name='food_view'), diff --git a/apps/food/views.py b/apps/food/views.py index 15050d9e..10f296f7 100644 --- a/apps/food/views.py +++ b/apps/food/views.py @@ -4,12 +4,16 @@ from django.db import transaction from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponseRedirect -from django_tables2.views import SingleTableView +from django_tables2.views import MultiTableMixin from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django.utils import timezone from django.views.generic import DetailView, UpdateView, TemplateView +from django.views.generic.list import ListView +from permission.backends import PermissionBackend from permission.views import ProtectQuerysetMixin, ProtectedCreateView +from member.models import Club +from note_kfet.middlewares import get_current_request from .forms import AddIngredientForms, BasicFoodForms, QRCodeForms, TransformedFoodForms from .models import BasicFood, Food, QRCode, TransformedFood @@ -253,26 +257,46 @@ class TransformedFoodCreateView(TransformedFoodFormView, ProtectedCreateView): ) -class TransfomedListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): +class TransformedListView(ProtectQuerysetMixin, LoginRequiredMixin, MultiTableMixin, ListView): """ - Displays not ready TransformedFood + Displays ready TransformedFood """ model = TransformedFood - table_class = TransformedFoodTable - ordering = ('name',) + tables = [TransformedFoodTable, TransformedFoodTable, TransformedFoodTable] extra_context = {"title": _("Transformed food")} def get_queryset(self, **kwargs): - return super().get_queryset(**kwargs)\ - .filter(is_ready=False)\ - .distinct() + return super().get_queryset(**kwargs).distinct() + + def get_tables(self): + tables = super().get_tables() + + tables[0].prefix = "all-" + tables[1].prefix = "open-" + tables[2].prefix = "served-" + return tables + + def get_tables_data(self): + # first table = all transformed food, second table = free, third = served + return [ + self.get_queryset().order_by("-creation_date"), + TransformedFood.objects.filter(is_ready=True,is_active=True,was_eaten=False,expiry_date__lt=timezone.now()) + .filter(PermissionBackend.filter_queryset(self.request, TransformedFood, "view")) + .distinct() + .order_by("-creation_date"), + TransformedFood.objects.filter(is_ready=True,is_active=True,was_eaten=False,expiry_date__gte=timezone.now()) + .filter(PermissionBackend.filter_queryset(self.request, TransformedFood, "view")) + .distinct() + .order_by("-creation_date") + ] def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context['open_table'] = TransformedFoodTable( - TransformedFood.objects.filter( - was_eaten=False, - expiry_date__lt=timezone.now() - ), - prefix="open-") + + # context["can_create_meal"] = PermissionBackend.check_perm(self.request, "food.add_transformedfood", TransformedFood(creation_date = timezone.now(), name = "", expiry_date = timezone.now(), owner = )) <- défi prendre un club qui fonctionne (s'il existe) pour l'utilisateur + context["can_create_meal"] = True + + tables = context["tables"] + for name, table in zip(["table", "open", "served"], tables): + context[name] = table return context diff --git a/apps/permission/fixtures/initial.json b/apps/permission/fixtures/initial.json index b59fdb61..92ab6340 100644 --- a/apps/permission/fixtures/initial.json +++ b/apps/permission/fixtures/initial.json @@ -3111,6 +3111,54 @@ "description": "Voir ceux nous ayant pour ami, pour toujours" } }, + { + "model": "permission.permission", + "pk": 199, + "fields": { + "model": [ + "food", + "transformedfood" + ], + "query": "", + "type": "view", + "mask": 3, + "field": "", + "permanent": false, + "description": "Voir tout les plats" + } + }, + { + "model": "permission.permission", + "pk": 200, + "fields": { + "model": [ + "food", + "transformedfood" + ], + "query": "{\"owner\": \"club\"}", + "type": "view", + "mask": 3, + "field": "", + "permanent": false, + "description": "Voir tout les plats de son club" + } + }, + { + "model": "permission.permission", + "pk": 200, + "fields": { + "model": [ + "food", + "transformedfood" + ], + "query": "{\"is_ready\": true, \"is_active\": true, \"was_eaten\": false}", + "type": "view", + "mask": 1, + "field": "", + "permanent": false, + "description": "Voir les plats préparés actifs servis" + } + }, { "model": "permission.role", "pk": 1, @@ -3148,11 +3196,11 @@ 187, 188, 189, - 190, - 191, - 195, - 196, - 198 + 190, + 191, + 195, + 196, + 198 ] } }, @@ -3190,7 +3238,8 @@ 157, 158, 159, - 160 + 160, + 200 ] } }, @@ -3216,7 +3265,8 @@ 49, 50, 141, - 169 + 169, + 199 ] } }, @@ -3390,7 +3440,8 @@ 166, 167, 168, - 182 + 182, + 199 ] } }, @@ -3594,7 +3645,8 @@ 168, 176, 177, - 197 + 197, + 199 ] } }, @@ -3612,6 +3664,17 @@ ] } }, + { + "model": "permission.role", + "pk": 22, + "fields": { + "for_club": 2, + "name": "Respo Bouffe", + "permissions": [ + 199 + ] + } + }, { "model": "wei.weirole", "pk": 12, diff --git a/note_kfet/templates/base.html b/note_kfet/templates/base.html index 15743018..9f5ae867 100644 --- a/note_kfet/templates/base.html +++ b/note_kfet/templates/base.html @@ -69,7 +69,7 @@ SPDX-License-Identifier: GPL-3.0-or-later {% if request.user.is_authenticated %} {% endif %} {% if user.is_authenticated and user|is_member:"Kfet" %}