mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-10-26 05:23:18 +01:00 
			
		
		
		
	Compare commits
	
		
			212 Commits
		
	
	
		
			New_permis
			...
			424aebbf3a
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 424aebbf3a | ||
|  | 4975c1ab6f | ||
|  | 61999a31a5 | ||
|  | b217f7ceec | ||
|  | 2755a5f7ab | ||
|  | 9ab4df94e6 | ||
|  | edb6abfff5 | ||
|  | 03c1bb41b6 | ||
|  | f03c13a4b8 | ||
|  | b1fa1c2cdd | ||
|  | a273dc3eef | ||
|  | 852651d126 | ||
|  | 3af35dc0fc | ||
|  | 4380414c6b | ||
|  | a94c937c6a | ||
|  | 0a261e6ad5 | ||
|  | ab9329f62b | ||
|  | b97b79e2ea | ||
|  | 483ea26f02 | ||
|  | 695ce63e08 | ||
|  | 79f50c27f1 | ||
|  | 5989721bc9 | ||
|  | bcc3e7cc53 | ||
|  | 608804db30 | ||
|  | 82a06c29dd | ||
|  | cf9d208586 | ||
|  | 432f50e49a | ||
|  | 883589e08c | ||
|  | c36f8c25a2 | ||
|  | 8783a63d7f | ||
|  | 4cc43fe4b6 | ||
|  | b7c0986a5f | ||
|  | 85ea43a7cf | ||
|  | f54dd30482 | ||
|  | 7eafe33945 | ||
|  | 6edef619aa | ||
|  | 8a1f30ebe2 | ||
|  | b2c6b0e85d | ||
|  | 1567bc6ce5 | ||
|  | c411197af3 | ||
|  | bc517f02e5 | ||
|  | e83ee8015f | ||
|  | c26534b6b7 | ||
|  | cdc6f0a3f8 | ||
|  | c153d5f10a | ||
|  | 3f76ca6472 | ||
|  | 5c5f579729 | ||
|  | a6df0e7c69 | ||
|  | 763535bea4 | ||
|  | df0d886db9 | ||
|  | 092cc37320 | ||
|  | 16b55e23af | ||
|  | 97621e8704 | ||
|  | cf4c23d1ac | ||
|  | d71105976f | ||
|  | 89cc03141b | ||
|  | 6822500fdc | ||
|  | 63f6528adc | ||
|  | 40ac1daece | ||
|  | e617048332 | ||
|  | 9eb6edb37d | ||
|  | 70a57bf02d | ||
|  | 02453e07ba | ||
|  | 4479e8f97a | ||
|  | a351415494 | ||
|  | 16cfaa809a | ||
|  | f2cd0b6d36 | ||
|  | a2e2ff5fa9 | ||
|  | 53d0480a12 | ||
|  | ff812a028c | ||
|  | 136f636fda | ||
|  | 5a8acbde00 | ||
|  | f60dc8cfa0 | ||
|  | 067dd6f9d1 | ||
|  | 7b1e32e514 | ||
|  | e88dbfd597 | ||
|  | 3d34270959 | ||
|  | 3bb99671ec | ||
|  | 0d69383dfd | ||
|  | 7b9ff119e8 | ||
|  | 108a56745c | ||
|  | 9643d7652b | ||
|  | fadb289ed7 | ||
|  | 905fc6e7cc | ||
|  | cdd81c1444 | ||
|  | 4afafceba1 | ||
|  | 3065eacc96 | ||
|  | 71ef3aedd8 | ||
|  | 0cf11c6348 | ||
|  | 70abd0f490 | ||
|  | 4445dd4a96 | ||
|  | 03932672f3 | ||
|  | dc6a40de02 | ||
|  | d58a299a8b | ||
|  | ad0a219ed3 | ||
|  | c4404ef995 | ||
|  | b4f3a158a6 | ||
|  | f0e9a7d3dc | ||
|  | a2b42c5329 | ||
|  | 6d6583bfe6 | ||
|  | 485d093002 | ||
|  | ff4353d344 | ||
|  | a90f45bd8b | ||
|  | 10c22ccc53 | ||
|  | 6969cee0f3 | ||
|  | ddeada200b | ||
|  | 8e2b24b2da | ||
|  | bd76c280ec | ||
|  | ca0a95ba9e | ||
|  | 614f76e699 | ||
|  | a5815f0bc7 | ||
|  | 84e9fea15f | ||
|  | b7a660ee40 | ||
|  | b9ebb1718a | ||
|  | 7ba5c76a89 | ||
|  | 702ddb5679 | ||
|  | 93aed87265 | ||
|  | 60355196ce | ||
|  | 9bffb32a5e | ||
|  | 5ef019c5c2 | ||
|  | 8da62e62fb | ||
|  | 56a43396d4 | ||
|  | 7966d6f397 | ||
|  | cb61c511ce | ||
|  | 25bfa575ed | ||
|  | e21d9fcfbe | ||
|  | b293904525 | ||
|  | bd7e6b8ad4 | ||
|  | a208a4fa25 | ||
|  | 4799b2c52d | ||
|  | 562dcfb908 | ||
|  | 12ef258ff0 | ||
|  | 2ae32ee3b6 | ||
|  | ec1bd45481 | ||
|  | 370a9a069e | ||
|  | 7f0a3784e9 | ||
|  | 36f4adf2e7 | ||
|  | ae7d5d5489 | ||
|  | 434097aba4 | ||
|  | a0ebf8658d | ||
|  | 423454ba5d | ||
|  | 3ccb31639c | ||
|  | 5fb12a1388 | ||
|  | fe029893b0 | ||
|  | 767e98c2a3 | ||
|  | 1bdad76fe9 | ||
|  | 0196db7fff | ||
|  | 1f53ad4407 | ||
|  | 018f6e3f13 | ||
|  | 9752a030d9 | ||
|  | b27bdb090d | ||
|  | 55a0fbb6cb | ||
|  | c356534309 | ||
|  | 51315a0555 | ||
|  | e5f9fe2cf5 | ||
|  | 6c63c6417c | ||
|  | 4563b2b640 | ||
|  | c630a3fbd5 | ||
|  | 79b8ebeca4 | ||
|  | dc14ba0101 | ||
|  | 6028bfeb56 | ||
|  | bd9773a8af | ||
|  | cdeb76d9f8 | ||
|  | ac4574200d | ||
|  | b17d31e8ee | ||
|  | 30d27459dd | ||
|  | 333f7aa284 | ||
|  | 587314e03c | ||
|  | 9f888a5281 | ||
|  | 88b1a25ca0 | ||
|  | 8cb50f58f2 | ||
|  | 041a8f20a9 | ||
|  | b1ffb28532 | ||
|  | 6225fb51f1 | ||
|  | 1dd74e8024 | ||
|  | 1af9f5f23c | ||
|  | 83d5a7ceff | ||
|  | a7cba0a4a3 | ||
|  | ccd9a66ab9 | ||
|  | c7a92fa4b2 | ||
|  | 5f1b698d58 | ||
|  | 0a5368d23f | ||
|  | 26b351a51c | ||
|  | 1836677c47 | ||
|  | e7a98c86f0 | ||
|  | eb5044490b | ||
|  | 983d7ec052 | ||
|  | dc56deaf85 | ||
|  | 19d1ecfc66 | ||
|  | 694f54e1c4 | ||
|  | b0c3eee699 | ||
|  | cd942779ca | ||
|  | 0d0fdef363 | ||
|  | 7ed544b3ac | ||
|  | 821efbf78b | ||
|  | a209e0d366 | ||
|  | ef485e0628 | ||
|  | 1481aa0635 | ||
|  | 867bf9fd25 | ||
|  | 47fda0ea36 | ||
|  | 623290827a | ||
|  | a87ce625f3 | ||
|  | 3559787fa7 | ||
|  | 787005e60d | ||
|  | 414e103686 | ||
|  | e6f3084588 | ||
|  | 145e55da75 | ||
|  | d3ba95cdca | ||
|  | 8ffb0ebb56 | ||
|  | 5038af9e34 | ||
|  | 819b4214c9 | ||
|  | b8a93b0b75 | 
| @@ -21,3 +21,6 @@ EMAIL_PASSWORD=CHANGE_ME | |||||||
| # Wiki configuration | # Wiki configuration | ||||||
| WIKI_USER=NoteKfet2020 | WIKI_USER=NoteKfet2020 | ||||||
| WIKI_PASSWORD= | WIKI_PASSWORD= | ||||||
|  |  | ||||||
|  | # OIDC | ||||||
|  | OIDC_RSA_PRIVATE_KEY=CHANGE_ME | ||||||
|   | |||||||
| @@ -8,7 +8,7 @@ variables: | |||||||
|   GIT_SUBMODULE_STRATEGY: recursive |   GIT_SUBMODULE_STRATEGY: recursive | ||||||
|  |  | ||||||
| # Ubuntu 22.04 | # Ubuntu 22.04 | ||||||
| py310-django42: | py310-django52: | ||||||
|   stage: test |   stage: test | ||||||
|   image: ubuntu:22.04 |   image: ubuntu:22.04 | ||||||
|   before_script: |   before_script: | ||||||
| @@ -22,10 +22,10 @@ py310-django42: | |||||||
|         python3-djangorestframework python3-django-oauth-toolkit python3-psycopg2 python3-pil |         python3-djangorestframework python3-django-oauth-toolkit python3-psycopg2 python3-pil | ||||||
|         python3-babel python3-lockfile python3-pip python3-phonenumbers python3-memcache |         python3-babel python3-lockfile python3-pip python3-phonenumbers python3-memcache | ||||||
|         python3-bs4 python3-setuptools tox texlive-xetex |         python3-bs4 python3-setuptools tox texlive-xetex | ||||||
|   script: tox -e py310-django42 |   script: tox -e py310-django52 | ||||||
|  |  | ||||||
| # Debian Bookworm | # Debian Bookworm | ||||||
| py311-django42: | py311-django52: | ||||||
|   stage: test |   stage: test | ||||||
|   image: debian:bookworm |   image: debian:bookworm | ||||||
|   before_script: |   before_script: | ||||||
| @@ -37,7 +37,7 @@ py311-django42: | |||||||
|         python3-djangorestframework python3-django-oauth-toolkit python3-psycopg2 python3-pil |         python3-djangorestframework python3-django-oauth-toolkit python3-psycopg2 python3-pil | ||||||
|         python3-babel python3-lockfile python3-pip python3-phonenumbers python3-memcache |         python3-babel python3-lockfile python3-pip python3-phonenumbers python3-memcache | ||||||
|         python3-bs4 python3-setuptools tox texlive-xetex |         python3-bs4 python3-setuptools tox texlive-xetex | ||||||
|   script: tox -e py311-django42 |   script: tox -e py311-django52 | ||||||
|  |  | ||||||
| linters: | linters: | ||||||
|   stage: quality-assurance |   stage: quality-assurance | ||||||
|   | |||||||
							
								
								
									
										16
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								README.md
									
									
									
									
									
								
							| @@ -58,7 +58,13 @@ Bien que cela permette de créer une instance sur toutes les distributions, | |||||||
|     (env)$ ./manage.py createsuperuser  # Création d'un⋅e utilisateur⋅rice initial |     (env)$ ./manage.py createsuperuser  # Création d'un⋅e utilisateur⋅rice initial | ||||||
|     ``` |     ``` | ||||||
|  |  | ||||||
| 6.  Enjoy : | 6. (Optionnel) **Création d'une clé privée OpenID Connect** | ||||||
|  |  | ||||||
|  | Pour activer le support d'OpenID Connect, il faut générer une clé privée, par | ||||||
|  | exemple avec openssl (`openssl genrsa -out oidc.key 4096`), et copier la clé dans .env dans le champ | ||||||
|  | `OIDC_RSA_PRIVATE_KEY`. | ||||||
|  |  | ||||||
|  | 7.  Enjoy : | ||||||
|  |  | ||||||
|     ```bash |     ```bash | ||||||
|     (env)$ ./manage.py runserver 0.0.0.0:8000 |     (env)$ ./manage.py runserver 0.0.0.0:8000 | ||||||
| @@ -228,7 +234,13 @@ Sinon vous pouvez suivre les étapes décrites ci-dessous. | |||||||
|         (env)$ ./manage.py check # pas de bêtise qui traine |         (env)$ ./manage.py check # pas de bêtise qui traine | ||||||
|         (env)$ ./manage.py migrate |         (env)$ ./manage.py migrate | ||||||
|  |  | ||||||
| 7.  *Enjoy \o/* | 7. **Création d'une clé privée OpenID Connect** | ||||||
|  |  | ||||||
|  | Pour activer le support d'OpenID Connect, il faut générer une clé privée, par | ||||||
|  | exemple avec openssl (`openssl genrsa -out oidc.key 4096`), et renseigner le champ | ||||||
|  | `OIDC_RSA_PRIVATE_KEY` dans le .env (par défaut `/var/secrets/oidc.key`). | ||||||
|  |  | ||||||
|  | 8.  *Enjoy \o/* | ||||||
|  |  | ||||||
| ### Installation avec Docker | ### Installation avec Docker | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| default_app_config = 'activity.apps.ActivityConfig' | default_app_config = 'activity.apps.ActivityConfig' | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.contrib import admin | from django.contrib import admin | ||||||
| @@ -35,7 +35,7 @@ class GuestAdmin(admin.ModelAdmin): | |||||||
|     """ |     """ | ||||||
|     Admin customisation for Guest |     Admin customisation for Guest | ||||||
|     """ |     """ | ||||||
|     list_display = ('last_name', 'first_name', 'activity', 'inviter') |     list_display = ('last_name', 'first_name', 'school', 'activity', 'inviter') | ||||||
|     form = GuestForm |     form = GuestForm | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.utils.translation import gettext_lazy as _ | from django.utils.translation import gettext_lazy as _ | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from .views import ActivityTypeViewSet, ActivityViewSet, EntryViewSet, GuestViewSet, OpenerViewSet | from .views import ActivityTypeViewSet, ActivityViewSet, EntryViewSet, GuestViewSet, OpenerViewSet | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from api.filters import RegexSafeSearchFilter | from api.filters import RegexSafeSearchFilter | ||||||
| @@ -51,9 +51,9 @@ class GuestViewSet(ReadProtectedModelViewSet): | |||||||
|     queryset = Guest.objects.order_by('id') |     queryset = Guest.objects.order_by('id') | ||||||
|     serializer_class = GuestSerializer |     serializer_class = GuestSerializer | ||||||
|     filter_backends = [DjangoFilterBackend, RegexSafeSearchFilter] |     filter_backends = [DjangoFilterBackend, RegexSafeSearchFilter] | ||||||
|     filterset_fields = ['activity', 'activity__name', 'last_name', 'first_name', 'inviter', 'inviter__alias__name', |     filterset_fields = ['activity', 'activity__name', 'last_name', 'first_name', 'school', 'inviter', 'inviter__alias__name', | ||||||
|                         'inviter__alias__normalized_name', ] |                         'inviter__alias__normalized_name', ] | ||||||
|     search_fields = ['$activity__name', '$last_name', '$first_name', '$inviter__user__email', '$inviter__alias__name', |     search_fields = ['$activity__name', '$last_name', '$first_name', '$school', '$inviter__user__email', '$inviter__alias__name', | ||||||
|                      '$inviter__alias__normalized_name', ] |                      '$inviter__alias__normalized_name', ] | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.apps import AppConfig | from django.apps import AppConfig | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from datetime import timedelta | from datetime import timedelta | ||||||
| @@ -107,7 +107,7 @@ class GuestForm(forms.ModelForm): | |||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = Guest |         model = Guest | ||||||
|         fields = ('last_name', 'first_name', 'inviter', ) |         fields = ('last_name', 'first_name', 'school', 'inviter', ) | ||||||
|         widgets = { |         widgets = { | ||||||
|             "inviter": Autocomplete( |             "inviter": Autocomplete( | ||||||
|                 NoteUser, |                 NoteUser, | ||||||
|   | |||||||
							
								
								
									
										18
									
								
								apps/activity/migrations/0006_guest_school.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								apps/activity/migrations/0006_guest_school.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | # Generated by Django 4.2.20 on 2025-03-25 09:58 | ||||||
|  |  | ||||||
|  | from django.db import migrations, models | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Migration(migrations.Migration): | ||||||
|  |     dependencies = [ | ||||||
|  |         ("activity", "0005_alter_opener_options_alter_opener_opener"), | ||||||
|  |     ] | ||||||
|  |  | ||||||
|  |     operations = [ | ||||||
|  |         migrations.AddField( | ||||||
|  |             model_name="guest", | ||||||
|  |             name="school", | ||||||
|  |             field=models.CharField(default="", max_length=255, verbose_name="school"), | ||||||
|  |             preserve_default=False, | ||||||
|  |         ), | ||||||
|  |     ] | ||||||
							
								
								
									
										19
									
								
								apps/activity/migrations/0007_alter_guest_activity.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								apps/activity/migrations/0007_alter_guest_activity.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | # Generated by Django 4.2.20 on 2025-05-08 19:07 | ||||||
|  |  | ||||||
|  | from django.db import migrations, models | ||||||
|  | import django.db.models.deletion | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Migration(migrations.Migration): | ||||||
|  |  | ||||||
|  |     dependencies = [ | ||||||
|  |         ('activity', '0006_guest_school'), | ||||||
|  |     ] | ||||||
|  |  | ||||||
|  |     operations = [ | ||||||
|  |         migrations.AlterField( | ||||||
|  |             model_name='guest', | ||||||
|  |             name='activity', | ||||||
|  |             field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='activity.activity'), | ||||||
|  |         ), | ||||||
|  |     ] | ||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import os | import os | ||||||
| @@ -201,7 +201,8 @@ class Entry(models.Model): | |||||||
|     def save(self, *args, **kwargs): |     def save(self, *args, **kwargs): | ||||||
|         qs = Entry.objects.filter(~Q(pk=self.pk), activity=self.activity, note=self.note, guest=self.guest) |         qs = Entry.objects.filter(~Q(pk=self.pk), activity=self.activity, note=self.note, guest=self.guest) | ||||||
|         if qs.exists(): |         if qs.exists(): | ||||||
|             raise ValidationError(_("Already entered on ") + _("{:%Y-%m-%d %H:%M:%S}").format(qs.get().time, )) |             raise ValidationError(_("Already entered on ") | ||||||
|  |                                   + _("{:%Y-%m-%d %H:%M:%S}").format(timezone.localtime(qs.get().time), )) | ||||||
|  |  | ||||||
|         if self.guest: |         if self.guest: | ||||||
|             self.note = self.guest.inviter |             self.note = self.guest.inviter | ||||||
| @@ -233,7 +234,7 @@ class Guest(models.Model): | |||||||
|     """ |     """ | ||||||
|     activity = models.ForeignKey( |     activity = models.ForeignKey( | ||||||
|         Activity, |         Activity, | ||||||
|         on_delete=models.PROTECT, |         on_delete=models.CASCADE, | ||||||
|         related_name='+', |         related_name='+', | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
| @@ -247,6 +248,11 @@ class Guest(models.Model): | |||||||
|         verbose_name=_("first name"), |         verbose_name=_("first name"), | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|  |     school = models.CharField( | ||||||
|  |         max_length=255, | ||||||
|  |         verbose_name=_("school"), | ||||||
|  |     ) | ||||||
|  |  | ||||||
|     inviter = models.ForeignKey( |     inviter = models.ForeignKey( | ||||||
|         NoteUser, |         NoteUser, | ||||||
|         on_delete=models.PROTECT, |         on_delete=models.PROTECT, | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.utils import timezone | from django.utils import timezone | ||||||
| @@ -51,11 +51,11 @@ class GuestTable(tables.Table): | |||||||
|         } |         } | ||||||
|         model = Guest |         model = Guest | ||||||
|         template_name = 'django_tables2/bootstrap4.html' |         template_name = 'django_tables2/bootstrap4.html' | ||||||
|         fields = ("last_name", "first_name", "inviter", ) |         fields = ("last_name", "first_name", "inviter", "school") | ||||||
|  |  | ||||||
|     def render_entry(self, record): |     def render_entry(self, record): | ||||||
|         if record.has_entry: |         if record.has_entry: | ||||||
|             return str(_("Entered on ") + str(_("{:%Y-%m-%d %H:%M:%S}").format(record.entry.time, ))) |             return str(_("Entered on ") + str(_("{:%Y-%m-%d %H:%M:%S}").format(timezone.localtime(record.entry.time)))) | ||||||
|         return mark_safe('<button id="{id}" class="btn btn-danger btn-sm" onclick="remove_guest(this.id)"> ' |         return mark_safe('<button id="{id}" class="btn btn-danger btn-sm" onclick="remove_guest(this.id)"> ' | ||||||
|                          '{delete_trans}</button>'.format(id=record.id, delete_trans=_("remove").capitalize())) |                          '{delete_trans}</button>'.format(id=record.id, delete_trans=_("remove").capitalize())) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -95,5 +95,23 @@ SPDX-License-Identifier: GPL-3.0-or-later | |||||||
|             errMsg(xhr.responseJSON); |             errMsg(xhr.responseJSON); | ||||||
|         }); |         }); | ||||||
|     }); |     }); | ||||||
|  |     $("#delete_activity").click(function () { | ||||||
|  |         if (!confirm("{% trans 'Are you sure you want to delete this activity?' %}")) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $.ajax({ | ||||||
|  |             url: "/api/activity/activity/{{ activity.pk }}/", | ||||||
|  |             type: "DELETE", | ||||||
|  |             headers: { | ||||||
|  |                 "X-CSRFTOKEN": CSRF_TOKEN | ||||||
|  |             } | ||||||
|  |         }).done(function () { | ||||||
|  |             addMsg("{% trans 'Activity deleted' %}", "success"); | ||||||
|  |             window.location.href = "/activity/";  // Redirige vers la liste des activités | ||||||
|  |         }).fail(function (xhr) { | ||||||
|  |             errMsg(xhr.responseJSON); | ||||||
|  |         }); | ||||||
|  |     }); | ||||||
| </script> | </script> | ||||||
| {% endblock %} | {% endblock %} | ||||||
|   | |||||||
| @@ -38,6 +38,7 @@ SPDX-License-Identifier: GPL-3.0-or-later | |||||||
| </a> | </a> | ||||||
|  |  | ||||||
| <input id="alias" type="text" class="form-control" placeholder="Nom/note ..."> | <input id="alias" type="text" class="form-control" placeholder="Nom/note ..."> | ||||||
|  | <button id="trigger" class="btn btn-secondary">Click me !</button> | ||||||
|  |  | ||||||
| <hr> | <hr> | ||||||
|  |  | ||||||
| @@ -63,15 +64,46 @@ SPDX-License-Identifier: GPL-3.0-or-later | |||||||
|         refreshBalance(); |         refreshBalance(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     function process_qrcode() { | ||||||
|  |         let name = alias_obj.val(); | ||||||
|  |         $.get("/api/note/note?search=" + name + "&format=json").done( | ||||||
|  |             function (res) { | ||||||
|  |                 let note = res.results[0]; | ||||||
|  |                 $.post("/api/activity/entry/?format=json", { | ||||||
|  |                     csrfmiddlewaretoken: CSRF_TOKEN, | ||||||
|  |                     activity: {{ activity.id }}, | ||||||
|  |                     note: note.id, | ||||||
|  |                     guest: null | ||||||
|  |                 }).done(function () { | ||||||
|  |                     addMsg(interpolate(gettext( | ||||||
|  |                         "Entry made for %s whose balance is %s €"), | ||||||
|  |                         [note.name, note.balance / 100]), "success", 4000); | ||||||
|  |                     reloadTable(true); | ||||||
|  |                 }).fail(function (xhr) { | ||||||
|  |                     errMsg(xhr.responseJSON, 4000); | ||||||
|  |                 }); | ||||||
|  |             }).fail(function (xhr) { | ||||||
|  |                 errMsg(xhr.responseJSON, 4000); | ||||||
|  |             }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     alias_obj.keyup(function(event) { |     alias_obj.keyup(function(event) { | ||||||
|         let code = event.originalEvent.keyCode |         let code = event.originalEvent.keyCode | ||||||
|         if (65 <= code <= 122 || code === 13) { |         if (65 <= code <= 122 || code === 13) { | ||||||
|             debounce(reloadTable)() |             debounce(reloadTable)() | ||||||
|         } |         } | ||||||
|  |         if (code === 0) | ||||||
|  |             process_qrcode(); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     $(document).ready(init); |     $(document).ready(init); | ||||||
|  |  | ||||||
|  |     alias_obj2 = document.getElementById("alias"); | ||||||
|  |     $("#trigger").click(function (e) { | ||||||
|  |         addMsg("Clicked", "success", 1000); | ||||||
|  |         alias_obj.val(alias_obj.val() + "\0"); | ||||||
|  |         alias_obj2.dispatchEvent(new KeyboardEvent('keyup')); | ||||||
|  |     }) | ||||||
|     function init() { |     function init() { | ||||||
|         $(".table-row").click(function (e) { |         $(".table-row").click(function (e) { | ||||||
|             let target = e.target.parentElement; |             let target = e.target.parentElement; | ||||||
|   | |||||||
| @@ -70,7 +70,10 @@ SPDX-License-Identifier: GPL-3.0-or-later | |||||||
|             {% if ".change_"|has_perm:activity %} |             {% if ".change_"|has_perm:activity %} | ||||||
|                 <a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_update' pk=activity.pk %}" data-turbolinks="false"> {% trans "edit"|capfirst %}</a> |                 <a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_update' pk=activity.pk %}" data-turbolinks="false"> {% trans "edit"|capfirst %}</a> | ||||||
|             {% endif %} |             {% endif %} | ||||||
|             {% if activity.activity_type.can_invite and not activity_started %} |             {% if not activity.valid and ".delete_"|has_perm:activity %} | ||||||
|  |                 <a class="btn btn-danger btn-sm my-1" id="delete_activity"> {% trans "delete"|capfirst %} </a> | ||||||
|  |             {% endif %} | ||||||
|  |             {% if activity.activity_type.can_invite and not activity_started and activity.valid %} | ||||||
|                 <a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_invite' pk=activity.pk %}" data-turbolinks="false"> {% trans "Invite" %}</a> |                 <a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_invite' pk=activity.pk %}" data-turbolinks="false"> {% trans "Invite" %}</a> | ||||||
|             {% endif %} |             {% endif %} | ||||||
|         {% endif %} |         {% endif %} | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from datetime import timedelta | from datetime import timedelta | ||||||
| @@ -50,6 +50,7 @@ class TestActivities(TestCase): | |||||||
|             inviter=self.user.note, |             inviter=self.user.note, | ||||||
|             last_name="GUEST", |             last_name="GUEST", | ||||||
|             first_name="Guest", |             first_name="Guest", | ||||||
|  |             school="School", | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|     def test_activity_list(self): |     def test_activity_list(self): | ||||||
| @@ -156,6 +157,7 @@ class TestActivities(TestCase): | |||||||
|             inviter=self.user.note.id, |             inviter=self.user.note.id, | ||||||
|             last_name="GUEST2", |             last_name="GUEST2", | ||||||
|             first_name="Guest", |             first_name="Guest", | ||||||
|  |             school="School", | ||||||
|         )) |         )) | ||||||
|         self.assertEqual(response.status_code, 200) |         self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
| @@ -167,6 +169,7 @@ class TestActivities(TestCase): | |||||||
|             inviter=self.user.note.id, |             inviter=self.user.note.id, | ||||||
|             last_name="GUEST2", |             last_name="GUEST2", | ||||||
|             first_name="Guest", |             first_name="Guest", | ||||||
|  |             school="School", | ||||||
|         )) |         )) | ||||||
|         self.assertRedirects(response, reverse("activity:activity_detail", args=(self.activity.pk,)), 302, 200) |         self.assertRedirects(response, reverse("activity:activity_detail", args=(self.activity.pk,)), 302, 200) | ||||||
|  |  | ||||||
| @@ -200,6 +203,7 @@ class TestActivityAPI(TestAPI): | |||||||
|             inviter=self.user.note, |             inviter=self.user.note, | ||||||
|             last_name="GUEST", |             last_name="GUEST", | ||||||
|             first_name="Guest", |             first_name="Guest", | ||||||
|  |             school="School", | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         self.entry = Entry.objects.create( |         self.entry = Entry.objects.create( | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.urls import path | from django.urls import path | ||||||
| @@ -15,4 +15,5 @@ urlpatterns = [ | |||||||
|     path('<int:pk>/update/', views.ActivityUpdateView.as_view(), name='activity_update'), |     path('<int:pk>/update/', views.ActivityUpdateView.as_view(), name='activity_update'), | ||||||
|     path('new/', views.ActivityCreateView.as_view(), name='activity_create'), |     path('new/', views.ActivityCreateView.as_view(), name='activity_create'), | ||||||
|     path('calendar.ics', views.CalendarView.as_view(), name='calendar_ics'), |     path('calendar.ics', views.CalendarView.as_view(), name='calendar_ics'), | ||||||
|  |     path('<int:pk>/delete', views.ActivityDeleteView.as_view(), name='delete_activity'), | ||||||
| ] | ] | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from hashlib import md5 | from hashlib import md5 | ||||||
| @@ -9,7 +9,7 @@ from django.contrib.contenttypes.models import ContentType | |||||||
| from django.core.exceptions import PermissionDenied | from django.core.exceptions import PermissionDenied | ||||||
| from django.db import transaction | from django.db import transaction | ||||||
| from django.db.models import F, Q | from django.db.models import F, Q | ||||||
| from django.http import HttpResponse | from django.http import HttpResponse, JsonResponse | ||||||
| from django.urls import reverse_lazy | from django.urls import reverse_lazy | ||||||
| from django.utils import timezone | from django.utils import timezone | ||||||
| from django.utils.decorators import method_decorator | from django.utils.decorators import method_decorator | ||||||
| @@ -153,6 +153,34 @@ class ActivityUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): | |||||||
|         return reverse_lazy('activity:activity_detail', kwargs={"pk": self.kwargs["pk"]}) |         return reverse_lazy('activity:activity_detail', kwargs={"pk": self.kwargs["pk"]}) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class ActivityDeleteView(View): | ||||||
|  |     """ | ||||||
|  |     Deletes an Activity | ||||||
|  |     """ | ||||||
|  |     def delete(self, request, pk): | ||||||
|  |         try: | ||||||
|  |             activity = Activity.objects.get(pk=pk) | ||||||
|  |             activity.delete() | ||||||
|  |             return JsonResponse({"message": "Activity deleted"}) | ||||||
|  |         except Activity.DoesNotExist: | ||||||
|  |             return JsonResponse({"error": "Activity not found"}, status=404) | ||||||
|  |  | ||||||
|  |     def dispatch(self, *args, **kwargs): | ||||||
|  |         """ | ||||||
|  |         Don't display the delete button if the user has no right to delete. | ||||||
|  |         """ | ||||||
|  |         if not self.request.user.is_authenticated: | ||||||
|  |             return self.handle_no_permission() | ||||||
|  |  | ||||||
|  |         activity = Activity.objects.get(pk=self.kwargs["pk"]) | ||||||
|  |         if not PermissionBackend.check_perm(self.request, "activity.delete_activity", activity): | ||||||
|  |             raise PermissionDenied(_("You are not allowed to delete this activity.")) | ||||||
|  |  | ||||||
|  |         if activity.valid: | ||||||
|  |             raise PermissionDenied(_("This activity is valid.")) | ||||||
|  |         return super().dispatch(*args, **kwargs) | ||||||
|  |  | ||||||
|  |  | ||||||
| class ActivityInviteView(ProtectQuerysetMixin, ProtectedCreateView): | class ActivityInviteView(ProtectQuerysetMixin, ProtectedCreateView): | ||||||
|     """ |     """ | ||||||
|     Invite a Guest, The rules to invites someone are defined in `forms:activity.GuestForm` |     Invite a Guest, The rules to invites someone are defined in `forms:activity.GuestForm` | ||||||
| @@ -168,6 +196,7 @@ class ActivityInviteView(ProtectQuerysetMixin, ProtectedCreateView): | |||||||
|             activity=activity, |             activity=activity, | ||||||
|             first_name="", |             first_name="", | ||||||
|             last_name="", |             last_name="", | ||||||
|  |             school="", | ||||||
|             inviter=self.request.user.note, |             inviter=self.request.user.note, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
| @@ -329,7 +358,7 @@ class ActivityEntryView(LoginRequiredMixin, SingleTableMixin, TemplateView): | |||||||
|         context["noteuser_ctype"] = ContentType.objects.get_for_model(NoteUser).pk |         context["noteuser_ctype"] = ContentType.objects.get_for_model(NoteUser).pk | ||||||
|         context["notespecial_ctype"] = ContentType.objects.get_for_model(NoteSpecial).pk |         context["notespecial_ctype"] = ContentType.objects.get_for_model(NoteSpecial).pk | ||||||
|  |  | ||||||
|         activities_open = Activity.objects.filter(open=True).filter( |         activities_open = Activity.objects.filter(open=True, activity_type__manage_entries=True).filter( | ||||||
|             PermissionBackend.filter_queryset(self.request, Activity, "view")).distinct().all() |             PermissionBackend.filter_queryset(self.request, Activity, "view")).distinct().all() | ||||||
|         context["activities_open"] = [a for a in activities_open |         context["activities_open"] = [a for a in activities_open | ||||||
|                                       if PermissionBackend.check_perm(self.request, |                                       if PermissionBackend.check_perm(self.request, | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| default_app_config = 'api.apps.APIConfig' | default_app_config = 'api.apps.APIConfig' | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.apps import AppConfig | from django.apps import AppConfig | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import json | import json | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
| @@ -47,6 +47,10 @@ if "wei" in settings.INSTALLED_APPS: | |||||||
|     from wei.api.urls import register_wei_urls |     from wei.api.urls import register_wei_urls | ||||||
|     register_wei_urls(router, 'wei') |     register_wei_urls(router, 'wei') | ||||||
|  |  | ||||||
|  | if "wrapped" in settings.INSTALLED_APPS: | ||||||
|  |     from wrapped.api.urls import register_wrapped_urls | ||||||
|  |     register_wrapped_urls(router, 'wrapped') | ||||||
|  |  | ||||||
| app_name = 'api' | app_name = 'api' | ||||||
|  |  | ||||||
| # Wire up our API using automatic URL routing. | # Wire up our API using automatic URL routing. | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import re | import re | ||||||
|   | |||||||
| @@ -1,37 +1,59 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.contrib import admin | from django.contrib import admin | ||||||
| from django.db import transaction | from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicParentModelAdmin | ||||||
| from note_kfet.admin import admin_site | from note_kfet.admin import admin_site | ||||||
|  |  | ||||||
| from .models import Allergen, BasicFood, QRCode, TransformedFood | from .models import Allergen, Food, BasicFood, TransformedFood, QRCode | ||||||
|  |  | ||||||
|  |  | ||||||
| @admin.register(QRCode, site=admin_site) |  | ||||||
| class QRCodeAdmin(admin.ModelAdmin): |  | ||||||
|     pass |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @admin.register(BasicFood, site=admin_site) |  | ||||||
| class BasicFoodAdmin(admin.ModelAdmin): |  | ||||||
|     @transaction.atomic |  | ||||||
|     def save_related(self, *args, **kwargs): |  | ||||||
|         ans = super().save_related(*args, **kwargs) |  | ||||||
|         args[1].instance.update() |  | ||||||
|         return ans |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @admin.register(TransformedFood, site=admin_site) |  | ||||||
| class TransformedFoodAdmin(admin.ModelAdmin): |  | ||||||
|     exclude = ["allergens", "expiry_date"] |  | ||||||
|  |  | ||||||
|     @transaction.atomic |  | ||||||
|     def save_related(self, request, form, *args, **kwargs): |  | ||||||
|         super().save_related(request, form, *args, **kwargs) |  | ||||||
|         form.instance.update() |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @admin.register(Allergen, site=admin_site) | @admin.register(Allergen, site=admin_site) | ||||||
| class AllergenAdmin(admin.ModelAdmin): | class AllergenAdmin(admin.ModelAdmin): | ||||||
|     pass |     """ | ||||||
|  |     Admin customisation for Allergen | ||||||
|  |     """ | ||||||
|  |     ordering = ['name'] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @admin.register(Food, site=admin_site) | ||||||
|  | class FoodAdmin(PolymorphicParentModelAdmin): | ||||||
|  |     """ | ||||||
|  |     Admin customisation for Food | ||||||
|  |     """ | ||||||
|  |     child_models = (Food, BasicFood, TransformedFood) | ||||||
|  |     list_display = ('name', 'expiry_date', 'owner', 'is_ready') | ||||||
|  |     list_filter = ('is_ready', 'end_of_life') | ||||||
|  |     search_fields = ['name'] | ||||||
|  |     ordering = ['expiry_date', 'name'] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @admin.register(BasicFood, site=admin_site) | ||||||
|  | class BasicFood(PolymorphicChildModelAdmin): | ||||||
|  |     """ | ||||||
|  |     Admin customisation for BasicFood | ||||||
|  |     """ | ||||||
|  |     list_display = ('name', 'expiry_date', 'date_type', 'owner', 'is_ready') | ||||||
|  |     list_filter = ('is_ready', 'date_type', 'end_of_life') | ||||||
|  |     search_fields = ['name'] | ||||||
|  |     ordering = ['expiry_date', 'name'] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @admin.register(TransformedFood, site=admin_site) | ||||||
|  | class TransformedFood(PolymorphicChildModelAdmin): | ||||||
|  |     """ | ||||||
|  |     Admin customisation for TransformedFood | ||||||
|  |     """ | ||||||
|  |     list_display = ('name', 'expiry_date', 'shelf_life', 'owner', 'is_ready') | ||||||
|  |     list_filter = ('is_ready', 'end_of_life', 'shelf_life') | ||||||
|  |     search_fields = ['name'] | ||||||
|  |     ordering = ['expiry_date', 'name'] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @admin.register(QRCode, site=admin_site) | ||||||
|  | class QRCodeAdmin(admin.ModelAdmin): | ||||||
|  |     """ | ||||||
|  |     Admin customisation for QRCode | ||||||
|  |     """ | ||||||
|  |     list_diplay = ('qr_code_number', 'food_container') | ||||||
|  |     search_fields = ['food_container__name'] | ||||||
|   | |||||||
| @@ -1,9 +1,9 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from rest_framework import serializers | from rest_framework import serializers | ||||||
|  |  | ||||||
| from ..models import Allergen, BasicFood, QRCode, TransformedFood | from ..models import Allergen, Food, BasicFood, TransformedFood, QRCode | ||||||
|  |  | ||||||
|  |  | ||||||
| class AllergenSerializer(serializers.ModelSerializer): | class AllergenSerializer(serializers.ModelSerializer): | ||||||
| @@ -11,40 +11,46 @@ class AllergenSerializer(serializers.ModelSerializer): | |||||||
|     REST API Serializer for Allergen. |     REST API Serializer for Allergen. | ||||||
|     The djangorestframework plugin will analyse the model `Allergen` and parse all fields in the API. |     The djangorestframework plugin will analyse the model `Allergen` and parse all fields in the API. | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = Allergen |         model = Allergen | ||||||
|         fields = '__all__' |         fields = '__all__' | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class FoodSerializer(serializers.ModelSerializer): | ||||||
|  |     """ | ||||||
|  |     REST API Serializer for Food. | ||||||
|  |     The djangorestframework plugin will analyse the model `Food` and parse all fields in the API. | ||||||
|  |     """ | ||||||
|  |     class Meta: | ||||||
|  |         model = Food | ||||||
|  |         fields = '__all__' | ||||||
|  |  | ||||||
|  |  | ||||||
| class BasicFoodSerializer(serializers.ModelSerializer): | class BasicFoodSerializer(serializers.ModelSerializer): | ||||||
|     """ |     """ | ||||||
|     REST API Serializer for BasicFood. |     REST API Serializer for BasicFood. | ||||||
|     The djangorestframework plugin will analyse the model `BasicFood` and parse all fields in the API. |     The djangorestframework plugin will analyse the model `BasicFood` and parse all fields in the API. | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = BasicFood |         model = BasicFood | ||||||
|         fields = '__all__' |         fields = '__all__' | ||||||
|  |  | ||||||
|  |  | ||||||
| class QRCodeSerializer(serializers.ModelSerializer): |  | ||||||
|     """ |  | ||||||
|     REST API Serializer for QRCode. |  | ||||||
|     The djangorestframework plugin will analyse the model `QRCode` and parse all fields in the API. |  | ||||||
|     """ |  | ||||||
|  |  | ||||||
|     class Meta: |  | ||||||
|         model = QRCode |  | ||||||
|         fields = '__all__' |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class TransformedFoodSerializer(serializers.ModelSerializer): | class TransformedFoodSerializer(serializers.ModelSerializer): | ||||||
|     """ |     """ | ||||||
|     REST API Serializer for TransformedFood. |     REST API Serializer for TransformedFood. | ||||||
|     The djangorestframework plugin will analyse the model `TransformedFood` and parse all fields in the API. |     The djangorestframework plugin will analyse the model `TransformedFood` and parse all fields in the API. | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = TransformedFood |         model = TransformedFood | ||||||
|         fields = '__all__' |         fields = '__all__' | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class QRCodeSerializer(serializers.ModelSerializer): | ||||||
|  |     """ | ||||||
|  |     REST API Serializer for QRCode. | ||||||
|  |     The djangorestframework plugin will analyse the model `QRCode` and parse all fields in the API. | ||||||
|  |     """ | ||||||
|  |     class Meta: | ||||||
|  |         model = QRCode | ||||||
|  |         fields = '__all__' | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from .views import AllergenViewSet, BasicFoodViewSet, QRCodeViewSet, TransformedFoodViewSet | from .views import AllergenViewSet, FoodViewSet, BasicFoodViewSet, TransformedFoodViewSet, QRCodeViewSet | ||||||
|  |  | ||||||
|  |  | ||||||
| def register_food_urls(router, path): | def register_food_urls(router, path): | ||||||
| @@ -9,6 +9,7 @@ def register_food_urls(router, path): | |||||||
|     Configure router for Food REST API. |     Configure router for Food REST API. | ||||||
|     """ |     """ | ||||||
|     router.register(path + '/allergen', AllergenViewSet) |     router.register(path + '/allergen', AllergenViewSet) | ||||||
|     router.register(path + '/basic_food', BasicFoodViewSet) |     router.register(path + '/food', FoodViewSet) | ||||||
|  |     router.register(path + '/basicfood', BasicFoodViewSet) | ||||||
|  |     router.register(path + '/transformedfood', TransformedFoodViewSet) | ||||||
|     router.register(path + '/qrcode', QRCodeViewSet) |     router.register(path + '/qrcode', QRCodeViewSet) | ||||||
|     router.register(path + '/transformed_food', TransformedFoodViewSet) |  | ||||||
|   | |||||||
| @@ -1,12 +1,12 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from api.viewsets import ReadProtectedModelViewSet | from api.viewsets import ReadProtectedModelViewSet | ||||||
| from django_filters.rest_framework import DjangoFilterBackend | from django_filters.rest_framework import DjangoFilterBackend | ||||||
| from rest_framework.filters import SearchFilter | from rest_framework.filters import SearchFilter | ||||||
|  |  | ||||||
| from .serializers import AllergenSerializer, BasicFoodSerializer, QRCodeSerializer, TransformedFoodSerializer | from .serializers import AllergenSerializer, FoodSerializer, BasicFoodSerializer, TransformedFoodSerializer, QRCodeSerializer | ||||||
| from ..models import Allergen, BasicFood, QRCode, TransformedFood | from ..models import Allergen, Food, BasicFood, TransformedFood, QRCode | ||||||
|  |  | ||||||
|  |  | ||||||
| class AllergenViewSet(ReadProtectedModelViewSet): | class AllergenViewSet(ReadProtectedModelViewSet): | ||||||
| @@ -22,11 +22,24 @@ class AllergenViewSet(ReadProtectedModelViewSet): | |||||||
|     search_fields = ['$name', ] |     search_fields = ['$name', ] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class FoodViewSet(ReadProtectedModelViewSet): | ||||||
|  |     """ | ||||||
|  |     REST API View set. | ||||||
|  |     The djangorestframework plugin will get all `Food` objects, serialize it to JSON with the given serializer, | ||||||
|  |     then render it on /api/food/food/ | ||||||
|  |     """ | ||||||
|  |     queryset = Food.objects.order_by('id') | ||||||
|  |     serializer_class = FoodSerializer | ||||||
|  |     filter_backends = [DjangoFilterBackend, SearchFilter] | ||||||
|  |     filterset_fields = ['name', ] | ||||||
|  |     search_fields = ['$name', ] | ||||||
|  |  | ||||||
|  |  | ||||||
| class BasicFoodViewSet(ReadProtectedModelViewSet): | class BasicFoodViewSet(ReadProtectedModelViewSet): | ||||||
|     """ |     """ | ||||||
|     REST API View set. |     REST API View set. | ||||||
|     The djangorestframework plugin will get all `BasicFood` objects, serialize it to JSON with the given serializer, |     The djangorestframework plugin will get all `BasicFood` objects, serialize it to JSON with the given serializer, | ||||||
|     then render it on /api/food/basic_food/ |     then render it on /api/food/basicfood/ | ||||||
|     """ |     """ | ||||||
|     queryset = BasicFood.objects.order_by('id') |     queryset = BasicFood.objects.order_by('id') | ||||||
|     serializer_class = BasicFoodSerializer |     serializer_class = BasicFoodSerializer | ||||||
| @@ -35,6 +48,19 @@ class BasicFoodViewSet(ReadProtectedModelViewSet): | |||||||
|     search_fields = ['$name', ] |     search_fields = ['$name', ] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TransformedFoodViewSet(ReadProtectedModelViewSet): | ||||||
|  |     """ | ||||||
|  |     REST API View set. | ||||||
|  |     The djangorestframework plugin will get all `TransformedFood` objects, serialize it to JSON with the given serializer, | ||||||
|  |     then render it on /api/food/transformedfood/ | ||||||
|  |     """ | ||||||
|  |     queryset = TransformedFood.objects.order_by('id') | ||||||
|  |     serializer_class = TransformedFoodSerializer | ||||||
|  |     filter_backends = [DjangoFilterBackend, SearchFilter] | ||||||
|  |     filterset_fields = ['name', ] | ||||||
|  |     search_fields = ['$name', ] | ||||||
|  |  | ||||||
|  |  | ||||||
| class QRCodeViewSet(ReadProtectedModelViewSet): | class QRCodeViewSet(ReadProtectedModelViewSet): | ||||||
|     """ |     """ | ||||||
|     REST API View set. |     REST API View set. | ||||||
| @@ -46,16 +72,3 @@ class QRCodeViewSet(ReadProtectedModelViewSet): | |||||||
|     filter_backends = [DjangoFilterBackend, SearchFilter] |     filter_backends = [DjangoFilterBackend, SearchFilter] | ||||||
|     filterset_fields = ['qr_code_number', ] |     filterset_fields = ['qr_code_number', ] | ||||||
|     search_fields = ['$qr_code_number', ] |     search_fields = ['$qr_code_number', ] | ||||||
|  |  | ||||||
|  |  | ||||||
| class TransformedFoodViewSet(ReadProtectedModelViewSet): |  | ||||||
|     """ |  | ||||||
|     REST API View set. |  | ||||||
|     The djangorestframework plugin will get all `TransformedFood` objects, serialize it to JSON with the given serializer, |  | ||||||
|     then render it on /api/food/transformed_food/ |  | ||||||
|     """ |  | ||||||
|     queryset = TransformedFood.objects.order_by('id') |  | ||||||
|     serializer_class = TransformedFoodSerializer |  | ||||||
|     filter_backends = [DjangoFilterBackend, SearchFilter] |  | ||||||
|     filterset_fields = ['name', ] |  | ||||||
|     search_fields = ['$name', ] |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										100
									
								
								apps/food/fixtures/initial.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								apps/food/fixtures/initial.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,100 @@ | |||||||
|  | [ | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 1, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Lait" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 2, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Oeufs" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 3, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Gluten" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 4, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Fruits à coques" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 5, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Arachides" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 6, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Sésame" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 7, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Soja" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 8, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Céleri" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 9, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Lupin" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 10, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Moutarde" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 11, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Sulfites" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 12, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Crustacés" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 13, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Mollusques" | ||||||
|  | 	} | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  | 	"model": "food.allergen", | ||||||
|  | 	"pk": 14, | ||||||
|  | 	"fields": { | ||||||
|  | 	    "name": "Poissons" | ||||||
|  | 	} | ||||||
|  |     } | ||||||
|  | ] | ||||||
| @@ -1,44 +1,43 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from random import shuffle | from random import shuffle | ||||||
|  |  | ||||||
| from django import forms |  | ||||||
| from django.utils.translation import gettext_lazy as _ |  | ||||||
| from django.utils import timezone |  | ||||||
| from member.models import Club |  | ||||||
| from bootstrap_datepicker_plus.widgets import DateTimePickerInput | from bootstrap_datepicker_plus.widgets import DateTimePickerInput | ||||||
|  | from django import forms | ||||||
|  | from django.forms.widgets import NumberInput | ||||||
|  | from django.utils.translation import gettext_lazy as _ | ||||||
|  | from member.models import Club | ||||||
| from note_kfet.inputs import Autocomplete | from note_kfet.inputs import Autocomplete | ||||||
| from note_kfet.middlewares import get_current_request | from note_kfet.middlewares import get_current_request | ||||||
| from permission.backends import PermissionBackend | from permission.backends import PermissionBackend | ||||||
|  |  | ||||||
| from .models import BasicFood, QRCode, TransformedFood | from .models import Food, BasicFood, TransformedFood, QRCode | ||||||
|  |  | ||||||
|  |  | ||||||
| class AddIngredientForms(forms.ModelForm): | class QRCodeForms(forms.ModelForm): | ||||||
|     """ |     """ | ||||||
|     Form for add an ingredient |     Form for create QRCode for container | ||||||
|     """ |     """ | ||||||
|     def __init__(self, *args, **kwargs): |     def __init__(self, *args, **kwargs): | ||||||
|         super().__init__(*args, **kwargs) |         super().__init__(*args, **kwargs) | ||||||
|         self.fields['ingredient'].queryset = self.fields['ingredient'].queryset.filter( |         self.fields['food_container'].queryset = self.fields['food_container'].queryset.filter( | ||||||
|  |             end_of_life__isnull=True, | ||||||
|             polymorphic_ctype__model='transformedfood', |             polymorphic_ctype__model='transformedfood', | ||||||
|             is_ready=False, |         ).filter(PermissionBackend.filter_queryset( | ||||||
|             is_active=True, |             get_current_request(), | ||||||
|             was_eaten=False, |             TransformedFood, | ||||||
|         ) |             "view", | ||||||
|         # Caution, the logic is inverted here, we flip the logic on saving in AddIngredientView |         )) | ||||||
|         self.fields['is_active'].initial = True |  | ||||||
|         self.fields['is_active'].label = _("Fully used") |  | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = TransformedFood |         model = QRCode | ||||||
|         fields = ('ingredient', 'is_active') |         fields = ('food_container',) | ||||||
|  |  | ||||||
|  |  | ||||||
| class BasicFoodForms(forms.ModelForm): | class BasicFoodForms(forms.ModelForm): | ||||||
|     """ |     """ | ||||||
|     Form for add non-transformed food |     Form for add basicfood | ||||||
|     """ |     """ | ||||||
|     def __init__(self, *args, **kwargs): |     def __init__(self, *args, **kwargs): | ||||||
|         super().__init__(*args, **kwargs) |         super().__init__(*args, **kwargs) | ||||||
| @@ -51,64 +50,138 @@ class BasicFoodForms(forms.ModelForm): | |||||||
|         clubs = list(Club.objects.filter(PermissionBackend.filter_queryset(get_current_request(), Club, "change")).all()) |         clubs = list(Club.objects.filter(PermissionBackend.filter_queryset(get_current_request(), Club, "change")).all()) | ||||||
|         shuffle(clubs) |         shuffle(clubs) | ||||||
|         self.fields['owner'].widget.attrs["placeholder"] = ", ".join(club.name for club in clubs[:4]) + ", ..." |         self.fields['owner'].widget.attrs["placeholder"] = ", ".join(club.name for club in clubs[:4]) + ", ..." | ||||||
|  |         self.fields['order'].widget.attrs["placeholder"] = _("Specific order given to GCKs") | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = BasicFood |         model = BasicFood | ||||||
|         fields = ('name', 'owner', 'date_type', 'expiry_date', 'is_active', 'was_eaten', 'allergens',) |         fields = ('name', 'owner', 'date_type', 'expiry_date', 'allergens', 'order',) | ||||||
|         widgets = { |         widgets = { | ||||||
|             "owner": Autocomplete( |             "owner": Autocomplete( | ||||||
|                 model=Club, |                 model=Club, | ||||||
|                 attrs={"api_url": "/api/members/club/"}, |                 attrs={"api_url": "/api/members/club/"}, | ||||||
|             ), |             ), | ||||||
|             'expiry_date': DateTimePickerInput(), |             "expiry_date": DateTimePickerInput(), | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
| class QRCodeForms(forms.ModelForm): |  | ||||||
|     """ |  | ||||||
|     Form for create QRCode |  | ||||||
|     """ |  | ||||||
|     def __init__(self, *args, **kwargs): |  | ||||||
|         super().__init__(*args, **kwargs) |  | ||||||
|         self.fields['food_container'].queryset = self.fields['food_container'].queryset.filter( |  | ||||||
|             is_active=True, |  | ||||||
|             was_eaten=False, |  | ||||||
|             polymorphic_ctype__model='transformedfood', |  | ||||||
|         ) |  | ||||||
|  |  | ||||||
|     class Meta: |  | ||||||
|         model = QRCode |  | ||||||
|         fields = ('food_container',) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class TransformedFoodForms(forms.ModelForm): | class TransformedFoodForms(forms.ModelForm): | ||||||
|     """ |     """ | ||||||
|     Form for add transformedfood |     Form for add transformedfood | ||||||
|     """ |     """ | ||||||
|     def __init__(self, *args, **kwargs): |     def __init__(self, *args, **kwargs): | ||||||
|         super().__init__(*args, **kwargs) |         super().__init__(*args, **kwargs) | ||||||
|         self.fields['name'].widget.attrs.update({"autofocus": "autofocus"}) |  | ||||||
|         self.fields['name'].required = True |         self.fields['name'].required = True | ||||||
|         self.fields['owner'].required = True |         self.fields['owner'].required = True | ||||||
|         self.fields['creation_date'].required = True |  | ||||||
|         self.fields['creation_date'].initial = timezone.now |  | ||||||
|         self.fields['is_active'].initial = True |  | ||||||
|         self.fields['is_ready'].initial = False |  | ||||||
|         self.fields['was_eaten'].initial = False |  | ||||||
|  |  | ||||||
|         # Some example |         # Some example | ||||||
|         self.fields['name'].widget.attrs.update({"placeholder": _("Lasagna")}) |         self.fields['name'].widget.attrs.update({"placeholder": _("Lasagna")}) | ||||||
|         clubs = list(Club.objects.filter(PermissionBackend.filter_queryset(get_current_request(), Club, "change")).all()) |         clubs = list(Club.objects.filter(PermissionBackend.filter_queryset(get_current_request(), Club, "change")).all()) | ||||||
|         shuffle(clubs) |         shuffle(clubs) | ||||||
|         self.fields['owner'].widget.attrs["placeholder"] = ", ".join(club.name for club in clubs[:4]) + ", ..." |         self.fields['owner'].widget.attrs["placeholder"] = ", ".join(club.name for club in clubs[:4]) + ", ..." | ||||||
|  |         self.fields['order'].widget.attrs["placeholder"] = _("Specific order given to GCKs") | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = TransformedFood |         model = TransformedFood | ||||||
|         fields = ('name', 'creation_date', 'owner', 'is_active', 'is_ready', 'was_eaten', 'shelf_life') |         fields = ('name', 'owner', 'order',) | ||||||
|         widgets = { |         widgets = { | ||||||
|             "owner": Autocomplete( |             "owner": Autocomplete( | ||||||
|                 model=Club, |                 model=Club, | ||||||
|                 attrs={"api_url": "/api/members/club/"}, |                 attrs={"api_url": "/api/members/club/"}, | ||||||
|             ), |             ), | ||||||
|             'creation_date': DateTimePickerInput(), |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class BasicFoodUpdateForms(forms.ModelForm): | ||||||
|  |     """ | ||||||
|  |     Form for update basicfood object | ||||||
|  |     """ | ||||||
|  |     class Meta: | ||||||
|  |         model = BasicFood | ||||||
|  |         fields = ('name', 'owner', 'date_type', 'expiry_date', 'end_of_life', 'is_ready', 'order', 'allergens') | ||||||
|  |         widgets = { | ||||||
|  |             "owner": Autocomplete( | ||||||
|  |                 model=Club, | ||||||
|  |                 attrs={"api_url": "/api/members/club/"}, | ||||||
|  |             ), | ||||||
|  |             "expiry_date": DateTimePickerInput(), | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TransformedFoodUpdateForms(forms.ModelForm): | ||||||
|  |     """ | ||||||
|  |     Form for update transformedfood object | ||||||
|  |     """ | ||||||
|  |     def __init__(self, *args, **kwargs): | ||||||
|  |         super().__init__(*args, **kwargs) | ||||||
|  |         self.fields['shelf_life'].label = _('Shelf life (in hours)') | ||||||
|  |  | ||||||
|  |     class Meta: | ||||||
|  |         model = TransformedFood | ||||||
|  |         fields = ('name', 'owner', 'end_of_life', 'is_ready', 'order', 'shelf_life') | ||||||
|  |         widgets = { | ||||||
|  |             "owner": Autocomplete( | ||||||
|  |                 model=Club, | ||||||
|  |                 attrs={"api_url": "/api/members/club/"}, | ||||||
|  |             ), | ||||||
|  |             "expiry_date": DateTimePickerInput(), | ||||||
|  |             "shelf_life": NumberInput(), | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class AddIngredientForms(forms.ModelForm): | ||||||
|  |     """ | ||||||
|  |     Form for add an ingredient | ||||||
|  |     """ | ||||||
|  |     fully_used = forms.BooleanField() | ||||||
|  |     fully_used.initial = True | ||||||
|  |     fully_used.required = False | ||||||
|  |     fully_used.label = _("Fully used") | ||||||
|  |  | ||||||
|  |     def __init__(self, *args, **kwargs): | ||||||
|  |         super().__init__(*args, **kwargs) | ||||||
|  |         # TODO find a better way to get pk (be not url scheme dependant) | ||||||
|  |         pk = get_current_request().path.split('/')[-1] | ||||||
|  |         self.fields['ingredients'].queryset = self.fields['ingredients'].queryset.filter( | ||||||
|  |             polymorphic_ctype__model="transformedfood", | ||||||
|  |             is_ready=False, | ||||||
|  |             end_of_life='', | ||||||
|  |         ).filter(PermissionBackend.filter_queryset(get_current_request(), Food, "change")).exclude(pk=pk) | ||||||
|  |  | ||||||
|  |     class Meta: | ||||||
|  |         model = TransformedFood | ||||||
|  |         fields = ('ingredients',) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class ManageIngredientsForm(forms.Form): | ||||||
|  |     """ | ||||||
|  |     Form to manage ingredient | ||||||
|  |     """ | ||||||
|  |     fully_used = forms.BooleanField() | ||||||
|  |     fully_used.initial = True | ||||||
|  |     fully_used.required = True | ||||||
|  |     fully_used.label = _('Fully used') | ||||||
|  |  | ||||||
|  |     name = forms.CharField() | ||||||
|  |     name.widget = Autocomplete( | ||||||
|  |         model=Food, | ||||||
|  |         resetable=True, | ||||||
|  |         attrs={"api_url": "/api/food/food", | ||||||
|  |                "class": "autocomplete"}, | ||||||
|  |     ) | ||||||
|  |     name.label = _('Name') | ||||||
|  |  | ||||||
|  |     qrcode = forms.IntegerField() | ||||||
|  |     qrcode.widget = Autocomplete( | ||||||
|  |         model=QRCode, | ||||||
|  |         resetable=True, | ||||||
|  |         attrs={"api_url": "/api/food/qrcode/", | ||||||
|  |                "name_field": "qr_code_number", | ||||||
|  |                "class": "autocomplete"}, | ||||||
|  |     ) | ||||||
|  |     qrcode.label = _('QR code number') | ||||||
|  |  | ||||||
|  |  | ||||||
|  | ManageIngredientsFormSet = forms.formset_factory( | ||||||
|  |     ManageIngredientsForm, | ||||||
|  |     extra=1, | ||||||
|  | ) | ||||||
|   | |||||||
| @@ -1,84 +1,199 @@ | |||||||
| # Generated by Django 2.2.28 on 2024-07-05 08:57 | # Generated by Django 4.2.20 on 2025-04-17 21:43 | ||||||
|  |  | ||||||
|  | import datetime | ||||||
| from django.db import migrations, models | from django.db import migrations, models | ||||||
| import django.db.models.deletion | import django.db.models.deletion | ||||||
| import django.utils.timezone | import django.utils.timezone | ||||||
|  |  | ||||||
|  |  | ||||||
| class Migration(migrations.Migration): | class Migration(migrations.Migration): | ||||||
|  |  | ||||||
|     initial = True |     initial = True | ||||||
|  |  | ||||||
|     dependencies = [ |     dependencies = [ | ||||||
|         ('contenttypes', '0002_remove_content_type_name'), |         ("contenttypes", "0002_remove_content_type_name"), | ||||||
|         ('member', '0011_profile_vss_charter_read'), |         ("member", "0013_auto_20240801_1436"), | ||||||
|     ] |     ] | ||||||
|  |  | ||||||
|     operations = [ |     operations = [ | ||||||
|         migrations.CreateModel( |         migrations.CreateModel( | ||||||
|             name='Allergen', |             name="Allergen", | ||||||
|             fields=[ |             fields=[ | ||||||
|                 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |                 ( | ||||||
|                 ('name', models.CharField(max_length=255, verbose_name='name')), |                     "id", | ||||||
|  |                     models.AutoField( | ||||||
|  |                         auto_created=True, | ||||||
|  |                         primary_key=True, | ||||||
|  |                         serialize=False, | ||||||
|  |                         verbose_name="ID", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ("name", models.CharField(max_length=255, verbose_name="name")), | ||||||
|             ], |             ], | ||||||
|             options={ |             options={ | ||||||
|                 'verbose_name': 'Allergen', |                 "verbose_name": "Allergen", | ||||||
|                 'verbose_name_plural': 'Allergens', |                 "verbose_name_plural": "Allergens", | ||||||
|             }, |             }, | ||||||
|         ), |         ), | ||||||
|         migrations.CreateModel( |         migrations.CreateModel( | ||||||
|             name='Food', |             name="Food", | ||||||
|             fields=[ |             fields=[ | ||||||
|                 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |                 ( | ||||||
|                 ('name', models.CharField(max_length=255, verbose_name='name')), |                     "id", | ||||||
|                 ('expiry_date', models.DateTimeField(verbose_name='expiry date')), |                     models.AutoField( | ||||||
|                 ('was_eaten', models.BooleanField(default=False, verbose_name='was eaten')), |                         auto_created=True, | ||||||
|                 ('is_ready', models.BooleanField(default=False, verbose_name='is ready')), |                         primary_key=True, | ||||||
|                 ('allergens', models.ManyToManyField(blank=True, to='food.Allergen', verbose_name='allergen')), |                         serialize=False, | ||||||
|                 ('owner', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='member.Club', verbose_name='owner')), |                         verbose_name="ID", | ||||||
|                 ('polymorphic_ctype', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='polymorphic_food.food_set+', to='contenttypes.ContentType')), |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ("name", models.CharField(max_length=255, verbose_name="name")), | ||||||
|  |                 ("expiry_date", models.DateTimeField(verbose_name="expiry date")), | ||||||
|  |                 ( | ||||||
|  |                     "end_of_life", | ||||||
|  |                     models.CharField(max_length=255, verbose_name="end of life"), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "is_ready", | ||||||
|  |                     models.BooleanField(max_length=255, verbose_name="is ready"), | ||||||
|  |                 ), | ||||||
|  |                 ("order", models.CharField(max_length=255, verbose_name="order")), | ||||||
|  |                 ( | ||||||
|  |                     "allergens", | ||||||
|  |                     models.ManyToManyField( | ||||||
|  |                         blank=True, to="food.allergen", verbose_name="allergens" | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "owner", | ||||||
|  |                     models.ForeignKey( | ||||||
|  |                         on_delete=django.db.models.deletion.PROTECT, | ||||||
|  |                         related_name="+", | ||||||
|  |                         to="member.club", | ||||||
|  |                         verbose_name="owner", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "polymorphic_ctype", | ||||||
|  |                     models.ForeignKey( | ||||||
|  |                         editable=False, | ||||||
|  |                         null=True, | ||||||
|  |                         on_delete=django.db.models.deletion.CASCADE, | ||||||
|  |                         related_name="polymorphic_%(app_label)s.%(class)s_set+", | ||||||
|  |                         to="contenttypes.contenttype", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|             ], |             ], | ||||||
|             options={ |             options={ | ||||||
|                 'verbose_name': 'foods', |                 "verbose_name": "Food", | ||||||
|  |                 "verbose_name_plural": "Foods", | ||||||
|             }, |             }, | ||||||
|         ), |         ), | ||||||
|         migrations.CreateModel( |         migrations.CreateModel( | ||||||
|             name='BasicFood', |             name="BasicFood", | ||||||
|             fields=[ |             fields=[ | ||||||
|                 ('food_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='food.Food')), |                 ( | ||||||
|                 ('date_type', models.CharField(choices=[('DLC', 'DLC'), ('DDM', 'DDM')], max_length=255)), |                     "food_ptr", | ||||||
|                 ('arrival_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='arrival date')), |                     models.OneToOneField( | ||||||
|  |                         auto_created=True, | ||||||
|  |                         on_delete=django.db.models.deletion.CASCADE, | ||||||
|  |                         parent_link=True, | ||||||
|  |                         primary_key=True, | ||||||
|  |                         serialize=False, | ||||||
|  |                         to="food.food", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "arrival_date", | ||||||
|  |                     models.DateTimeField( | ||||||
|  |                         default=django.utils.timezone.now, verbose_name="arrival date" | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "date_type", | ||||||
|  |                     models.CharField( | ||||||
|  |                         choices=[("DLC", "DLC"), ("DDM", "DDM")], max_length=255 | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|             ], |             ], | ||||||
|             options={ |             options={ | ||||||
|                 'verbose_name': 'Basic food', |                 "verbose_name": "Basic food", | ||||||
|                 'verbose_name_plural': 'Basic foods', |                 "verbose_name_plural": "Basic foods", | ||||||
|             }, |             }, | ||||||
|             bases=('food.food',), |             bases=("food.food",), | ||||||
|         ), |         ), | ||||||
|         migrations.CreateModel( |         migrations.CreateModel( | ||||||
|             name='QRCode', |             name="QRCode", | ||||||
|             fields=[ |             fields=[ | ||||||
|                 ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |                 ( | ||||||
|                 ('qr_code_number', models.PositiveIntegerField(unique=True, verbose_name='QR-code number')), |                     "id", | ||||||
|                 ('food_container', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='QR_code', to='food.Food', verbose_name='food container')), |                     models.AutoField( | ||||||
|  |                         auto_created=True, | ||||||
|  |                         primary_key=True, | ||||||
|  |                         serialize=False, | ||||||
|  |                         verbose_name="ID", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "qr_code_number", | ||||||
|  |                     models.PositiveIntegerField( | ||||||
|  |                         unique=True, verbose_name="qr code number" | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "food_container", | ||||||
|  |                     models.ForeignKey( | ||||||
|  |                         on_delete=django.db.models.deletion.CASCADE, | ||||||
|  |                         related_name="QR_code", | ||||||
|  |                         to="food.food", | ||||||
|  |                         verbose_name="food container", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|             ], |             ], | ||||||
|             options={ |             options={ | ||||||
|                 'verbose_name': 'QR-code', |                 "verbose_name": "QR-code", | ||||||
|                 'verbose_name_plural': 'QR-codes', |                 "verbose_name_plural": "QR-codes", | ||||||
|             }, |             }, | ||||||
|         ), |         ), | ||||||
|         migrations.CreateModel( |         migrations.CreateModel( | ||||||
|             name='TransformedFood', |             name="TransformedFood", | ||||||
|             fields=[ |             fields=[ | ||||||
|                 ('food_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='food.Food')), |                 ( | ||||||
|                 ('creation_date', models.DateTimeField(verbose_name='creation date')), |                     "food_ptr", | ||||||
|                 ('is_active', models.BooleanField(default=True, verbose_name='is active')), |                     models.OneToOneField( | ||||||
|                 ('ingredient', models.ManyToManyField(blank=True, related_name='transformed_ingredient_inv', to='food.Food', verbose_name='transformed ingredient')), |                         auto_created=True, | ||||||
|  |                         on_delete=django.db.models.deletion.CASCADE, | ||||||
|  |                         parent_link=True, | ||||||
|  |                         primary_key=True, | ||||||
|  |                         serialize=False, | ||||||
|  |                         to="food.food", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "creation_date", | ||||||
|  |                     models.DateTimeField( | ||||||
|  |                         default=django.utils.timezone.now, verbose_name="creation date" | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "shelf_life", | ||||||
|  |                     models.DurationField( | ||||||
|  |                         default=datetime.timedelta(days=3), verbose_name="shelf life" | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|  |                 ( | ||||||
|  |                     "ingredients", | ||||||
|  |                     models.ManyToManyField( | ||||||
|  |                         blank=True, | ||||||
|  |                         related_name="transformed_ingredient_inv", | ||||||
|  |                         to="food.food", | ||||||
|  |                         verbose_name="transformed ingredient", | ||||||
|  |                     ), | ||||||
|  |                 ), | ||||||
|             ], |             ], | ||||||
|             options={ |             options={ | ||||||
|                 'verbose_name': 'Transformed food', |                 "verbose_name": "Transformed food", | ||||||
|                 'verbose_name_plural': 'Transformed foods', |                 "verbose_name_plural": "Transformed foods", | ||||||
|             }, |             }, | ||||||
|             bases=('food.food',), |             bases=("food.food",), | ||||||
|         ), |         ), | ||||||
|     ] |     ] | ||||||
|   | |||||||
| @@ -1,19 +0,0 @@ | |||||||
| # Generated by Django 2.2.28 on 2024-07-06 20:37 |  | ||||||
|  |  | ||||||
| import datetime |  | ||||||
| from django.db import migrations, models |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Migration(migrations.Migration): |  | ||||||
|  |  | ||||||
|     dependencies = [ |  | ||||||
|         ('food', '0001_initial'), |  | ||||||
|     ] |  | ||||||
|  |  | ||||||
|     operations = [ |  | ||||||
|         migrations.AddField( |  | ||||||
|             model_name='transformedfood', |  | ||||||
|             name='shelf_life', |  | ||||||
|             field=models.DurationField(default=datetime.timedelta(days=3), verbose_name='shelf life'), |  | ||||||
|         ), |  | ||||||
|     ] |  | ||||||
| @@ -1,62 +0,0 @@ | |||||||
| from django.db import migrations |  | ||||||
|  |  | ||||||
| def create_14_mandatory_allergens(apps, schema_editor): |  | ||||||
|     """ |  | ||||||
|     There are 14 mandatory allergens, they are pre-injected |  | ||||||
|     """ |  | ||||||
|  |  | ||||||
|     Allergen = apps.get_model("food", "allergen") |  | ||||||
|      |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Gluten", |  | ||||||
|     )  |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Fruits à coques", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Crustacés", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Céléri", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Oeufs", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Moutarde", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Poissons", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Soja", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Lait", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Sulfites", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Sésame", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Lupin", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Arachides", |  | ||||||
|     ) |  | ||||||
|     Allergen.objects.get_or_create( |  | ||||||
|         name="Mollusques", |  | ||||||
|     ) |  | ||||||
|  |  | ||||||
| class Migration(migrations.Migration): |  | ||||||
|     dependencies = [ |  | ||||||
|         ('food', '0002_transformedfood_shelf_life'), |  | ||||||
|     ] |  | ||||||
|  |  | ||||||
|     operations = [ |  | ||||||
|         migrations.RunPython(create_14_mandatory_allergens), |  | ||||||
|     ] |  | ||||||
|      |  | ||||||
|      |  | ||||||
| @@ -1,28 +0,0 @@ | |||||||
| # Generated by Django 2.2.28 on 2024-08-13 21:58 |  | ||||||
|  |  | ||||||
| from django.db import migrations, models |  | ||||||
| import django.db.models.deletion |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Migration(migrations.Migration): |  | ||||||
|  |  | ||||||
|     dependencies = [ |  | ||||||
|         ('food', '0003_create_14_allergens_mandatory'), |  | ||||||
|     ] |  | ||||||
|  |  | ||||||
|     operations = [ |  | ||||||
|         migrations.RemoveField( |  | ||||||
|             model_name='transformedfood', |  | ||||||
|             name='is_active', |  | ||||||
|         ), |  | ||||||
|         migrations.AddField( |  | ||||||
|             model_name='food', |  | ||||||
|             name='is_active', |  | ||||||
|             field=models.BooleanField(default=True, verbose_name='is active'), |  | ||||||
|         ), |  | ||||||
|         migrations.AlterField( |  | ||||||
|             model_name='qrcode', |  | ||||||
|             name='food_container', |  | ||||||
|             field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='QR_code', to='food.Food', verbose_name='food container'), |  | ||||||
|         ), |  | ||||||
|     ] |  | ||||||
| @@ -1,20 +0,0 @@ | |||||||
| # Generated by Django 4.2.15 on 2024-08-28 08:00 |  | ||||||
|  |  | ||||||
| from django.db import migrations, models |  | ||||||
| import django.db.models.deletion |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Migration(migrations.Migration): |  | ||||||
|  |  | ||||||
|     dependencies = [ |  | ||||||
|         ('contenttypes', '0002_remove_content_type_name'), |  | ||||||
|         ('food', '0004_auto_20240813_2358'), |  | ||||||
|     ] |  | ||||||
|  |  | ||||||
|     operations = [ |  | ||||||
|         migrations.AlterField( |  | ||||||
|             model_name='food', |  | ||||||
|             name='polymorphic_ctype', |  | ||||||
|             field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='polymorphic_%(app_label)s.%(class)s_set+', to='contenttypes.contenttype'), |  | ||||||
|         ), |  | ||||||
|     ] |  | ||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from datetime import timedelta | from datetime import timedelta | ||||||
| @@ -6,37 +6,13 @@ from datetime import timedelta | |||||||
| from django.db import models, transaction | from django.db import models, transaction | ||||||
| from django.utils import timezone | from django.utils import timezone | ||||||
| from django.utils.translation import gettext_lazy as _ | from django.utils.translation import gettext_lazy as _ | ||||||
| from member.models import Club |  | ||||||
| from polymorphic.models import PolymorphicModel | from polymorphic.models import PolymorphicModel | ||||||
|  | from member.models import Club | ||||||
|  |  | ||||||
| class QRCode(models.Model): |  | ||||||
|     """ |  | ||||||
|     An QRCode model |  | ||||||
|     """ |  | ||||||
|     qr_code_number = models.PositiveIntegerField( |  | ||||||
|         verbose_name=_("QR-code number"), |  | ||||||
|         unique=True, |  | ||||||
|     ) |  | ||||||
|  |  | ||||||
|     food_container = models.ForeignKey( |  | ||||||
|         'Food', |  | ||||||
|         on_delete=models.CASCADE, |  | ||||||
|         related_name='QR_code', |  | ||||||
|         verbose_name=_('food container'), |  | ||||||
|     ) |  | ||||||
|  |  | ||||||
|     class Meta: |  | ||||||
|         verbose_name = _("QR-code") |  | ||||||
|         verbose_name_plural = _("QR-codes") |  | ||||||
|  |  | ||||||
|     def __str__(self): |  | ||||||
|         return _("QR-code number {qr_code_number}").format(qr_code_number=self.qr_code_number) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Allergen(models.Model): | class Allergen(models.Model): | ||||||
|     """ |     """ | ||||||
|     A list of allergen and alimentary restrictions |     Allergen and alimentary restrictions | ||||||
|     """ |     """ | ||||||
|     name = models.CharField( |     name = models.CharField( | ||||||
|         verbose_name=_('name'), |         verbose_name=_('name'), | ||||||
| @@ -44,16 +20,19 @@ class Allergen(models.Model): | |||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         verbose_name = _('Allergen') |         verbose_name = _("Allergen") | ||||||
|         verbose_name_plural = _('Allergens') |         verbose_name_plural = _("Allergens") | ||||||
|  |  | ||||||
|     def __str__(self): |     def __str__(self): | ||||||
|         return self.name |         return self.name | ||||||
|  |  | ||||||
|  |  | ||||||
| class Food(PolymorphicModel): | class Food(PolymorphicModel): | ||||||
|  |     """ | ||||||
|  |     Describe any type of food | ||||||
|  |     """ | ||||||
|     name = models.CharField( |     name = models.CharField( | ||||||
|         verbose_name=_('name'), |         verbose_name=_("name"), | ||||||
|         max_length=255, |         max_length=255, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
| @@ -67,7 +46,7 @@ class Food(PolymorphicModel): | |||||||
|     allergens = models.ManyToManyField( |     allergens = models.ManyToManyField( | ||||||
|         Allergen, |         Allergen, | ||||||
|         blank=True, |         blank=True, | ||||||
|         verbose_name=_('allergen'), |         verbose_name=_('allergens'), | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     expiry_date = models.DateTimeField( |     expiry_date = models.DateTimeField( | ||||||
| @@ -75,41 +54,69 @@ class Food(PolymorphicModel): | |||||||
|         null=False, |         null=False, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     was_eaten = models.BooleanField( |     end_of_life = models.CharField( | ||||||
|         default=False, |         blank=True, | ||||||
|         verbose_name=_('was eaten'), |         verbose_name=_('end of life'), | ||||||
|  |         max_length=255, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     # is_ready != is_active : is_ready signifie que la nourriture est prête à être manger, |  | ||||||
|     #                         is_active signifie que la nourriture n'est pas encore archivé |  | ||||||
|     # il sert dans les cas où il est plus intéressant que de l'open soit conservé (confiture par ex) |  | ||||||
|  |  | ||||||
|     is_ready = models.BooleanField( |     is_ready = models.BooleanField( | ||||||
|         default=False, |  | ||||||
|         verbose_name=_('is ready'), |         verbose_name=_('is ready'), | ||||||
|  |         max_length=255, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     is_active = models.BooleanField( |     order = models.CharField( | ||||||
|         default=True, |         blank=True, | ||||||
|         verbose_name=_('is active'), |         verbose_name=_('order'), | ||||||
|  |         max_length=255, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     def __str__(self): |     def __str__(self): | ||||||
|         return self.name |         return self.name | ||||||
|  |  | ||||||
|     @transaction.atomic |     @transaction.atomic | ||||||
|     def save(self, force_insert=False, force_update=False, using=None, update_fields=None): |     def update_allergens(self): | ||||||
|         return super().save(force_insert, force_update, using, update_fields) |         # update parents | ||||||
|  |         for parent in self.transformed_ingredient_inv.iterator(): | ||||||
|  |             old_allergens = list(parent.allergens.all()).copy() | ||||||
|  |             parent.allergens.clear() | ||||||
|  |             for child in parent.ingredients.iterator(): | ||||||
|  |                 if child.pk != self.pk: | ||||||
|  |                     parent.allergens.set(parent.allergens.union(child.allergens.all())) | ||||||
|  |             parent.allergens.set(parent.allergens.union(self.allergens.all())) | ||||||
|  |             if old_allergens != list(parent.allergens.all()): | ||||||
|  |                 parent.save(old_allergens=old_allergens) | ||||||
|  |  | ||||||
|  |     def update_expiry_date(self): | ||||||
|  |         # update parents | ||||||
|  |         for parent in self.transformed_ingredient_inv.iterator(): | ||||||
|  |             old_expiry_date = parent.expiry_date | ||||||
|  |             parent.expiry_date = parent.shelf_life + parent.creation_date | ||||||
|  |             for child in parent.ingredients.iterator(): | ||||||
|  |                 if (child.pk != self.pk | ||||||
|  |                     and not (child.polymorphic_ctype.model == 'basicfood' | ||||||
|  |                              and child.date_type == 'DDM')): | ||||||
|  |                     parent.expiry_date = min(parent.expiry_date, child.expiry_date) | ||||||
|  |  | ||||||
|  |             if self.polymorphic_ctype.model == 'basicfood' and self.date_type == 'DLC': | ||||||
|  |                 parent.expiry_date = min(parent.expiry_date, self.expiry_date) | ||||||
|  |             if old_expiry_date != parent.expiry_date: | ||||||
|  |                 parent.save() | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         verbose_name = _('food') |         verbose_name = _('Food') | ||||||
|         verbose_name = _('foods') |         verbose_name_plural = _('Foods') | ||||||
|  |  | ||||||
|  |  | ||||||
| class BasicFood(Food): | class BasicFood(Food): | ||||||
|     """ |     """ | ||||||
|     Food which has been directly buy on supermarket |     A basic food is a food directly buy and stored | ||||||
|     """ |     """ | ||||||
|  |     arrival_date = models.DateTimeField( | ||||||
|  |         default=timezone.now, | ||||||
|  |         verbose_name=_('arrival date'), | ||||||
|  |     ) | ||||||
|  |  | ||||||
|     date_type = models.CharField( |     date_type = models.CharField( | ||||||
|         max_length=255, |         max_length=255, | ||||||
|         choices=( |         choices=( | ||||||
| @@ -118,50 +125,70 @@ class BasicFood(Food): | |||||||
|         ) |         ) | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     arrival_date = models.DateTimeField( |  | ||||||
|         verbose_name=_('arrival date'), |  | ||||||
|         default=timezone.now, |  | ||||||
|     ) |  | ||||||
|  |  | ||||||
|     # label = models.ImageField( |  | ||||||
|     #     verbose_name=_('food label'), |  | ||||||
|     #     max_length=255, |  | ||||||
|     #     blank=False, |  | ||||||
|     #     null=False, |  | ||||||
|     #     upload_to='label/', |  | ||||||
|     # ) |  | ||||||
|  |  | ||||||
|     @transaction.atomic |     @transaction.atomic | ||||||
|     def update_allergens(self): |     def save(self, force_insert=False, force_update=False, using=None, update_fields=None, **kwargs): | ||||||
|         # update parents |         created = self.pk is None | ||||||
|         for parent in self.transformed_ingredient_inv.iterator(): |         if not created: | ||||||
|             parent.update_allergens() |             # Check if important fields are updated | ||||||
|  |             old_food = Food.objects.select_for_update().get(pk=self.pk) | ||||||
|  |             if not hasattr(self, "_force_save"): | ||||||
|  |                 # Allergens | ||||||
|  |  | ||||||
|     @transaction.atomic |                 if ('old_allergens' in kwargs | ||||||
|     def update_expiry_date(self): |                         and list(self.allergens.all()) != kwargs['old_allergens']): | ||||||
|         # update parents |  | ||||||
|         for parent in self.transformed_ingredient_inv.iterator(): |  | ||||||
|             parent.update_expiry_date() |  | ||||||
|  |  | ||||||
|     @transaction.atomic |  | ||||||
|     def update(self): |  | ||||||
|                     self.update_allergens() |                     self.update_allergens() | ||||||
|  |  | ||||||
|  |                 # Expiry date | ||||||
|  |                 if ((self.expiry_date != old_food.expiry_date | ||||||
|  |                         and self.date_type == 'DLC') | ||||||
|  |                         or old_food.date_type != self.date_type): | ||||||
|                     self.update_expiry_date() |                     self.update_expiry_date() | ||||||
|  |  | ||||||
|  |         return super().save(force_insert, force_update, using, update_fields) | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|  |     def get_lastests_objects(number, distinct_field, order_by_field): | ||||||
|  |         """ | ||||||
|  |         Get the last object with distinct field and ranked with order_by | ||||||
|  |         This methods exist because we can't distinct with one field and | ||||||
|  |         order with another | ||||||
|  |         """ | ||||||
|  |         foods = BasicFood.objects.order_by(order_by_field).all() | ||||||
|  |         field = [] | ||||||
|  |         for food in foods: | ||||||
|  |             if getattr(food, distinct_field) in field: | ||||||
|  |                 continue | ||||||
|  |             else: | ||||||
|  |                 field.append(getattr(food, distinct_field)) | ||||||
|  |                 number -= 1 | ||||||
|  |                 yield food | ||||||
|  |             if not number: | ||||||
|  |                 return | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         verbose_name = _('Basic food') |         verbose_name = _('Basic food') | ||||||
|         verbose_name_plural = _('Basic foods') |         verbose_name_plural = _('Basic foods') | ||||||
|  |  | ||||||
|  |     def __str__(self): | ||||||
|  |         return self.name | ||||||
|  |  | ||||||
|  |  | ||||||
| class TransformedFood(Food): | class TransformedFood(Food): | ||||||
|     """ |     """ | ||||||
|     Transformed food  are a mix between basic food and meal |     A transformed food is a food with ingredients | ||||||
|     """ |     """ | ||||||
|     creation_date = models.DateTimeField( |     creation_date = models.DateTimeField( | ||||||
|  |         default=timezone.now, | ||||||
|         verbose_name=_('creation date'), |         verbose_name=_('creation date'), | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     ingredient = models.ManyToManyField( |     # Without microbiological analyzes, the storage time is 3 days | ||||||
|  |     shelf_life = models.DurationField( | ||||||
|  |         default=timedelta(days=3), | ||||||
|  |         verbose_name=_('shelf life'), | ||||||
|  |     ) | ||||||
|  |  | ||||||
|  |     ingredients = models.ManyToManyField( | ||||||
|         Food, |         Food, | ||||||
|         blank=True, |         blank=True, | ||||||
|         symmetrical=False, |         symmetrical=False, | ||||||
| @@ -169,58 +196,91 @@ class TransformedFood(Food): | |||||||
|         verbose_name=_('transformed ingredient'), |         verbose_name=_('transformed ingredient'), | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     # Without microbiological analyzes, the storage time is 3 days |     def check_cycle(self, ingredients, origin, checked): | ||||||
|     shelf_life = models.DurationField( |         for ingredient in ingredients: | ||||||
|         verbose_name=_("shelf life"), |             if ingredient == origin: | ||||||
|         default=timedelta(days=3), |                 # We break the cycle | ||||||
|     ) |                 self.ingredients.remove(ingredient) | ||||||
|  |             if ingredient.polymorphic_ctype.model == 'transformedfood' and ingredient not in checked: | ||||||
|  |                 ingredient.check_cycle(ingredient.ingredients.all(), origin, checked) | ||||||
|  |                 checked.append(ingredient) | ||||||
|  |  | ||||||
|     @transaction.atomic |     @transaction.atomic | ||||||
|     def archive(self): |     def save(self, force_insert=False, force_update=False, using=None, update_fields=None, **kwargs): | ||||||
|         # When a meal are archived, if it was eaten, update ingredient fully used for this meal |         created = self.pk is None | ||||||
|         raise NotImplementedError |         if not created: | ||||||
|  |             # Check if important fields are updated | ||||||
|  |             update = {'allergens': False, 'expiry_date': False} | ||||||
|  |             old_food = Food.objects.select_for_update().get(pk=self.pk) | ||||||
|  |             if not hasattr(self, "_force_save"): | ||||||
|  |                 # Allergens | ||||||
|  |                 # Unfortunately with the many-to-many relation we can't access | ||||||
|  |                 # to old allergens | ||||||
|  |                 if ('old_allergens' in kwargs | ||||||
|  |                         and list(self.allergens.all()) != kwargs['old_allergens']): | ||||||
|  |                     update['allergens'] = True | ||||||
|  |  | ||||||
|     @transaction.atomic |                 # Expiry date | ||||||
|     def update_allergens(self): |                 update['expiry_date'] = (self.shelf_life != old_food.shelf_life | ||||||
|         # When allergens are changed, simply update the parents' allergens |                                          or self.creation_date != old_food.creation_date) | ||||||
|         old_allergens = list(self.allergens.all()) |                 if update['expiry_date']: | ||||||
|         self.allergens.clear() |  | ||||||
|         for ingredient in self.ingredient.iterator(): |  | ||||||
|             self.allergens.set(self.allergens.union(ingredient.allergens.all())) |  | ||||||
|  |  | ||||||
|         if old_allergens == list(self.allergens.all()): |  | ||||||
|             return |  | ||||||
|         super().save() |  | ||||||
|  |  | ||||||
|         # update parents |  | ||||||
|         for parent in self.transformed_ingredient_inv.iterator(): |  | ||||||
|             parent.update_allergens() |  | ||||||
|  |  | ||||||
|     @transaction.atomic |  | ||||||
|     def update_expiry_date(self): |  | ||||||
|         # When expiry_date is changed, simply update the parents' expiry_date |  | ||||||
|         old_expiry_date = self.expiry_date |  | ||||||
|                     self.expiry_date = self.creation_date + self.shelf_life |                     self.expiry_date = self.creation_date + self.shelf_life | ||||||
|         for ingredient in self.ingredient.iterator(): |                 # Unfortunately with the set method ingredients are already save, | ||||||
|             self.expiry_date = min(self.expiry_date, ingredient.expiry_date) |                 # we check cycle after if possible | ||||||
|  |                 if ('old_ingredients' in kwargs | ||||||
|  |                         and list(self.ingredients.all()) != list(kwargs['old_ingredients'])): | ||||||
|  |                     update['allergens'] = True | ||||||
|  |                     update['expiry_date'] = True | ||||||
|  |  | ||||||
|         if old_expiry_date == self.expiry_date: |                     # it's preferable to keep a queryset but we allow list too | ||||||
|             return |                     if type(kwargs['old_ingredients']) is list: | ||||||
|         super().save() |                         kwargs['old_ingredients'] = Food.objects.filter( | ||||||
|  |                             pk__in=[food.pk for food in kwargs['old_ingredients']]) | ||||||
|         # update parents |                     self.check_cycle(self.ingredients.all().difference(kwargs['old_ingredients']), self, []) | ||||||
|         for parent in self.transformed_ingredient_inv.iterator(): |                 if update['allergens']: | ||||||
|             parent.update_expiry_date() |  | ||||||
|  |  | ||||||
|     @transaction.atomic |  | ||||||
|     def update(self): |  | ||||||
|                     self.update_allergens() |                     self.update_allergens() | ||||||
|  |                 if update['expiry_date']: | ||||||
|                     self.update_expiry_date() |                     self.update_expiry_date() | ||||||
|  |  | ||||||
|     @transaction.atomic |         if created: | ||||||
|     def save(self, *args, **kwargs): |             self.expiry_date = self.shelf_life + self.creation_date | ||||||
|         super().save(*args, **kwargs) |  | ||||||
|  |             # We save here because we need pk for many-to-many relation | ||||||
|  |             super().save(force_insert, force_update, using, update_fields) | ||||||
|  |  | ||||||
|  |             for child in self.ingredients.iterator(): | ||||||
|  |                 self.allergens.set(self.allergens.union(child.allergens.all())) | ||||||
|  |                 if not (child.polymorphic_ctype.model == 'basicfood' and child.date_type == 'DDM'): | ||||||
|  |                     self.expiry_date = min(self.expiry_date, child.expiry_date) | ||||||
|  |         return super().save(force_insert, force_update, using, update_fields) | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         verbose_name = _('Transformed food') |         verbose_name = _('Transformed food') | ||||||
|         verbose_name_plural = _('Transformed foods') |         verbose_name_plural = _('Transformed foods') | ||||||
|  |  | ||||||
|  |     def __str__(self): | ||||||
|  |         return self.name | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class QRCode(models.Model): | ||||||
|  |     """ | ||||||
|  |     QR-code for register food | ||||||
|  |     """ | ||||||
|  |     qr_code_number = models.PositiveIntegerField( | ||||||
|  |         unique=True, | ||||||
|  |         verbose_name=_('qr code number'), | ||||||
|  |     ) | ||||||
|  |  | ||||||
|  |     food_container = models.ForeignKey( | ||||||
|  |         Food, | ||||||
|  |         on_delete=models.CASCADE, | ||||||
|  |         related_name='QR_code', | ||||||
|  |         verbose_name=_('food container'), | ||||||
|  |     ) | ||||||
|  |  | ||||||
|  |     class Meta: | ||||||
|  |         verbose_name = _('QR-code') | ||||||
|  |         verbose_name_plural = _('QR-codes') | ||||||
|  |  | ||||||
|  |     def __str__(self): | ||||||
|  |         return _('QR-code number') + ' ' + str(self.qr_code_number) | ||||||
|   | |||||||
| @@ -1,19 +1,21 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import django_tables2 as tables | import django_tables2 as tables | ||||||
| from django_tables2 import A |  | ||||||
|  |  | ||||||
| from .models import TransformedFood | from .models import Food | ||||||
|  |  | ||||||
|  |  | ||||||
| class TransformedFoodTable(tables.Table): | class FoodTable(tables.Table): | ||||||
|     name = tables.LinkColumn( |     """ | ||||||
|         'food:food_view', |     List all foods. | ||||||
|         args=[A('pk'), ], |     """ | ||||||
|     ) |  | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = TransformedFood |         model = Food | ||||||
|         template_name = 'django_tables2/bootstrap4.html' |         template_name = 'django_tables2/bootstrap4.html' | ||||||
|         fields = ('name', "owner", "allergens", "expiry_date") |         fields = ('name', 'owner', 'allergens', 'expiry_date') | ||||||
|  |         row_attrs = { | ||||||
|  |             'class': 'table-row', | ||||||
|  |             'data-href': lambda record: 'detail/' + str(record.pk), | ||||||
|  |             'style': 'cursor:pointer', | ||||||
|  |         } | ||||||
|   | |||||||
| @@ -1,20 +0,0 @@ | |||||||
| {% extends "base.html" %} |  | ||||||
| {% comment %} |  | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later |  | ||||||
| {% endcomment %} |  | ||||||
| {% load i18n crispy_forms_tags %} |  | ||||||
|  |  | ||||||
| {% block content %} |  | ||||||
| <div class="card bg-white mb-3"> |  | ||||||
|   <h3 class="card-header text-center"> |  | ||||||
|     {{ title }} |  | ||||||
|   </h3> |  | ||||||
|   <div class="card-body" id="form"> |  | ||||||
|     <form method="post"> |  | ||||||
|       {%  csrf_token %} |  | ||||||
|       {{ form|crispy }} |  | ||||||
|       <button class="btn btn-primary" type="submit">{% trans "Submit"%}</button> |  | ||||||
|     </form> |  | ||||||
|   </div> |  | ||||||
| </div> |  | ||||||
| {% endblock %} |  | ||||||
| @@ -1,37 +0,0 @@ | |||||||
| {% extends "base.html" %} |  | ||||||
| {% comment %} |  | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later |  | ||||||
| {% endcomment %} |  | ||||||
| {% load i18n crispy_forms_tags %} |  | ||||||
|  |  | ||||||
| {% block content %} |  | ||||||
| <div class="card bg-white mb-3"> |  | ||||||
|   <h3 class="card-header text-center"> |  | ||||||
|     {{ title }} {{ food.name }} |  | ||||||
|   </h3> |  | ||||||
|   <div class="card-body"> |  | ||||||
|     <ul> |  | ||||||
|       <li><p>{% trans 'Owner' %} : {{ food.owner }}</p></li> |  | ||||||
|       <li><p>{% trans 'Arrival date' %} : {{ food.arrival_date }}</p></li> |  | ||||||
|       <li><p>{% trans 'Expiry date' %} : {{ food.expiry_date }} ({{ food.date_type }})</p></li> |  | ||||||
|       <li>{% trans 'Allergens' %} :</li> |  | ||||||
|       <ul> |  | ||||||
|       {% for allergen in food.allergens.iterator %} |  | ||||||
|         <li>{{ allergen.name }}</li> |  | ||||||
|       {% endfor %} |  | ||||||
|       </ul> |  | ||||||
| 	<p> |  | ||||||
| 	<li><p>{% trans 'Active' %} : {{ food.is_active }}<p></li> |  | ||||||
| 	<li><p>{% trans 'Eaten' %} : {{ food.was_eaten }}<p></li> |  | ||||||
|     </ul> |  | ||||||
|     {% if can_update %} |  | ||||||
| 	<a class="btn btn-sm btn-warning" href="{% url "food:basic_update" pk=food.pk %}">{% trans 'Update' %}</a> |  | ||||||
|     {% endif %} |  | ||||||
|     {% if can_add_ingredient %} |  | ||||||
|     	<a class="btn btn-sm btn-success" href="{% url "food:add_ingredient" pk=food.pk %}"> |  | ||||||
| 		{% trans 'Add to a meal' %} |  | ||||||
| 	</a> |  | ||||||
|     {% endif %} |  | ||||||
|   </div> |  | ||||||
| </div> |  | ||||||
| {% endblock %} |  | ||||||
| @@ -1,55 +0,0 @@ | |||||||
| {% extends "base.html" %} |  | ||||||
| {% comment %} |  | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later |  | ||||||
| {% endcomment %} |  | ||||||
| {% load render_table from django_tables2 %} |  | ||||||
| {% load i18n crispy_forms_tags %} |  | ||||||
|  |  | ||||||
| {% block content %} |  | ||||||
| <div class="card bg-white mb-3"> |  | ||||||
|   <h3 class="card-header text-center"> |  | ||||||
|     {{ title }} |  | ||||||
|   </h3> |  | ||||||
|   <div class="card-body" id="form"> |  | ||||||
|     <a class="btn btn-sm btn-success" href="{% url "food:qrcode_basic_create" slug=slug %}"> |  | ||||||
|       {% trans 'New basic food' %} |  | ||||||
|     </a> |  | ||||||
|     <form method="post"> |  | ||||||
|       {%  csrf_token %} |  | ||||||
|       {{ form|crispy }} |  | ||||||
|       <button class="btn btn-primary" type="submit">{% trans "Submit" %}</button> |  | ||||||
|     </form> |  | ||||||
|     <div class="card-body" id="profile_infos"> |  | ||||||
|       <h4>{% trans "Copy constructor" %}</h4> |  | ||||||
|       <table class="table"> |  | ||||||
|         <thead> |  | ||||||
|           <tr> |  | ||||||
|             <th class="orderable"> |  | ||||||
|               {% trans "Name" %} |  | ||||||
|             </th> |  | ||||||
|             <th class="orderable"> |  | ||||||
|               {% trans "Owner" %} |  | ||||||
|             </th> |  | ||||||
|             <th class="orderable"> |  | ||||||
|               {% trans "Arrival date" %} |  | ||||||
|             </th> |  | ||||||
|             <th class="orderable"> |  | ||||||
|               {% trans "Expiry date" %} |  | ||||||
|             </th> |  | ||||||
|           </tr> |  | ||||||
|         </thead> |  | ||||||
|         <tbody> |  | ||||||
|           {% for basic in last_basic %} |  | ||||||
|             <tr> |  | ||||||
|               <td><a href="{% url "food:qrcode_basic_create" slug=slug %}?copy={{ basic.pk }}">{{ basic.name }}</a></td> |  | ||||||
|               <td>{{ basic.owner }}</td> |  | ||||||
|               <td>{{ basic.arrival_date }}</td> |  | ||||||
|               <td>{{ basic.expiry_date }}</td> |  | ||||||
|             </tr> |  | ||||||
|           {% endfor %} |  | ||||||
|         </tbody> |  | ||||||
|       </table> |  | ||||||
|     </div> |  | ||||||
|   </div> |  | ||||||
| </div> |  | ||||||
| {% endblock %} |  | ||||||
							
								
								
									
										56
									
								
								apps/food/templates/food/food_detail.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								apps/food/templates/food/food_detail.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | {% extends "base.html" %} | ||||||
|  | {% comment %} | ||||||
|  | Copyright (C) by BDE ENS Paris-Saclay | ||||||
|  | SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  | {% endcomment %} | ||||||
|  | {% load i18n crispy_forms_tags %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  | <div class="card bg-white mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {{ title }} {{ food.name }} | ||||||
|  |   </h3> | ||||||
|  |   <div class="card-body"> | ||||||
|  |     <ul> | ||||||
|  |       {% if QR_code %} | ||||||
|  |       <li> {{QR_code}} </li> | ||||||
|  |       {% endif %} | ||||||
|  |       {% for field, value in fields %} | ||||||
|  |       <li> {{ field }} : {{ value }}</li> | ||||||
|  |       {% endfor %} | ||||||
|  |       {% if meals %} | ||||||
|  |       <li> {% trans "Contained in" %} : | ||||||
|  |       {% for meal in meals %} | ||||||
|  |       <a href="{% url "food:transformedfood_view" pk=meal.pk %}">{{ meal.name }}</a>{% if not forloop.last %},{% endif %} | ||||||
|  |       {% endfor %} | ||||||
|  |       </li> | ||||||
|  |       {% endif %} | ||||||
|  |       {% if foods %} | ||||||
|  |       <li> {% trans "Contain" %} : | ||||||
|  |       {% for food in foods %} | ||||||
|  |         <a href="{% url "food:food_view" pk=food.pk %}">{{ food.name }}</a>{% if not forloop.last %},{% endif %} | ||||||
|  |       {% endfor %} | ||||||
|  |       </li> | ||||||
|  |       {% endif %} | ||||||
|  |     </ul> | ||||||
|  |       {% if update %} | ||||||
|  |         <a class="btn btn-sm btn-secondary" href="{% url "food:food_update" pk=food.pk %}"> | ||||||
|  |           {% trans "Update" %} | ||||||
|  |         </a> | ||||||
|  |       {% endif %} | ||||||
|  |       {% if add_ingredient %} | ||||||
|  |         <a class="btn btn-sm btn-primary" href="{% url "food:add_ingredient" pk=food.pk %}"> | ||||||
|  |           {% trans "Add to a meal" %} | ||||||
|  |         </a> | ||||||
|  |       {% endif %} | ||||||
|  |       {% if manage_ingredients %} | ||||||
|  |         <a class="btn btn-sm btn-secondary" href="{% url "food:manage_ingredients" pk=food.pk %}"> | ||||||
|  |           {% trans "Manage ingredients" %} | ||||||
|  |         </a> | ||||||
|  |       {% endif %} | ||||||
|  |         <a class="btn btn-sm btn-primary" href="{% url "food:food_list" %}"> | ||||||
|  |           {% trans "Return to the food list" %} | ||||||
|  |         </a> | ||||||
|  |   </div> | ||||||
|  | </div> | ||||||
|  | {% endblock %} | ||||||
							
								
								
									
										132
									
								
								apps/food/templates/food/food_list.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								apps/food/templates/food/food_list.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,132 @@ | |||||||
|  | {% extends "base_search.html" %} | ||||||
|  | {% comment %} | ||||||
|  | Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
|  | SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  | {% endcomment %} | ||||||
|  | {% load render_table from django_tables2 %} | ||||||
|  | {% load i18n %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  | <div class="card bg-light"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |       {{ title }} | ||||||
|  |   </h3> | ||||||
|  |   <div class="card-body"> | ||||||
|  |     <style> | ||||||
|  |       input[type=number]::-webkit-inner-spin-button, | ||||||
|  |       input[type=number]::-webkit-outer-spin-button { | ||||||
|  |           -webkit-appearance: none; | ||||||
|  |           margin: 0; | ||||||
|  |       } | ||||||
|  |       input[type=number] { | ||||||
|  |           appearance: textfield; | ||||||
|  |           padding: 6px; | ||||||
|  |           border: 1px solid #ccc; | ||||||
|  |           border-radius: 4px; | ||||||
|  |           width: 100px; | ||||||
|  |       } | ||||||
|  |     </style> | ||||||
|  |     <div class="d-flex align-items-center" style="max-width: 300px;"> | ||||||
|  |       <form method="get" action="{% url 'food:redirect_view' %}" class="d-flex w-100"> | ||||||
|  |         <input type="number" name="slug" placeholder="QR-code" required class="form-control form-control-sm" style="max-width: 120px;"> | ||||||
|  |         <button type="submit" class="btn btn-sm btn-primary">{% trans "View food" %}</button> | ||||||
|  |       </form> | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  |   <div class="card-body"> | ||||||
|  |       <input id="searchbar" type="text" class="form-control" | ||||||
|  |           placeholder="{% trans "Search by attribute such as name..." %}"> | ||||||
|  |   </div> | ||||||
|  |  | ||||||
|  |   {% block extra_inside_card %} | ||||||
|  |   {% endblock %} | ||||||
|  |  | ||||||
|  |   <div id="dynamic-table"> | ||||||
|  |       {% if table.data %} | ||||||
|  |       {% render_table table %} | ||||||
|  |       {% else %} | ||||||
|  |       <div class="card-body"> | ||||||
|  |           <div class="alert alert-warning"> | ||||||
|  |               {% trans "There is no results." %} | ||||||
|  |           </div> | ||||||
|  |       </div> | ||||||
|  |       {% endif %} | ||||||
|  |   </div> | ||||||
|  | </div> | ||||||
|  | <br> | ||||||
|  | <div class="card bg-light mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {% trans "Meal served" %} | ||||||
|  |   </h3> | ||||||
|  |   {% if can_add_meal %} | ||||||
|  |   <div class="card-footer"> | ||||||
|  |     <a class="btn btn-sm btn-primary" href="{% url 'food:transformedfood_create' %}"> | ||||||
|  |       {% trans "New meal" %} | ||||||
|  |     </a> | ||||||
|  |   </div> | ||||||
|  |   {% endif %} | ||||||
|  |   {% if served.data %} | ||||||
|  |   {% render_table served %} | ||||||
|  |   {% else %} | ||||||
|  |   <div class="card-body"> | ||||||
|  |     <div class="alert alert-warning"> | ||||||
|  |       {% trans "There is no meal served." %} | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  | </div> | ||||||
|  |   {% endif %} | ||||||
|  | <div class="card bg-light mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {% trans "Free food" %} | ||||||
|  |   </h3> | ||||||
|  |   {% if open.data %} | ||||||
|  |   {% render_table open %} | ||||||
|  |   {% else %} | ||||||
|  |   <div class="card-body"> | ||||||
|  |     <div class="alert alert-warning"> | ||||||
|  |       {% trans "There is no free food." %} | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  |   {% endif %} | ||||||
|  | </div> | ||||||
|  | {% if club_tables %} | ||||||
|  | <div class="card bg-light mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {% trans "Food of your clubs" %} | ||||||
|  |   </h3> | ||||||
|  | </div> | ||||||
|  |   {% for table in club_tables %} | ||||||
|  | <div class="card bg-light mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {% trans "Food of club" %} {{ table.prefix }}  | ||||||
|  |   </h3> | ||||||
|  |   {% if table.data %} | ||||||
|  |     {% render_table table %} | ||||||
|  |   {% else %} | ||||||
|  |   <div class="card-body"> | ||||||
|  |     <div class="alert alert-warning"> | ||||||
|  |       {% trans "Yours club has not food yet." %} | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  |   {% endif %} | ||||||
|  | </div> | ||||||
|  |   {% endfor %} | ||||||
|  |   {% endif %} | ||||||
|  | </div> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | <script> | ||||||
|  |   document.addEventListener('DOMContentLoaded', function() { | ||||||
|  |       document.getElementById('goButton').addEventListener('click', function(event) { | ||||||
|  |           event.preventDefault(); | ||||||
|  |           const slug = document.getElementById('slugInput').value; | ||||||
|  |           if (slug && !isNaN(slug)) { | ||||||
|  |               window.location.href = `/food/${slug}/`; | ||||||
|  |           } else { | ||||||
|  |               alert("Veuillez entrer un nombre valide."); | ||||||
|  |           } | ||||||
|  |       }); | ||||||
|  |   }); | ||||||
|  |   </script> | ||||||
|  |  | ||||||
|  | {% endblock %} | ||||||
| @@ -1,5 +1,6 @@ | |||||||
| {% extends "base.html" %} | {% extends "base.html" %} | ||||||
| {% comment %} | {% comment %} | ||||||
|  | Copyright (C) by BDE ENS Paris-Saclay | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later | SPDX-License-Identifier: GPL-3.0-or-later | ||||||
| {% endcomment %} | {% endcomment %} | ||||||
| {% load i18n crispy_forms_tags %} | {% load i18n crispy_forms_tags %} | ||||||
							
								
								
									
										116
									
								
								apps/food/templates/food/manage_ingredients.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								apps/food/templates/food/manage_ingredients.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,116 @@ | |||||||
|  | {% extends "base.html" %} | ||||||
|  | {% comment %} | ||||||
|  | Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
|  | SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  | {% endcomment %} | ||||||
|  | {% load i18n crispy_forms_tags %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  | <div class="card bg-white mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {{ title }} | ||||||
|  |   </h3> | ||||||
|  |   <div class="card-body" id="form"></div> | ||||||
|  |   <form method="post" action=""> | ||||||
|  |     {% csrf_token %} | ||||||
|  |     <table class="table table-condensed table-striped"> | ||||||
|  |       {# Fill initial data #} | ||||||
|  |       {% for display, form in formset %} | ||||||
|  |       {% if forloop.first %} | ||||||
|  |       <thead> | ||||||
|  | 	<tr> | ||||||
|  | 	  <th>{{ form.name.label }}</th> | ||||||
|  | 	  <th>{{ form.qrcode.label }}</th> | ||||||
|  | 	  <th>{{ form.fully_used.label }}</th> | ||||||
|  | 	</tr> | ||||||
|  |       </thead> | ||||||
|  |       <tbody id="form_body"> | ||||||
|  | 	{% endif %} | ||||||
|  | 	{% if display %} | ||||||
|  | 	<tr class="row-formset ingredients"> | ||||||
|  | 	{% else %} | ||||||
|  | 	<tr class="row-formset ingredients" style="display: none"> | ||||||
|  | 	{% endif %} | ||||||
|  | 	  <td>{{ form.name }}</td> | ||||||
|  | 	  <td>{{ form.qrcode }}</td> | ||||||
|  | 	  <td>{{ form.fully_used }}</td> | ||||||
|  | 	</tr> | ||||||
|  |       {% endfor %} | ||||||
|  |       </tbody> | ||||||
|  |     </table> | ||||||
|  |  | ||||||
|  |     {# Display buttons to add and remove ingredients #} | ||||||
|  |     <div class="card-body"> | ||||||
|  |       <div class="btn-group btn-block" role="group"> | ||||||
|  | 	<button type="button" id="add_more" class="btn btn-success">{% trans "Add ingredient" %}</button> | ||||||
|  | 	<button type="button" id="remove_one" class="btn btn-danger">{% trans "Remove ingredient" %}</button> | ||||||
|  |       </div> | ||||||
|  |     <button class="btn btn-primary" type="submit">{% trans "Submit"%}</button> | ||||||
|  |     </div> | ||||||
|  |   </form> | ||||||
|  | </div> | ||||||
|  |  | ||||||
|  | {% endblock %} | ||||||
|  | {% block extrajavascript %}  | ||||||
|  | <script> | ||||||
|  | /* script that handles add and remove lines */ | ||||||
|  |  | ||||||
|  | const foods = {{ ingredients | safe }}; | ||||||
|  |  | ||||||
|  | function set_ingredient_id () { | ||||||
|  | 	let ingredients = document.getElementsByClassName('ingredients'); | ||||||
|  | 	for (var i = 0; i < ingredients.length; i++) { | ||||||
|  | 		ingredients[i].id = 'ingredients-' + parseInt(i); | ||||||
|  | 	}; | ||||||
|  | } | ||||||
|  | set_ingredient_id(); | ||||||
|  |  | ||||||
|  | function prepopulate () { | ||||||
|  | 	for (var i = 0; i < {{ ingredients_count }}; i++) { | ||||||
|  | 		let prefix = 'id_form-' + parseInt(i) + '-'; | ||||||
|  | 		document.getElementById(prefix + 'name_pk').value = parseInt(foods[i]['food_pk']); | ||||||
|  | 		document.getElementById(prefix + 'name').value = foods[i]['food_name']; | ||||||
|  | 		document.getElementById(prefix + 'qrcode_pk').value = parseInt(foods[i]['qr_pk']); | ||||||
|  | 		if (foods[i]['qr_number'] === '') { | ||||||
|  | 			document.getElementById(prefix + 'qrcode').value = ''; | ||||||
|  | 		} | ||||||
|  | 		else { | ||||||
|  | 		document.getElementById(prefix + 'qrcode').value = parseInt(foods[i]['qr_number']); | ||||||
|  | 		}; | ||||||
|  | 		document.getElementById(prefix + 'fully_used').checked = Boolean(foods[i]['fully_used']); | ||||||
|  | 	}; | ||||||
|  | } | ||||||
|  | prepopulate(); | ||||||
|  |  | ||||||
|  | function delete_form_data (form_id) { | ||||||
|  | 	let prefix = "id_form-" + parseInt(form_id) + "-"; | ||||||
|  | 	document.getElementById(prefix + "name_pk").value = ""; | ||||||
|  | 	document.getElementById(prefix + "name").value = ""; | ||||||
|  | 	document.getElementById(prefix + "qrcode_pk").value = ""; | ||||||
|  | 	document.getElementById(prefix + "qrcode").value = ""; | ||||||
|  | 	document.getElementById(prefix + "fully_used").checked = true; | ||||||
|  | } | ||||||
|  | var form_count = {{ ingredients_count }} + 1; | ||||||
|  |  | ||||||
|  | $('#add_more').click(function () { | ||||||
|  | 	let ingredient_form = document.getElementById('ingredients-' + parseInt(form_count)); | ||||||
|  | 	if (ingredient_form === null) { | ||||||
|  | 		addMsg(gettext("You can't add more ingredient"), "danger",  5000); | ||||||
|  | 		return;}; | ||||||
|  | 	ingredient_form.style = "display: true"; | ||||||
|  | 	form_count += 1; | ||||||
|  | }); | ||||||
|  |    | ||||||
|  | $('#remove_one').click(function () { | ||||||
|  | 	let ingredient_form = document.getElementById('ingredients-' + parseInt(form_count - 1)); | ||||||
|  | 	if (ingredient_form === null) { | ||||||
|  | 		return;}; | ||||||
|  | 	ingredient_form.style = "display: none"; | ||||||
|  | 	delete_form_data(form_count - 1); | ||||||
|  | 	form_count -= 1; | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | addMsg(gettext("Add ingredient with their name or their qrcode, if two different priority is given to qrcode"), "warning");  | ||||||
|  |  | ||||||
|  | </script> | ||||||
|  | {% endblock %} | ||||||
							
								
								
									
										52
									
								
								apps/food/templates/food/qrcode.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								apps/food/templates/food/qrcode.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | |||||||
|  | {% extends "base.html" %} | ||||||
|  | {% comment %} | ||||||
|  | Copyright (C) by BDE ENS Paris-Saclay | ||||||
|  | SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  | {% endcomment %} | ||||||
|  | {% load i18n crispy_forms_tags %} | ||||||
|  | {% load render_table from django_tables2 %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  | <div class="card bg-white mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {{ title }} | ||||||
|  |   </h3> | ||||||
|  |   <div class="card-body" id="form"> | ||||||
|  |     <form method="post"> | ||||||
|  |       {% csrf_token %} | ||||||
|  |       {{ form | crispy }} | ||||||
|  |       <button class="btn btn-primary" type="submit">{% trans "Submit"%}</button> | ||||||
|  |     </form> | ||||||
|  |     <div class="card-body"> | ||||||
|  |     <h4> | ||||||
|  |       {% trans "Copy constructor" %} | ||||||
|  |       <a class="btn btn-secondary" href="{% url "food:basicfood_create" slug=slug %}">{% trans "New food" %}</a> | ||||||
|  |     </h4> | ||||||
|  |       <table class="table"> | ||||||
|  | 	<thead> | ||||||
|  | 	  <tr> | ||||||
|  | 	    <th class="orderable"> | ||||||
|  | 	      {% trans "Name" %} | ||||||
|  | 	    </th> | ||||||
|  | 	    <th class="orderable"> | ||||||
|  | 	      {% trans "Owner" %} | ||||||
|  | 	    </th> | ||||||
|  | 	    <th class="orderable"> | ||||||
|  | 	      {% trans "Expiry date" %} | ||||||
|  | 	    </th> | ||||||
|  | 	  </tr> | ||||||
|  | 	</thead> | ||||||
|  | 	<tbody> | ||||||
|  | 	  {% for food in last_items %} | ||||||
|  | 	    <tr> | ||||||
|  | 		    <td><a href="{% url "food:basicfood_create" slug=slug %}?copy={{ food.pk }}">{{ food.name }}</a></td> | ||||||
|  | 	      <td>{{ food.owner }}</td> | ||||||
|  | 	      <td>{{ food.expiry_date }}</td> | ||||||
|  | 	    </tr> | ||||||
|  | 	  {% endfor %} | ||||||
|  | 	</tbody> | ||||||
|  |       </table> | ||||||
|  |     </div> | ||||||
|  |   </div> | ||||||
|  | </div> | ||||||
|  | {% endblock %} | ||||||
| @@ -1,39 +0,0 @@ | |||||||
| {% extends "base.html" %} |  | ||||||
| {% comment %} |  | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later |  | ||||||
| {% endcomment %} |  | ||||||
| {% load i18n crispy_forms_tags %} |  | ||||||
|  |  | ||||||
| {% block content %} |  | ||||||
| <div class="card bg-white mb-3"> |  | ||||||
|     <h3 class="card-header text-center"> |  | ||||||
| 	{{ title }} {% trans 'number' %} {{ qrcode.qr_code_number }} |  | ||||||
|     </h3> |  | ||||||
| 	<div class="card-body"> |  | ||||||
| 	    <ul> |  | ||||||
| 		<li><p>{% trans 'Name' %} : {{ qrcode.food_container.name }}</p></li> |  | ||||||
| 		<li><p>{% trans 'Owner' %} : {{ qrcode.food_container.owner }}</p></li> |  | ||||||
| 		<li><p>{% trans 'Expiry date' %} : {{ qrcode.food_container.expiry_date  }}</p></li> |  | ||||||
| 	    </ul> |  | ||||||
| 	{% if qrcode.food_container.polymorphic_ctype.model == 'basicfood' and can_update_basic %} |  | ||||||
| 	    <a class="btn btn-sm btn-warning" href="{% url "food:basic_update" pk=qrcode.food_container.pk %}" data-turbolinks="false"> |  | ||||||
| 		{% trans 'Update' %} |  | ||||||
| 	    </a> |  | ||||||
| 	{% elif can_update_transformed %} |  | ||||||
| 	    <a class="btn btn-sm btn-warning" href="{% url "food:transformed_update" pk=qrcode.food_container.pk %}"> |  | ||||||
| 		{% trans 'Update' %} |  | ||||||
| 	    </a> |  | ||||||
| 	{% endif %} |  | ||||||
| 	{% if can_view_detail %} |  | ||||||
| 	    <a class="btn btn-sm btn-primary" href="{% url "food:food_view" pk=qrcode.food_container.pk %}"> |  | ||||||
| 		{% trans 'View details' %} |  | ||||||
| 	    </a> |  | ||||||
| 	{% endif %} |  | ||||||
| 	{% if can_add_ingredient %} |  | ||||||
| 	    <a class="btn btn-sm btn-success" href="{% url "food:add_ingredient" pk=qrcode.food_container.pk %}"> |  | ||||||
| 		{% trans 'Add to a meal' %} |  | ||||||
| 	    </a> |  | ||||||
| 	{% endif %} |  | ||||||
| 	</div> |  | ||||||
| </div> |  | ||||||
| {% endblock %} |  | ||||||
| @@ -1,51 +0,0 @@ | |||||||
| {% extends "base.html" %} |  | ||||||
| {% comment %} |  | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later |  | ||||||
| {% endcomment %} |  | ||||||
| {% load i18n crispy_forms_tags %} |  | ||||||
|  |  | ||||||
| {% block content %} |  | ||||||
| <div class="card bg-white mb-3"> |  | ||||||
|     <h3 class="card-header text-center"> |  | ||||||
| 	{{ title }} {{ food.name }} |  | ||||||
|     </h3> |  | ||||||
| 	<div class="card-body"> |  | ||||||
| 	    <ul> |  | ||||||
| 		<li><p>{% trans 'Owner' %} : {{ food.owner }}</p></li> |  | ||||||
| 		{% if can_see_ready %} |  | ||||||
| 		<li><p>{% trans 'Ready' %} : {{ food.is_ready }}</p></li> |  | ||||||
| 		{% endif %} |  | ||||||
| 		<li><p>{% trans 'Creation date' %} : {{ food.creation_date }}</p></li> |  | ||||||
| 		<li><p>{% trans 'Expiry date' %} : {{ food.expiry_date }}</p></li> |  | ||||||
| 		<li>{% trans 'Allergens' %} :</li> |  | ||||||
| 		<ul> |  | ||||||
| 		    {% for allergen in food.allergens.iterator %} |  | ||||||
| 		    <li>{{ allergen.name }}</li> |  | ||||||
| 		    {% endfor %} |  | ||||||
| 	        </ul> |  | ||||||
| 		<p> |  | ||||||
| 		<li>{% trans 'Ingredients' %} :</li> |  | ||||||
| 		<ul> |  | ||||||
| 		    {% for ingredient in food.ingredient.iterator %} |  | ||||||
| 		    <li><a href="{% url "food:food_view" pk=ingredient.pk %}">{{ ingredient.name }}</a></li> |  | ||||||
| 		    {% endfor %} |  | ||||||
| 		</ul> |  | ||||||
| 		<p> |  | ||||||
| 		<li><p>{% trans 'Shelf life' %} : {{ food.shelf_life }}</p></li> |  | ||||||
| 		<li><p>{% trans 'Ready' %} : {{ food.is_ready }}</p></li> |  | ||||||
| 		<li><p>{% trans 'Active' %} : {{ food.is_active }}</p></li> |  | ||||||
| 		<li><p>{% trans 'Eaten' %} : {{ food.was_eaten }}</p></li> |  | ||||||
| 	    </ul> |  | ||||||
| 	    {% if can_update %} |  | ||||||
| 	        <a class="btn btn-sm btn-warning" href="{% url "food:transformed_update" pk=food.pk %}"> |  | ||||||
| 		    {% trans 'Update' %} |  | ||||||
| 		</a> |  | ||||||
| 	    {% endif %} |  | ||||||
| 	    {% if can_add_ingredient %} |  | ||||||
| 	        <a class="btn btn-sm btn-success" href="{% url "food:add_ingredient" pk=food.pk %}"> |  | ||||||
| 		    {% trans 'Add to a meal' %} |  | ||||||
| 		</a> |  | ||||||
| 	    {% endif %} |  | ||||||
|     </div> |  | ||||||
| </div> |  | ||||||
| {% endblock %} |  | ||||||
| @@ -1,20 +0,0 @@ | |||||||
| {% extends "base.html" %} |  | ||||||
| {% comment %} |  | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later |  | ||||||
| {% endcomment %} |  | ||||||
| {% load i18n crispy_forms_tags %} |  | ||||||
|  |  | ||||||
| {% block content %} |  | ||||||
| <div class="card bg-white mb-3"> |  | ||||||
|   <h3 class="card-header text-center"> |  | ||||||
|     {{ title }} |  | ||||||
|   </h3> |  | ||||||
|   <div class="card-body" id="form"> |  | ||||||
|     <form method="post"> |  | ||||||
|       {%  csrf_token %} |  | ||||||
|       {{ form|crispy }} |  | ||||||
|       <button class="btn btn-primary" type="submit">{% trans "Submit"%}</button> |  | ||||||
|     </form> |  | ||||||
|   </div> |  | ||||||
| </div> |  | ||||||
| {% endblock %} |  | ||||||
| @@ -1,60 +0,0 @@ | |||||||
| {% extends "base.html" %} |  | ||||||
| {% comment %} |  | ||||||
| SPDX-License-Identifier: GPL-3.0-or-later |  | ||||||
| {% endcomment %} |  | ||||||
| {% load render_table from django_tables2 %} |  | ||||||
| {% load i18n %} |  | ||||||
|  |  | ||||||
| {% block content %} |  | ||||||
| <div class="card bg-light mb-3"> |  | ||||||
|     <h3 class="card-header text-center"> |  | ||||||
| 	{% trans "Meal served" %} |  | ||||||
|     </h3> |  | ||||||
|     {% if can_create_meal %} |  | ||||||
|     <div class="card-footer"> |  | ||||||
| 	<a class="btn btn-sm btn-success" href="{% url 'food:transformed_create' %}" data-turbolinks="false"> |  | ||||||
| 	    {% trans 'New meal' %} |  | ||||||
| 	</a> |  | ||||||
|     </div> |  | ||||||
|     {% endif %} |  | ||||||
|     {% if served.data %} |  | ||||||
|     {% render_table served %} |  | ||||||
|     {% else %} |  | ||||||
|     <div class="card-body"> |  | ||||||
| 	<div class="alert alert-warning"> |  | ||||||
| 	    {% trans "There is no meal served." %} |  | ||||||
| 	</div> |  | ||||||
|     </div> |  | ||||||
|     {% endif %} |  | ||||||
| </div> |  | ||||||
|  |  | ||||||
| <div class="card bg-light mb-3"> |  | ||||||
|     <h3 class="card-header text-center"> |  | ||||||
| 	{% trans "Open" %} |  | ||||||
|     </h3> |  | ||||||
|     {% if open.data %} |  | ||||||
|     {% render_table open %} |  | ||||||
|     {% else %} |  | ||||||
|     <div class="card-body"> |  | ||||||
| 	<div class="alert alert-warning"> |  | ||||||
| 	    {% trans "There is no free meal." %} |  | ||||||
| 	</div> |  | ||||||
|     </div> |  | ||||||
|     {% endif %} |  | ||||||
| </div> |  | ||||||
|  |  | ||||||
| <div class="card bg-light mb-3"> |  | ||||||
|     <h3 class="card-header text-center"> |  | ||||||
|         {% trans "All meals" %} |  | ||||||
|     </h3> |  | ||||||
|     {% if table.data %} |  | ||||||
|     {% render_table table %} |  | ||||||
|     {% else %} |  | ||||||
|     <div class="card-body"> |  | ||||||
|         <div class="alert alert-warning"> |  | ||||||
|             {% trans "There is no meal." %} |  | ||||||
|         </div> |  | ||||||
|     </div> |  | ||||||
|     {% endif %} |  | ||||||
| </div> |  | ||||||
| {% endblock %} |  | ||||||
							
								
								
									
										87
									
								
								apps/food/templates/food/transformedfood_update.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								apps/food/templates/food/transformedfood_update.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,87 @@ | |||||||
|  | {% extends "base.html" %} | ||||||
|  | {% comment %} | ||||||
|  | Copyright (C) by BDE ENS Paris-Saclay | ||||||
|  | SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  | {% endcomment %} | ||||||
|  | {% load i18n crispy_forms_tags %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  | <div class="card bg-white mb-3"> | ||||||
|  |   <h3 class="card-header text-center"> | ||||||
|  |     {{ title }} | ||||||
|  |   </h3> | ||||||
|  |   <div class="card-body" id="form"> | ||||||
|  |     <form method="post"> | ||||||
|  |       {% csrf_token %} | ||||||
|  |       {{ form | crispy }} | ||||||
|  |       <table class="table table-condensed table-striped"> | ||||||
|  |         {# Fill initial data #} | ||||||
|  |         {% for ingredient_form in formset %} | ||||||
|  |         {% if forloop.first %} | ||||||
|  |         <thead> | ||||||
|  |           <tr> | ||||||
|  | 	    <th>{% trans "Name" %}</th> | ||||||
|  | 	    <th>{% trans "QR-code number" %}</th> | ||||||
|  | 	    <th>{% trans "Fully used" %}<th> | ||||||
|  |           </tr> | ||||||
|  |         </thead> | ||||||
|  |         <tbody id="form_body"> | ||||||
|  |         {% endif %} | ||||||
|  |         <tr class="row-formset"> | ||||||
|  | 		{{ ingredient_form | crispy }} | ||||||
|  |           <td>{{ ingredient_form.name }}</td> | ||||||
|  | 	  <td>{{ ingredient_form.qrcode }}</td> | ||||||
|  | 	  <td>{{ ingredient_form.fully_used }}</td> | ||||||
|  |         </tr> | ||||||
|  |         {% endfor %} | ||||||
|  |         </tbody> | ||||||
|  |       </table> | ||||||
|  |       {# Display buttons to add and remove products #} | ||||||
|  |       <div class="card-body"> | ||||||
|  |         <div class="btn-group btn-block" role="group"> | ||||||
|  |           <button type="button" id="add_more" class="btn btn-success">{% trans "Add ingredient" %}</button> | ||||||
|  | 	  <button type="button" id="remove_one" class="btn btn-danger">{% trans "Remove ingredient" %}</button> | ||||||
|  |         </div> | ||||||
|  |         <button type="submit" class="btn btn-block btn-primary">{% trans "Submit" %}</button> | ||||||
|  |       </div> | ||||||
|  |     </form> | ||||||
|  |   </div> | ||||||
|  | </div> | ||||||
|  |  | ||||||
|  | {# Hidden div that store an empty product form, to be copied into new forms #} | ||||||
|  | <div id="empty_form" style="display: none;"> | ||||||
|  |     <table class='no_error'> | ||||||
|  |         <tbody id="for_real"> | ||||||
|  |             <tr class="row-formset"> | ||||||
|  |                 <td>{{ formset.empty_form.name }}</td> | ||||||
|  | 		<td>{{ formset.empty_form.qrcode }}</td> | ||||||
|  | 		<td>{{ formset.empty_form.fully_used }}</td> | ||||||
|  |             </tr> | ||||||
|  |         </tbody> | ||||||
|  |     </table> | ||||||
|  | </div> | ||||||
|  | {% endblock %} | ||||||
|  | {% block extrajavascript %} | ||||||
|  | <script> | ||||||
|  |     /* script that handles add and remove lines */ | ||||||
|  |     IDS = {}; | ||||||
|  |  | ||||||
|  |     $("#id_form-TOTAL_FORMS").val($(".row-formset").length - 1); | ||||||
|  |  | ||||||
|  |     $('#add_more').click(function () { | ||||||
|  |         let form_idx = $('#id_form-TOTAL_FORMS').val(); | ||||||
|  |         $('#form_body').append($('#for_real').html().replace(/__prefix__/g, form_idx)); | ||||||
|  |         $('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1); | ||||||
|  |         $('#id_form-' + parseInt(form_idx) + '-id').val(IDS[parseInt(form_idx)]); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     $('#remove_one').click(function () { | ||||||
|  |         let form_idx = $('#id_form-TOTAL_FORMS').val(); | ||||||
|  |         if (form_idx > 0) { | ||||||
|  |             IDS[parseInt(form_idx) - 1] = $('#id_form-' + (parseInt(form_idx) - 1) + '-id').val(); | ||||||
|  |             $('#form_body tr:last-child').remove(); | ||||||
|  |             $('#id_form-TOTAL_FORMS').val(parseInt(form_idx) - 1); | ||||||
|  |         } | ||||||
|  |     }); | ||||||
|  | </script> | ||||||
|  | {% endblock %} | ||||||
| @@ -1,3 +0,0 @@ | |||||||
| # from django.test import TestCase |  | ||||||
|  |  | ||||||
| # Create your tests here. |  | ||||||
							
								
								
									
										170
									
								
								apps/food/tests/test_food.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										170
									
								
								apps/food/tests/test_food.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,170 @@ | |||||||
|  | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
|  | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
|  | from api.tests import TestAPI | ||||||
|  | from django.contrib.auth.models import User | ||||||
|  | from django.test import TestCase | ||||||
|  | from django.urls import reverse | ||||||
|  | from django.utils import timezone | ||||||
|  |  | ||||||
|  | from ..api.views import AllergenViewSet, BasicFoodViewSet, TransformedFoodViewSet, QRCodeViewSet | ||||||
|  | from ..models import Allergen, BasicFood, TransformedFood, QRCode | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TestFood(TestCase): | ||||||
|  |     """ | ||||||
|  |     Test food | ||||||
|  |     """ | ||||||
|  |     fixtures = ('initial',) | ||||||
|  |  | ||||||
|  |     def setUp(self): | ||||||
|  |         self.user = User.objects.create_superuser( | ||||||
|  |             username='admintoto', | ||||||
|  |             password='toto1234', | ||||||
|  |             email='toto@example.com' | ||||||
|  |         ) | ||||||
|  |         self.client.force_login(self.user) | ||||||
|  |  | ||||||
|  |         sess = self.client.session | ||||||
|  |         sess['permission_mask'] = 42 | ||||||
|  |         sess.save() | ||||||
|  |  | ||||||
|  |         self.allergen = Allergen.objects.create( | ||||||
|  |             name='allergen', | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.basicfood = BasicFood.objects.create( | ||||||
|  |             name='basicfood', | ||||||
|  |             owner_id=1, | ||||||
|  |             expiry_date=timezone.now(), | ||||||
|  |             is_ready=False, | ||||||
|  |             date_type='DLC', | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.transformedfood = TransformedFood.objects.create( | ||||||
|  |             name='transformedfood', | ||||||
|  |             owner_id=1, | ||||||
|  |             expiry_date=timezone.now(), | ||||||
|  |             is_ready=False, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.qrcode = QRCode.objects.create( | ||||||
|  |             qr_code_number=1, | ||||||
|  |             food_container=self.basicfood, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         def test_food_list(self): | ||||||
|  |             """ | ||||||
|  |             Display food list | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:food_list')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |         def test_qrcode_create(self): | ||||||
|  |             """ | ||||||
|  |             Display QRCode creation | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:qrcode_create')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |         def test_basicfood_create(self): | ||||||
|  |             """ | ||||||
|  |             Display BasicFood creation | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:basicfood_create')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |         def test_transformedfood_create(self): | ||||||
|  |             """ | ||||||
|  |             Display TransformedFood creation | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:transformedfood_create')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |         def test_food_create(self): | ||||||
|  |             """ | ||||||
|  |             Display Food update | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:food_update')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |         def test_food_view(self): | ||||||
|  |             """ | ||||||
|  |             Display Food detail | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:food_view')) | ||||||
|  |             self.assertEqual(response.status_code, 302) | ||||||
|  |  | ||||||
|  |         def test_basicfood_view(self): | ||||||
|  |             """ | ||||||
|  |             Display BasicFood detail | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:basicfood_view')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |         def test_transformedfood_view(self): | ||||||
|  |             """ | ||||||
|  |             Display TransformedFood detail | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:transformedfood_view')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |         def test_add_ingredient(self): | ||||||
|  |             """ | ||||||
|  |             Display add ingredient view | ||||||
|  |             """ | ||||||
|  |             response = self.client.get(reverse('food:add_ingredient')) | ||||||
|  |             self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TestFoodAPI(TestAPI): | ||||||
|  |     def setUp(self) -> None: | ||||||
|  |         super().setUP() | ||||||
|  |  | ||||||
|  |         self.allergen = Allergen.objects.create( | ||||||
|  |             name='name', | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.basicfood = BasicFood.objects.create( | ||||||
|  |             name='basicfood', | ||||||
|  |             owner_id=1, | ||||||
|  |             expiry_date=timezone.now(), | ||||||
|  |             is_ready=False, | ||||||
|  |             date_type='DLC', | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.transformedfood = TransformedFood.objects.create( | ||||||
|  |             name='transformedfood', | ||||||
|  |             owner_id=1, | ||||||
|  |             expiry_date=timezone.now(), | ||||||
|  |             is_ready=False, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.qrcode = QRCode.objects.create( | ||||||
|  |             qr_code_number=1, | ||||||
|  |             food_container=self.basicfood, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         def test_allergen_api(self): | ||||||
|  |             """ | ||||||
|  |             Load Allergen API page and test all filters and permissions | ||||||
|  |             """ | ||||||
|  |             self.check_viewset(AllergenViewSet, '/api/food/allergen/') | ||||||
|  |  | ||||||
|  |         def test_basicfood_api(self): | ||||||
|  |             """ | ||||||
|  |             Load BasicFood API page and test all filters and permissions | ||||||
|  |             """ | ||||||
|  |             self.check_viewset(BasicFoodViewSet, '/api/food/basicfood/') | ||||||
|  |  | ||||||
|  |         def test_transformedfood_api(self): | ||||||
|  |             """ | ||||||
|  |             Load TransformedFood API page and test all filters and permissions | ||||||
|  |             """ | ||||||
|  |             self.check_viewset(TransformedFoodViewSet, '/api/food/transformedfood/') | ||||||
|  |  | ||||||
|  |         def test_qrcode_api(self): | ||||||
|  |             """ | ||||||
|  |             Load QRCode API page and test all filters and permissions | ||||||
|  |             """ | ||||||
|  |             self.check_viewset(QRCodeViewSet, '/api/food/qrcode/') | ||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.urls import path | from django.urls import path | ||||||
| @@ -8,14 +8,15 @@ from . import views | |||||||
| app_name = 'food' | app_name = 'food' | ||||||
|  |  | ||||||
| urlpatterns = [ | urlpatterns = [ | ||||||
|     path('', views.TransformedListView.as_view(), name='food_list'), |     path('', views.FoodListView.as_view(), name='food_list'), | ||||||
|     path('<int:slug>', views.QRCodeView.as_view(), name='qrcode_view'), |     path('<int:slug>', views.QRCodeCreateView.as_view(), name='qrcode_create'), | ||||||
|     path('detail/<int:pk>', views.FoodView.as_view(), name='food_view'), |     path('<int:slug>/add/basic', views.BasicFoodCreateView.as_view(), name='basicfood_create'), | ||||||
|  |     path('add/transformed', views.TransformedFoodCreateView.as_view(), name='transformedfood_create'), | ||||||
|     path('<int:slug>/create_qrcode', views.QRCodeCreateView.as_view(), name='qrcode_create'), |     path('update/<int:pk>', views.FoodUpdateView.as_view(), name='food_update'), | ||||||
|     path('<int:slug>/create_qrcode/basic', views.QRCodeBasicFoodCreateView.as_view(), name='qrcode_basic_create'), |     path('update/ingredients/<int:pk>', views.ManageIngredientsView.as_view(), name='manage_ingredients'), | ||||||
|     path('create/transformed', views.TransformedFoodCreateView.as_view(), name='transformed_create'), |     path('detail/<int:pk>', views.FoodDetailView.as_view(), name='food_view'), | ||||||
|     path('update/basic/<int:pk>', views.BasicFoodUpdateView.as_view(), name='basic_update'), |     path('detail/basic/<int:pk>', views.BasicFoodDetailView.as_view(), name='basicfood_view'), | ||||||
|     path('update/transformed/<int:pk>', views.TransformedFoodUpdateView.as_view(), name='transformed_update'), |     path('detail/transformed/<int:pk>', views.TransformedFoodDetailView.as_view(), name='transformedfood_view'), | ||||||
|     path('add/<int:pk>', views.AddIngredientView.as_view(), name='add_ingredient'), |     path('add/ingredient/<int:pk>', views.AddIngredientView.as_view(), name='add_ingredient'), | ||||||
|  |     path('redirect/', views.QRCodeRedirectView.as_view(), name='redirect_view'), | ||||||
| ] | ] | ||||||
|   | |||||||
							
								
								
									
										53
									
								
								apps/food/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								apps/food/utils.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | |||||||
|  | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
|  | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
|  | from django.utils.translation import gettext_lazy as _ | ||||||
|  |  | ||||||
|  | seconds = (_('second'), _('seconds')) | ||||||
|  | minutes = (_('minute'), _('minutes')) | ||||||
|  | hours = (_('hour'), _('hours')) | ||||||
|  | days = (_('day'), _('days')) | ||||||
|  | weeks = (_('week'), _('weeks')) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def plural(x): | ||||||
|  |     if x == 1: | ||||||
|  |         return 0 | ||||||
|  |     return 1 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def pretty_duration(duration): | ||||||
|  |     """ | ||||||
|  |     I receive datetime.timedelta object | ||||||
|  |     You receive string object | ||||||
|  |     """ | ||||||
|  |     text = [] | ||||||
|  |     sec = duration.seconds | ||||||
|  |     d = duration.days | ||||||
|  |  | ||||||
|  |     if d >= 7: | ||||||
|  |         w = d // 7 | ||||||
|  |         text.append(str(w) + ' ' + weeks[plural(w)]) | ||||||
|  |         d -= w * 7 | ||||||
|  |     if d > 0: | ||||||
|  |         text.append(str(d) + ' ' + days[plural(d)]) | ||||||
|  |  | ||||||
|  |     if sec >= 3600: | ||||||
|  |         h = sec // 3600 | ||||||
|  |         text.append(str(h) + ' ' + hours[plural(h)]) | ||||||
|  |         sec -= h * 3600 | ||||||
|  |  | ||||||
|  |     if sec >= 60: | ||||||
|  |         m = sec // 60 | ||||||
|  |         text.append(str(m) + ' ' + minutes[plural(m)]) | ||||||
|  |         sec -= m * 60 | ||||||
|  |  | ||||||
|  |     if sec > 0: | ||||||
|  |         text.append(str(sec) + ' ' + seconds[plural(sec)]) | ||||||
|  |  | ||||||
|  |     if len(text) == 0: | ||||||
|  |         return '' | ||||||
|  |     if len(text) == 1: | ||||||
|  |         return text[0] | ||||||
|  |     if len(text) >= 2: | ||||||
|  |         return ', '.join(t for t in text[:-1]) + ' ' + _('and') + ' ' + text[-1] | ||||||
| @@ -1,421 +1,523 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.db import transaction | from datetime import timedelta | ||||||
| from django.contrib.auth.mixins import LoginRequiredMixin |  | ||||||
| from django.http import HttpResponseRedirect | from api.viewsets import is_regex | ||||||
| from django_tables2.views import MultiTableMixin | from django_tables2.views import MultiTableMixin | ||||||
| from django.urls import reverse | from django.db import transaction | ||||||
| from django.utils.translation import gettext_lazy as _ | from django.db.models import Q | ||||||
| from django.utils import timezone | from django.http import HttpResponseRedirect, Http404 | ||||||
| from django.views.generic import DetailView, UpdateView | from django.views.generic import DetailView, UpdateView, CreateView | ||||||
| from django.views.generic.list import ListView | from django.views.generic.list import ListView | ||||||
| from django.forms import HiddenInput | from django.views.generic.base import RedirectView | ||||||
|  | from django.urls import reverse_lazy | ||||||
|  | from django.utils import timezone | ||||||
|  | from django.utils.translation import gettext_lazy as _ | ||||||
|  | from member.models import Club, Membership | ||||||
| from permission.backends import PermissionBackend | from permission.backends import PermissionBackend | ||||||
| from permission.views import ProtectQuerysetMixin, ProtectedCreateView | from permission.views import ProtectQuerysetMixin, ProtectedCreateView, LoginRequiredMixin | ||||||
|  |  | ||||||
| from .forms import AddIngredientForms, BasicFoodForms, QRCodeForms, TransformedFoodForms | from .models import Food, BasicFood, TransformedFood, QRCode | ||||||
| from .models import BasicFood, Food, QRCode, TransformedFood | from .forms import QRCodeForms, BasicFoodForms, TransformedFoodForms, \ | ||||||
| from .tables import TransformedFoodTable |     ManageIngredientsForm, ManageIngredientsFormSet, AddIngredientForms, \ | ||||||
|  |     BasicFoodUpdateForms, TransformedFoodUpdateForms | ||||||
|  | from .tables import FoodTable | ||||||
|  | from .utils import pretty_duration | ||||||
|  |  | ||||||
|  |  | ||||||
| class AddIngredientView(ProtectQuerysetMixin, UpdateView): | class FoodListView(ProtectQuerysetMixin, LoginRequiredMixin, MultiTableMixin, ListView): | ||||||
|     """ |     """ | ||||||
|     A view to add an ingredient |     Display Food | ||||||
|     """ |     """ | ||||||
|     model = Food |     model = Food | ||||||
|     template_name = 'food/add_ingredient_form.html' |     tables = [FoodTable, FoodTable, FoodTable, ] | ||||||
|     extra_context = {"title": _("Add the ingredient")} |     extra_context = {"title": _('Food')} | ||||||
|     form_class = AddIngredientForms |     template_name = 'food/food_list.html' | ||||||
|  |  | ||||||
|     def get_context_data(self, **kwargs): |     def get_queryset(self, **kwargs): | ||||||
|         context = super().get_context_data(**kwargs) |         return super().get_queryset(**kwargs).distinct() | ||||||
|         context["pk"] = self.kwargs["pk"] |  | ||||||
|         return context |  | ||||||
|  |  | ||||||
|     @transaction.atomic |     def get_tables(self): | ||||||
|     def form_valid(self, form): |         bureau_role_pk = 4 | ||||||
|         form.instance.creater = self.request.user |         clubs = Club.objects.filter(membership__in=Membership.objects.filter( | ||||||
|         food = Food.objects.get(pk=self.kwargs['pk']) |             user=self.request.user, roles=bureau_role_pk).filter( | ||||||
|         add_ingredient_form = AddIngredientForms(data=self.request.POST) |                 date_end__gte=timezone.now())) | ||||||
|         if food.is_ready: |  | ||||||
|             form.add_error(None, _("The product is already prepared")) |  | ||||||
|             return self.form_invalid(form) |  | ||||||
|         if not add_ingredient_form.is_valid(): |  | ||||||
|             return self.form_invalid(form) |  | ||||||
|  |  | ||||||
|         # We flip logic ""fully used = not is_active"" |         tables = [FoodTable] * (clubs.count() + 3) | ||||||
|         food.is_active = not food.is_active |         self.tables = tables | ||||||
|         # Save the aliment and the allergens associed |         tables = super().get_tables() | ||||||
|         for transformed_pk in self.request.POST.getlist('ingredient'): |         tables[0].prefix = 'search-' | ||||||
|             transformed = TransformedFood.objects.get(pk=transformed_pk) |         tables[1].prefix = 'open-' | ||||||
|             if not transformed.is_ready: |         tables[2].prefix = 'served-' | ||||||
|                 transformed.ingredient.add(food) |         for i in range(clubs.count()): | ||||||
|                 transformed.update() |             tables[i + 3].prefix = clubs[i].name | ||||||
|         food.save() |         return tables | ||||||
|  |  | ||||||
|         return HttpResponseRedirect(self.get_success_url()) |     def get_tables_data(self): | ||||||
|  |         # table search | ||||||
|  |         qs = self.get_queryset().order_by('name') | ||||||
|  |         if "search" in self.request.GET and self.request.GET['search']: | ||||||
|  |             pattern = self.request.GET['search'] | ||||||
|  |  | ||||||
|     def get_success_url(self, **kwargs): |             # check regex | ||||||
|         return reverse('food:food_list') |             valid_regex = is_regex(pattern) | ||||||
|  |             suffix = '__iregex' if valid_regex else '__istartswith' | ||||||
|  |             prefix = '^' if valid_regex else '' | ||||||
| class BasicFoodUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): |             qs = qs.filter(Q(**{f'name{suffix}': prefix + pattern}) | ||||||
|     """ |                            | Q(**{f'owner__name{suffix}': prefix + pattern})) | ||||||
|     A view to update a basic food |         else: | ||||||
|     """ |             qs = qs.none() | ||||||
|     model = BasicFood |         search_table = qs.filter(PermissionBackend.filter_queryset(self.request, Food, 'view')) | ||||||
|     form_class = BasicFoodForms |         # table open | ||||||
|     template_name = 'food/basicfood_form.html' |         open_table = self.get_queryset().order_by('expiry_date').filter( | ||||||
|     extra_context = {"title": _("Update an aliment")} |             Q(polymorphic_ctype__model='transformedfood') | ||||||
|  |             | Q(polymorphic_ctype__model='basicfood', basicfood__date_type='DLC')).filter( | ||||||
|     @transaction.atomic |                 expiry_date__lt=timezone.now(), end_of_life='').filter( | ||||||
|     def form_valid(self, form): |                     PermissionBackend.filter_queryset(self.request, Food, 'view')) | ||||||
|         form.instance.creater = self.request.user |         # table served | ||||||
|         basic_food_form = BasicFoodForms(data=self.request.POST) |         served_table = self.get_queryset().order_by('-pk').filter( | ||||||
|         if not basic_food_form.is_valid(): |             end_of_life='', is_ready=True).exclude( | ||||||
|             return self.form_invalid(form) |                 Q(polymorphic_ctype__model='basicfood', | ||||||
|  |                   basicfood__date_type='DLC', | ||||||
|         ans = super().form_valid(form) |                   expiry_date__lte=timezone.now(),) | ||||||
|         form.instance.update() |                 | Q(polymorphic_ctype__model='transformedfood', | ||||||
|         return ans |                     expiry_date__lte=timezone.now(), | ||||||
|  |                     )) | ||||||
|     def get_success_url(self, **kwargs): |         # tables club | ||||||
|         self.object.refresh_from_db() |         bureau_role_pk = 4 | ||||||
|         return reverse('food:food_view', kwargs={"pk": self.object.pk}) |         clubs = Club.objects.filter(membership__in=Membership.objects.filter( | ||||||
|  |             user=self.request.user, roles=bureau_role_pk).filter( | ||||||
|     def get_context_data(self, **kwargs): |                 date_end__gte=timezone.now())) | ||||||
|         context = super().get_context_data(**kwargs) |         club_table = [] | ||||||
|         return context |         for club in clubs: | ||||||
|  |             club_table.append(self.get_queryset().order_by('expiry_date').filter( | ||||||
|  |                 owner=club, end_of_life='').filter( | ||||||
| class FoodView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): |                     PermissionBackend.filter_queryset(self.request, Food, 'view') | ||||||
|     """ |             )) | ||||||
|     A view to see a food |         return [search_table, open_table, served_table] + club_table | ||||||
|     """ |  | ||||||
|     model = Food |  | ||||||
|     extra_context = {"title": _("Details of:")} |  | ||||||
|     context_object_name = "food" |  | ||||||
|  |  | ||||||
|     def get_context_data(self, **kwargs): |     def get_context_data(self, **kwargs): | ||||||
|         context = super().get_context_data(**kwargs) |         context = super().get_context_data(**kwargs) | ||||||
|  |  | ||||||
|         context["can_update"] = PermissionBackend.check_perm(self.request, "food.change_food") |         tables = context['tables'] | ||||||
|         context["can_add_ingredient"] = PermissionBackend.check_perm(self.request, "food.change_transformedfood") |         # for extends base_search.html we need to name 'search_table' in 'table' | ||||||
|  |         for name, table in zip(['table', 'open', 'served'], tables): | ||||||
|  |             context[name] = table | ||||||
|  |         context['club_tables'] = tables[3:] | ||||||
|  |  | ||||||
|  |         context['can_add_meal'] = PermissionBackend.check_perm(self.request, 'food.transformedfood_add') | ||||||
|         return context |         return context | ||||||
|  |  | ||||||
|  |  | ||||||
| class QRCodeBasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): | class QRCodeCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): | ||||||
|     ##################################################################### |  | ||||||
|     # TO DO |  | ||||||
|     # - this feature is very pratical for meat or fish, nevertheless we can implement this later |  | ||||||
|     # - fix picture save |  | ||||||
|     # - implement solution crop and convert image (reuse or recode ImageForm from members apps) |  | ||||||
|     ##################################################################### |  | ||||||
|     """ |     """ | ||||||
|     A view to add a basic food with a qrcode |     A view to add qrcode | ||||||
|     """ |  | ||||||
|     model = BasicFood |  | ||||||
|     form_class = BasicFoodForms |  | ||||||
|     template_name = 'food/basicfood_form.html' |  | ||||||
|     extra_context = {"title": _("Add a new basic food with QRCode")} |  | ||||||
|  |  | ||||||
|     @transaction.atomic |  | ||||||
|     def form_valid(self, form): |  | ||||||
|         form.instance.creater = self.request.user |  | ||||||
|         basic_food_form = BasicFoodForms(data=self.request.POST) |  | ||||||
|         if not basic_food_form.is_valid(): |  | ||||||
|             return self.form_invalid(form) |  | ||||||
|  |  | ||||||
|         # Save the aliment and the allergens associed |  | ||||||
|         basic_food = form.save(commit=False) |  | ||||||
|         # We assume the date of labeling and the same as the date of arrival |  | ||||||
|         basic_food.arrival_date = timezone.now() |  | ||||||
|         basic_food.is_ready = False |  | ||||||
|         basic_food.is_active = True |  | ||||||
|         basic_food.was_eaten = False |  | ||||||
|         basic_food._force_save = True |  | ||||||
|         basic_food.save() |  | ||||||
|         basic_food.refresh_from_db() |  | ||||||
|  |  | ||||||
|         qrcode = QRCode() |  | ||||||
|         qrcode.qr_code_number = self.kwargs['slug'] |  | ||||||
|         qrcode.food_container = basic_food |  | ||||||
|         qrcode.save() |  | ||||||
|  |  | ||||||
|         return super().form_valid(form) |  | ||||||
|  |  | ||||||
|     def get_success_url(self, **kwargs): |  | ||||||
|         self.object.refresh_from_db() |  | ||||||
|         return reverse('food:qrcode_view', kwargs={"slug": self.kwargs['slug']}) |  | ||||||
|  |  | ||||||
|     def get_sample_object(self): |  | ||||||
|  |  | ||||||
|         # We choose a club which may work or BDE else |  | ||||||
|         owner_id = 1 |  | ||||||
|         for membership in self.request.user.memberships.all(): |  | ||||||
|             club_id = membership.club.id |  | ||||||
|             food = BasicFood(name="", expiry_date=timezone.now(), owner_id=club_id) |  | ||||||
|             if PermissionBackend.check_perm(self.request, "food.add_basicfood", food): |  | ||||||
|                 owner_id = club_id |  | ||||||
|  |  | ||||||
|         return BasicFood( |  | ||||||
|             name="", |  | ||||||
|             expiry_date=timezone.now(), |  | ||||||
|             owner_id=owner_id, |  | ||||||
|         ) |  | ||||||
|  |  | ||||||
|     def get_context_data(self, **kwargs): |  | ||||||
|         # Some field are hidden on create |  | ||||||
|         context = super().get_context_data(**kwargs) |  | ||||||
|  |  | ||||||
|         form = context['form'] |  | ||||||
|         form.fields['is_active'].widget = HiddenInput() |  | ||||||
|         form.fields['was_eaten'].widget = HiddenInput() |  | ||||||
|  |  | ||||||
|         copy = self.request.GET.get('copy', None) |  | ||||||
|         if copy is not None: |  | ||||||
|             basic = BasicFood.objects.get(pk=copy) |  | ||||||
|             for field in ['date_type', 'expiry_date', 'name', 'owner']: |  | ||||||
|                 form.fields[field].initial = getattr(basic, field) |  | ||||||
|             for field in ['allergens']: |  | ||||||
|                 form.fields[field].initial = getattr(basic, field).all() |  | ||||||
|  |  | ||||||
|         return context |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class QRCodeCreateView(ProtectQuerysetMixin, ProtectedCreateView): |  | ||||||
|     """ |  | ||||||
|     A view to add a new qrcode |  | ||||||
|     """ |     """ | ||||||
|     model = QRCode |     model = QRCode | ||||||
|     template_name = 'food/create_qrcode_form.html' |     template_name = 'food/qrcode.html' | ||||||
|     form_class = QRCodeForms |     form_class = QRCodeForms | ||||||
|     extra_context = {"title": _("Add a new QRCode")} |     extra_context = {"title": _("Add a new QRCode")} | ||||||
|  |  | ||||||
|     def get(self, *args, **kwargs): |     def get(self, *args, **kwargs): | ||||||
|         qrcode = kwargs["slug"] |         qrcode = kwargs["slug"] | ||||||
|         if self.model.objects.filter(qr_code_number=qrcode).count() > 0: |         if self.model.objects.filter(qr_code_number=qrcode).count() > 0: | ||||||
|             return HttpResponseRedirect(reverse("food:qrcode_view", kwargs=kwargs)) |             pk = self.model.objects.get(qr_code_number=qrcode).food_container.pk | ||||||
|  |             return HttpResponseRedirect(reverse_lazy("food:food_view", kwargs={"pk": pk})) | ||||||
|         else: |         else: | ||||||
|             return super().get(*args, **kwargs) |             return super().get(*args, **kwargs) | ||||||
|  |  | ||||||
|     def get_context_data(self, **kwargs): |  | ||||||
|         context = super().get_context_data(**kwargs) |  | ||||||
|         context["slug"] = self.kwargs["slug"] |  | ||||||
|  |  | ||||||
|         context["last_basic"] = BasicFood.objects.order_by('-pk').all()[:10] |  | ||||||
|  |  | ||||||
|         return context |  | ||||||
|  |  | ||||||
|     @transaction.atomic |     @transaction.atomic | ||||||
|     def form_valid(self, form): |     def form_valid(self, form): | ||||||
|         form.instance.creater = self.request.user |  | ||||||
|         qrcode_food_form = QRCodeForms(data=self.request.POST) |         qrcode_food_form = QRCodeForms(data=self.request.POST) | ||||||
|         if not qrcode_food_form.is_valid(): |         if not qrcode_food_form.is_valid(): | ||||||
|             return self.form_invalid(form) |             return self.form_invalid(form) | ||||||
|  |  | ||||||
|         # Save the qrcode |  | ||||||
|         qrcode = form.save(commit=False) |         qrcode = form.save(commit=False) | ||||||
|         qrcode.qr_code_number = self.kwargs["slug"] |         qrcode.qr_code_number = self.kwargs['slug'] | ||||||
|         qrcode._force_save = True |         qrcode._force_save = True | ||||||
|         qrcode.save() |         qrcode.save() | ||||||
|         qrcode.refresh_from_db() |         qrcode.refresh_from_db() | ||||||
|  |         return super().form_valid(form) | ||||||
|  |  | ||||||
|         qrcode.food_container.save() |     def get_context_data(self, **kwargs): | ||||||
|  |         context = super().get_context_data(**kwargs) | ||||||
|  |         context['slug'] = self.kwargs['slug'] | ||||||
|  |  | ||||||
|  |         # get last 10 BasicFood objects with distincts 'name' ordered by '-pk' | ||||||
|  |         # we can't use .distinct and .order_by with differents columns hence the generator | ||||||
|  |         context['last_items'] = [food for food in BasicFood.get_lastests_objects(10, 'name', '-pk')] | ||||||
|  |         return context | ||||||
|  |  | ||||||
|  |     def get_success_url(self, **kwargs): | ||||||
|  |         self.object.refresh_from_db() | ||||||
|  |         return reverse_lazy('food:food_view', kwargs={'pk': self.object.food_container.pk}) | ||||||
|  |  | ||||||
|  |     def get_sample_object(self): | ||||||
|  |         return QRCode( | ||||||
|  |             qr_code_number=self.kwargs['slug'], | ||||||
|  |             food_container_id=1, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): | ||||||
|  |     """ | ||||||
|  |     A view to add basicfood | ||||||
|  |     """ | ||||||
|  |     model = BasicFood | ||||||
|  |     form_class = BasicFoodForms | ||||||
|  |     extra_context = {"title": _("Add an aliment")} | ||||||
|  |     template_name = "food/food_update.html" | ||||||
|  |  | ||||||
|  |     def get_sample_object(self): | ||||||
|  |         # We choose a club which may work or BDE else | ||||||
|  |         food = BasicFood( | ||||||
|  |             name="", | ||||||
|  |             owner_id=1, | ||||||
|  |             expiry_date=timezone.now(), | ||||||
|  |             is_ready=True, | ||||||
|  |             arrival_date=timezone.now(), | ||||||
|  |             date_type='DLC', | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         for membership in self.request.user.memberships.all(): | ||||||
|  |             club_id = membership.club.id | ||||||
|  |             food.owner_id = club_id | ||||||
|  |             if PermissionBackend.check_perm(self.request, "food.add_basicfood", food): | ||||||
|  |                 return food | ||||||
|  |  | ||||||
|  |         return food | ||||||
|  |  | ||||||
|  |     @transaction.atomic | ||||||
|  |     def form_valid(self, form): | ||||||
|  |         if QRCode.objects.filter(qr_code_number=self.kwargs['slug']).count() > 0: | ||||||
|  |             return HttpResponseRedirect(reverse_lazy('food:qrcode_create', kwargs={'slug': self.kwargs['slug']})) | ||||||
|  |         food_form = BasicFoodForms(data=self.request.POST) | ||||||
|  |         if not food_form.is_valid(): | ||||||
|  |             return self.form_invalid(form) | ||||||
|  |  | ||||||
|  |         food = form.save(commit=False) | ||||||
|  |         food.is_ready = False | ||||||
|  |         food.save() | ||||||
|  |         food.refresh_from_db() | ||||||
|  |  | ||||||
|  |         qrcode = QRCode() | ||||||
|  |         qrcode.qr_code_number = self.kwargs['slug'] | ||||||
|  |         qrcode.food_container = food | ||||||
|  |         qrcode.save() | ||||||
|  |  | ||||||
|         return super().form_valid(form) |         return super().form_valid(form) | ||||||
|  |  | ||||||
|     def get_success_url(self, **kwargs): |     def get_success_url(self, **kwargs): | ||||||
|         self.object.refresh_from_db() |         self.object.refresh_from_db() | ||||||
|         return reverse('food:qrcode_view', kwargs={"slug": self.kwargs['slug']}) |         return reverse_lazy('food:basicfood_view', kwargs={"pk": self.object.pk}) | ||||||
|  |  | ||||||
|     def get_sample_object(self): |     def get_context_data(self, *args, **kwargs): | ||||||
|         return QRCode( |         context = super().get_context_data(*args, **kwargs) | ||||||
|             qr_code_number=self.kwargs["slug"], |  | ||||||
|             food_container_id=1 |  | ||||||
|         ) |  | ||||||
|  |  | ||||||
|  |         copy = self.request.GET.get('copy', None) | ||||||
| class QRCodeView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): |         if copy is not None: | ||||||
|     """ |             food = BasicFood.objects.get(pk=copy) | ||||||
|     A view to see a qrcode |             print(context['form'].fields) | ||||||
|     """ |             for field in context['form'].fields: | ||||||
|     model = QRCode |                 if field == 'allergens': | ||||||
|     extra_context = {"title": _("QRCode")} |                     context['form'].fields[field].initial = getattr(food, field).all() | ||||||
|     context_object_name = "qrcode" |  | ||||||
|     slug_field = "qr_code_number" |  | ||||||
|  |  | ||||||
|     def get(self, *args, **kwargs): |  | ||||||
|         qrcode = kwargs["slug"] |  | ||||||
|         if self.model.objects.filter(qr_code_number=qrcode).count() > 0: |  | ||||||
|             return super().get(*args, **kwargs) |  | ||||||
|                 else: |                 else: | ||||||
|             return HttpResponseRedirect(reverse("food:qrcode_create", kwargs=kwargs)) |                     context['form'].fields[field].initial = getattr(food, field) | ||||||
|  |  | ||||||
|     def get_context_data(self, **kwargs): |  | ||||||
|         context = super().get_context_data(**kwargs) |  | ||||||
|  |  | ||||||
|         qr_code_number = self.kwargs['slug'] |  | ||||||
|         qrcode = self.model.objects.get(qr_code_number=qr_code_number) |  | ||||||
|  |  | ||||||
|         model = qrcode.food_container.polymorphic_ctype.model |  | ||||||
|  |  | ||||||
|         if model == "basicfood": |  | ||||||
|             context["can_update_basic"] = PermissionBackend.check_perm(self.request, "food.change_basicfood") |  | ||||||
|             context["can_view_detail"] = PermissionBackend.check_perm(self.request, "food.view_basicfood") |  | ||||||
|         if model == "transformedfood": |  | ||||||
|             context["can_update_transformed"] = PermissionBackend.check_perm(self.request, "food.change_transformedfood") |  | ||||||
|             context["can_view_detail"] = PermissionBackend.check_perm(self.request, "food.view_transformedfood") |  | ||||||
|         context["can_add_ingredient"] = PermissionBackend.check_perm(self.request, "food.change_transformedfood") |  | ||||||
|         return context |         return context | ||||||
|  |  | ||||||
|  |  | ||||||
| class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): | class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): | ||||||
|     """ |     """ | ||||||
|     A view to add a tranformed food |     A view to add transformedfood | ||||||
|     """ |     """ | ||||||
|     model = TransformedFood |     model = TransformedFood | ||||||
|     template_name = 'food/transformedfood_form.html' |  | ||||||
|     form_class = TransformedFoodForms |     form_class = TransformedFoodForms | ||||||
|     extra_context = {"title": _("Add a new meal")} |     extra_context = {"title": _("Add a meal")} | ||||||
|  |     template_name = "food/food_update.html" | ||||||
|     @transaction.atomic |  | ||||||
|     def form_valid(self, form): |  | ||||||
|         form.instance.creater = self.request.user |  | ||||||
|         transformed_food_form = TransformedFoodForms(data=self.request.POST) |  | ||||||
|         if not transformed_food_form.is_valid(): |  | ||||||
|             return self.form_invalid(form) |  | ||||||
|  |  | ||||||
|         # Save the aliment and allergens associated |  | ||||||
|         transformed_food = form.save(commit=False) |  | ||||||
|         transformed_food.expiry_date = transformed_food.creation_date |  | ||||||
|         transformed_food.is_active = True |  | ||||||
|         transformed_food.is_ready = False |  | ||||||
|         transformed_food.was_eaten = False |  | ||||||
|         transformed_food._force_save = True |  | ||||||
|         transformed_food.save() |  | ||||||
|         transformed_food.refresh_from_db() |  | ||||||
|         ans = super().form_valid(form) |  | ||||||
|         transformed_food.update() |  | ||||||
|         return ans |  | ||||||
|  |  | ||||||
|     def get_success_url(self, **kwargs): |  | ||||||
|         self.object.refresh_from_db() |  | ||||||
|         return reverse('food:food_view', kwargs={"pk": self.object.pk}) |  | ||||||
|  |  | ||||||
|     def get_sample_object(self): |     def get_sample_object(self): | ||||||
|         # We choose a club which may work or BDE else |         # We choose a club which may work or BDE else | ||||||
|         owner_id = 1 |         food = TransformedFood( | ||||||
|         for membership in self.request.user.memberships.all(): |  | ||||||
|             club_id = membership.club.id |  | ||||||
|             food = TransformedFood(name="", |  | ||||||
|                                    creation_date=timezone.now(), |  | ||||||
|                                    expiry_date=timezone.now(), |  | ||||||
|                                    owner_id=club_id) |  | ||||||
|             if PermissionBackend.check_perm(self.request, "food.add_transformedfood", food): |  | ||||||
|                 owner_id = club_id |  | ||||||
|                 break |  | ||||||
|  |  | ||||||
|         return TransformedFood( |  | ||||||
|             name="", |             name="", | ||||||
|             owner_id=owner_id, |             owner_id=1, | ||||||
|             creation_date=timezone.now(), |  | ||||||
|             expiry_date=timezone.now(), |             expiry_date=timezone.now(), | ||||||
|  |             is_ready=True, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|     def get_context_data(self, **kwargs): |         for membership in self.request.user.memberships.all(): | ||||||
|         context = super().get_context_data(**kwargs) |             club_id = membership.club.id | ||||||
|  |             food.owner_id = club_id | ||||||
|  |             if PermissionBackend.check_perm(self.request, "food.add_transformedfood", food): | ||||||
|  |                 return food | ||||||
|  |  | ||||||
|         # Some field are hidden on create |         return food | ||||||
|         form = context['form'] |  | ||||||
|         form.fields['is_active'].widget = HiddenInput() |  | ||||||
|         form.fields['is_ready'].widget = HiddenInput() |  | ||||||
|         form.fields['was_eaten'].widget = HiddenInput() |  | ||||||
|         form.fields['shelf_life'].widget = HiddenInput() |  | ||||||
|  |  | ||||||
|         return context |     @transaction.atomic | ||||||
|  |     def form_valid(self, form): | ||||||
|  |         form.instance.expiry_date = timezone.now() + timedelta(days=3) | ||||||
|  |         form.instance.is_ready = False | ||||||
|  |         return super().form_valid(form) | ||||||
|  |  | ||||||
|  |     def get_success_url(self, **kwargs): | ||||||
|  |         self.object.refresh_from_db() | ||||||
|  |         return reverse_lazy('food:transformedfood_view', kwargs={"pk": self.object.pk}) | ||||||
|  |  | ||||||
|  |  | ||||||
| class TransformedFoodUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): | MAX_FORMS = 100 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class ManageIngredientsView(LoginRequiredMixin, UpdateView): | ||||||
|     """ |     """ | ||||||
|     A view to update transformed product |     A view to manage ingredient for a transformed food | ||||||
|     """ |     """ | ||||||
|     model = TransformedFood |     model = TransformedFood | ||||||
|     template_name = 'food/transformedfood_form.html' |     fields = ['ingredients'] | ||||||
|     form_class = TransformedFoodForms |     extra_context = {"title": _("Manage ingredients of:")} | ||||||
|     extra_context = {'title': _('Update a meal')} |     template_name = 'food/manage_ingredients.html' | ||||||
|  |  | ||||||
|  |     @transaction.atomic | ||||||
|  |     def form_valid(self, form): | ||||||
|  |         old_ingredients = list(self.object.ingredients.all()).copy() | ||||||
|  |         old_allergens = list(self.object.allergens.all()).copy() | ||||||
|  |         self.object.ingredients.clear() | ||||||
|  |         for i in range(self.object.ingredients.all().count() + 1 + MAX_FORMS): | ||||||
|  |             prefix = 'form-' + str(i) + '-' | ||||||
|  |             if form.data[prefix + 'qrcode'] not in ['0', '']: | ||||||
|  |                 ingredient = QRCode.objects.get(pk=form.data[prefix + 'qrcode']).food_container | ||||||
|  |                 self.object.ingredients.add(ingredient) | ||||||
|  |                 if (prefix + 'fully_used') in form.data and form.data[prefix + 'fully_used'] == 'on': | ||||||
|  |                     ingredient.end_of_life = _('Fully used in {meal}'.format( | ||||||
|  |                         meal=self.object.name)) | ||||||
|  |                     ingredient.save() | ||||||
|  |  | ||||||
|  |             elif form.data[prefix + 'name'] != '': | ||||||
|  |                 ingredient = Food.objects.get(pk=form.data[prefix + 'name']) | ||||||
|  |                 self.object.ingredients.add(ingredient) | ||||||
|  |                 if (prefix + 'fully_used') in form.data and form.data[prefix + 'fully_used'] == 'on': | ||||||
|  |                     ingredient.end_of_life = _('Fully used in {meal}'.format( | ||||||
|  |                         meal=self.object.name)) | ||||||
|  |                     ingredient.save() | ||||||
|  |         # We recalculate new expiry date and allergens | ||||||
|  |         self.object.expiry_date = self.object.creation_date + self.object.shelf_life | ||||||
|  |         self.object.allergens.clear() | ||||||
|  |  | ||||||
|  |         for ingredient in self.object.ingredients.iterator(): | ||||||
|  |             if not (ingredient.polymorphic_ctype.model == 'basicfood' and ingredient.date_type == 'DDM'): | ||||||
|  |                 self.object.expiry_date = min(self.object.expiry_date, ingredient.expiry_date) | ||||||
|  |             self.object.allergens.set(self.object.allergens.union(ingredient.allergens.all())) | ||||||
|  |  | ||||||
|  |         self.object.save(old_ingredients=old_ingredients, old_allergens=old_allergens) | ||||||
|  |         return HttpResponseRedirect(self.get_success_url()) | ||||||
|  |  | ||||||
|  |     def get_context_data(self, *args, **kwargs): | ||||||
|  |         context = super().get_context_data(*args, **kwargs) | ||||||
|  |         context['title'] += ' ' + self.object.name | ||||||
|  |         formset = ManageIngredientsFormSet() | ||||||
|  |         ingredients = self.object.ingredients.all() | ||||||
|  |         formset.extra += ingredients.count() + MAX_FORMS | ||||||
|  |         context['form'] = ManageIngredientsForm() | ||||||
|  |         context['ingredients_count'] = ingredients.count() | ||||||
|  |         display = [True] * (1 + ingredients.count()) + [False] * (formset.extra - ingredients.count() - 1) | ||||||
|  |         context['formset'] = zip(display, formset) | ||||||
|  |         context['ingredients'] = [] | ||||||
|  |         for ingredient in ingredients: | ||||||
|  |             qr = QRCode.objects.filter(food_container=ingredient) | ||||||
|  |  | ||||||
|  |             context['ingredients'].append({ | ||||||
|  |                 'food_pk': ingredient.pk, | ||||||
|  |                 'food_name': ingredient.name, | ||||||
|  |                 'qr_pk': '' if qr.count() == 0 else qr[0].pk, | ||||||
|  |                 'qr_number': '' if qr.count() == 0 else qr[0].qr_code_number, | ||||||
|  |                 'fully_used': 'true' if ingredient.end_of_life else '', | ||||||
|  |             }) | ||||||
|  |         return context | ||||||
|  |  | ||||||
|  |     def get_success_url(self, **kwargs): | ||||||
|  |         return reverse_lazy('food:transformedfood_view', kwargs={"pk": self.object.pk}) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class AddIngredientView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): | ||||||
|  |     """ | ||||||
|  |     A view to add ingredient to a meal | ||||||
|  |     """ | ||||||
|  |     model = Food | ||||||
|  |     extra_context = {"title": _("Add the ingredient:")} | ||||||
|  |     form_class = AddIngredientForms | ||||||
|  |     template_name = 'food/food_update.html' | ||||||
|  |  | ||||||
|  |     def get_context_data(self, *args, **kwargs): | ||||||
|  |         context = super().get_context_data(*args, **kwargs) | ||||||
|  |         context['title'] += ' ' + self.object.name | ||||||
|  |         return context | ||||||
|  |  | ||||||
|  |     @transaction.atomic | ||||||
|  |     def form_valid(self, form): | ||||||
|  |         meals = TransformedFood.objects.filter(pk__in=form.data.getlist('ingredients')).all() | ||||||
|  |         if not meals: | ||||||
|  |             return HttpResponseRedirect(reverse_lazy('food:food_view', kwargs={"pk": self.object.pk})) | ||||||
|  |         for meal in meals: | ||||||
|  |             old_ingredients = list(meal.ingredients.all()).copy() | ||||||
|  |             old_allergens = list(meal.allergens.all()).copy() | ||||||
|  |             meal.ingredients.add(self.object.pk) | ||||||
|  |             # update allergen and expiry date if necessary | ||||||
|  |             if not (self.object.polymorphic_ctype.model == 'basicfood' | ||||||
|  |                     and self.object.date_type == 'DDM'): | ||||||
|  |                 meal.expiry_date = min(meal.expiry_date, self.object.expiry_date) | ||||||
|  |             meal.allergens.set(meal.allergens.union(self.object.allergens.all())) | ||||||
|  |             meal.save(old_ingredients=old_ingredients, old_allergens=old_allergens) | ||||||
|  |             if 'fully_used' in form.data: | ||||||
|  |                 if not self.object.end_of_life: | ||||||
|  |                     self.object.end_of_life = _(f'Food fully used in : {meal.name}') | ||||||
|  |                 else: | ||||||
|  |                     self.object.end_of_life += ', ' + meal.name | ||||||
|  |         if 'fully_used' in form.data: | ||||||
|  |             self.object.is_ready = False | ||||||
|  |         self.object.save() | ||||||
|  |         # We redirect only the first parent | ||||||
|  |         parent_pk = meals[0].pk | ||||||
|  |         return HttpResponseRedirect(self.get_success_url(parent_pk=parent_pk)) | ||||||
|  |  | ||||||
|  |     def get_success_url(self, **kwargs): | ||||||
|  |         return reverse_lazy('food:transformedfood_view', kwargs={"pk": kwargs['parent_pk']}) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class FoodUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): | ||||||
|  |     """ | ||||||
|  |     A view to update Food | ||||||
|  |     """ | ||||||
|  |     model = Food | ||||||
|  |     extra_context = {"title": _("Update an aliment")} | ||||||
|  |     template_name = 'food/food_update.html' | ||||||
|  |  | ||||||
|     @transaction.atomic |     @transaction.atomic | ||||||
|     def form_valid(self, form): |     def form_valid(self, form): | ||||||
|         form.instance.creater = self.request.user |         form.instance.creater = self.request.user | ||||||
|         transformedfood_form = TransformedFoodForms(data=self.request.POST) |         food = Food.objects.get(pk=self.kwargs['pk']) | ||||||
|         if not transformedfood_form.is_valid(): |         old_allergens = list(food.allergens.all()).copy() | ||||||
|             return self.form_invalid(form) |  | ||||||
|  |  | ||||||
|  |         if food.polymorphic_ctype.model == 'transformedfood': | ||||||
|  |             old_ingredients = food.ingredients.all() | ||||||
|  |             form.instance.shelf_life = timedelta( | ||||||
|  |                 seconds=int(form.data['shelf_life']) * 60 * 60) | ||||||
|  |  | ||||||
|  |         food_form = self.get_form_class()(data=self.request.POST) | ||||||
|  |         if not food_form.is_valid(): | ||||||
|  |             return self.form_invalid(form) | ||||||
|         ans = super().form_valid(form) |         ans = super().form_valid(form) | ||||||
|         form.instance.update() |         if food.polymorphic_ctype.model == 'transformedfood': | ||||||
|  |             form.instance.save(old_ingredients=old_ingredients) | ||||||
|  |         else: | ||||||
|  |             form.instance.save(old_allergens=old_allergens) | ||||||
|         return ans |         return ans | ||||||
|  |  | ||||||
|  |     def get_form_class(self, **kwargs): | ||||||
|  |         food = Food.objects.get(pk=self.kwargs['pk']) | ||||||
|  |         if food.polymorphic_ctype.model == 'basicfood': | ||||||
|  |             return BasicFoodUpdateForms | ||||||
|  |         else: | ||||||
|  |             return TransformedFoodUpdateForms | ||||||
|  |  | ||||||
|  |     def get_form(self, **kwargs): | ||||||
|  |         form = super().get_form(**kwargs) | ||||||
|  |         if 'shelf_life' in form.initial: | ||||||
|  |             hours = form.initial['shelf_life'].days * 24 + form.initial['shelf_life'].seconds // 3600 | ||||||
|  |             form.initial['shelf_life'] = hours | ||||||
|  |         return form | ||||||
|  |  | ||||||
|     def get_success_url(self, **kwargs): |     def get_success_url(self, **kwargs): | ||||||
|         self.object.refresh_from_db() |         self.object.refresh_from_db() | ||||||
|         return reverse('food:food_view', kwargs={"pk": self.object.pk}) |         return reverse_lazy('food:food_view', kwargs={"pk": self.object.pk}) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class FoodDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): | ||||||
|  |     """ | ||||||
|  |     A view to see a food | ||||||
|  |     """ | ||||||
|  |     model = Food | ||||||
|  |     extra_context = {"title": _('Details of:')} | ||||||
|  |     context_object_name = "food" | ||||||
|  |     template_name = "food/food_detail.html" | ||||||
|  |  | ||||||
|     def get_context_data(self, **kwargs): |     def get_context_data(self, **kwargs): | ||||||
|         context = super().get_context_data(**kwargs) |         context = super().get_context_data(**kwargs) | ||||||
|  |         fields = ["name", "owner", "expiry_date", "allergens", "is_ready", "end_of_life", "order"] | ||||||
|  |  | ||||||
|  |         fields = dict([(field, getattr(self.object, field)) for field in fields]) | ||||||
|  |         if fields["is_ready"]: | ||||||
|  |             fields["is_ready"] = _("Yes") | ||||||
|  |         else: | ||||||
|  |             fields["is_ready"] = _("No") | ||||||
|  |         fields["allergens"] = ", ".join( | ||||||
|  |             allergen.name for allergen in fields["allergens"].all()) | ||||||
|  |  | ||||||
|  |         context["fields"] = [( | ||||||
|  |             Food._meta.get_field(field).verbose_name.capitalize(), | ||||||
|  |             value) for field, value in fields.items()] | ||||||
|  |         if self.object.QR_code.exists(): | ||||||
|  |             context["QR_code"] = self.object.QR_code.first() | ||||||
|  |         context["meals"] = self.object.transformed_ingredient_inv.all() | ||||||
|  |         context["update"] = PermissionBackend.check_perm(self.request, "food.change_food") | ||||||
|  |         context["add_ingredient"] = (self.object.end_of_life == '' and PermissionBackend.check_perm(self.request, "food.change_transformedfood")) | ||||||
|         return context |         return context | ||||||
|  |  | ||||||
|  |     def get(self, *args, **kwargs): | ||||||
|  |         if Food.objects.filter(pk=kwargs['pk']).count() != 1: | ||||||
|  |             return Http404 | ||||||
|  |         model = Food.objects.get(pk=kwargs['pk']).polymorphic_ctype.model | ||||||
|  |         if 'stop_redirect' in kwargs and kwargs['stop_redirect']: | ||||||
|  |             return super().get(*args, **kwargs) | ||||||
|  |         kwargs = {'pk': kwargs['pk']} | ||||||
|  |         if model == 'basicfood': | ||||||
|  |             return HttpResponseRedirect(reverse_lazy("food:basicfood_view", kwargs=kwargs)) | ||||||
|  |         return HttpResponseRedirect(reverse_lazy("food:transformedfood_view", kwargs=kwargs)) | ||||||
|  |  | ||||||
| class TransformedListView(ProtectQuerysetMixin, LoginRequiredMixin, MultiTableMixin, ListView): |  | ||||||
|     """ |  | ||||||
|     Displays ready TransformedFood |  | ||||||
|     """ |  | ||||||
|     model = TransformedFood |  | ||||||
|     tables = [TransformedFoodTable, TransformedFoodTable, TransformedFoodTable] |  | ||||||
|     extra_context = {"title": _("Transformed food")} |  | ||||||
|  |  | ||||||
|     def get_queryset(self, **kwargs): |  | ||||||
|         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") |  | ||||||
|         ] |  | ||||||
|  |  | ||||||
|  | class BasicFoodDetailView(FoodDetailView): | ||||||
|     def get_context_data(self, **kwargs): |     def get_context_data(self, **kwargs): | ||||||
|         context = super().get_context_data(**kwargs) |         context = super().get_context_data(**kwargs) | ||||||
|  |         fields = ['arrival_date', 'date_type'] | ||||||
|         # We choose a club which should work |         for field in fields: | ||||||
|         for membership in self.request.user.memberships.all(): |             context["fields"].append(( | ||||||
|             club_id = membership.club.id |                 BasicFood._meta.get_field(field).verbose_name.capitalize(), | ||||||
|             food = TransformedFood( |                 getattr(self.object, field) | ||||||
|                 name="", |             )) | ||||||
|                 owner_id=club_id, |  | ||||||
|                 creation_date=timezone.now(), |  | ||||||
|                 expiry_date=timezone.now(), |  | ||||||
|             ) |  | ||||||
|             if PermissionBackend.check_perm(self.request, "food.add_transformedfood", food): |  | ||||||
|                 context['can_create_meal'] = True |  | ||||||
|                 break |  | ||||||
|  |  | ||||||
|         tables = context["tables"] |  | ||||||
|         for name, table in zip(["table", "open", "served"], tables): |  | ||||||
|             context[name] = table |  | ||||||
|         return context |         return context | ||||||
|  |  | ||||||
|  |     def get(self, *args, **kwargs): | ||||||
|  |         if Food.objects.filter(pk=kwargs['pk']).count() == 1: | ||||||
|  |             kwargs['stop_redirect'] = (Food.objects.get(pk=kwargs['pk']).polymorphic_ctype.model == 'basicfood') | ||||||
|  |         return super().get(*args, **kwargs) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TransformedFoodDetailView(FoodDetailView): | ||||||
|  |     def get_context_data(self, **kwargs): | ||||||
|  |         context = super().get_context_data(**kwargs) | ||||||
|  |         context["fields"].append(( | ||||||
|  |             TransformedFood._meta.get_field("creation_date").verbose_name.capitalize(), | ||||||
|  |             self.object.creation_date | ||||||
|  |         )) | ||||||
|  |         context["fields"].append(( | ||||||
|  |             TransformedFood._meta.get_field("shelf_life").verbose_name.capitalize(), | ||||||
|  |             pretty_duration(self.object.shelf_life) | ||||||
|  |         )) | ||||||
|  |         context["foods"] = self.object.ingredients.all() | ||||||
|  |         context["manage_ingredients"] = True | ||||||
|  |         return context | ||||||
|  |  | ||||||
|  |     def get(self, *args, **kwargs): | ||||||
|  |         if Food.objects.filter(pk=kwargs['pk']).count() == 1: | ||||||
|  |             kwargs['stop_redirect'] = (Food.objects.get(pk=kwargs['pk']).polymorphic_ctype.model == 'transformedfood') | ||||||
|  |         return super().get(*args, **kwargs) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class QRCodeRedirectView(RedirectView): | ||||||
|  |     """ | ||||||
|  |     Redirects to the QR code creation page from Food List | ||||||
|  |     """ | ||||||
|  |     def get_redirect_url(self, *args, **kwargs): | ||||||
|  |         slug = self.request.GET.get('slug') | ||||||
|  |         if slug: | ||||||
|  |             return reverse_lazy('food:qrcode_create', kwargs={'slug': slug}) | ||||||
|  |         return reverse_lazy('food:list') | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| default_app_config = 'logs.apps.LogsConfig' | default_app_config = 'logs.apps.LogsConfig' | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from rest_framework import serializers | from rest_framework import serializers | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from .views import ChangelogViewSet | from .views import ChangelogViewSet | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django_filters.rest_framework import DjangoFilterBackend | from django_filters.rest_framework import DjangoFilterBackend | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.apps import AppConfig | from django.apps import AppConfig | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.contrib.contenttypes.models import ContentType | from django.contrib.contenttypes.models import ContentType | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| default_app_config = 'member.apps.MemberConfig' | default_app_config = 'member.apps.MemberConfig' | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.contrib import admin | from django.contrib import admin | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from rest_framework import serializers | from rest_framework import serializers | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from .views import ProfileViewSet, ClubViewSet, MembershipViewSet | from .views import ProfileViewSet, ClubViewSet, MembershipViewSet | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django_filters.rest_framework import DjangoFilterBackend | from django_filters.rest_framework import DjangoFilterBackend | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.apps import AppConfig | from django.apps import AppConfig | ||||||
| @@ -6,7 +6,7 @@ from django.conf import settings | |||||||
| from django.db.models.signals import post_save | from django.db.models.signals import post_save | ||||||
| from django.utils.translation import gettext_lazy as _ | from django.utils.translation import gettext_lazy as _ | ||||||
|  |  | ||||||
| from .signals import save_user_profile | from .signals import save_user_profile, update_wei_registration_fee_on_membership_creation, update_wei_registration_fee_on_club_change | ||||||
|  |  | ||||||
|  |  | ||||||
| class MemberConfig(AppConfig): | class MemberConfig(AppConfig): | ||||||
| @@ -17,7 +17,16 @@ class MemberConfig(AppConfig): | |||||||
|         """ |         """ | ||||||
|         Define app internal signals to interact with other apps |         Define app internal signals to interact with other apps | ||||||
|         """ |         """ | ||||||
|  |         from .models import Membership, Club | ||||||
|         post_save.connect( |         post_save.connect( | ||||||
|             save_user_profile, |             save_user_profile, | ||||||
|             sender=settings.AUTH_USER_MODEL, |             sender=settings.AUTH_USER_MODEL, | ||||||
|         ) |         ) | ||||||
|  |         post_save.connect( | ||||||
|  |             update_wei_registration_fee_on_membership_creation, | ||||||
|  |             sender=Membership | ||||||
|  |         ) | ||||||
|  |         post_save.connect( | ||||||
|  |             update_wei_registration_fee_on_club_change, | ||||||
|  |             sender=Club | ||||||
|  |         ) | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from cas_server.auth import DjangoAuthUser  # pragma: no cover | from cas_server.auth import DjangoAuthUser  # pragma: no cover | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import io | import io | ||||||
| @@ -23,7 +23,7 @@ from .models import Profile, Club, Membership | |||||||
| class CustomAuthenticationForm(AuthenticationForm): | class CustomAuthenticationForm(AuthenticationForm): | ||||||
|     permission_mask = forms.ModelChoiceField( |     permission_mask = forms.ModelChoiceField( | ||||||
|         label=_("Permission mask"), |         label=_("Permission mask"), | ||||||
|         queryset=PermissionMask.objects.order_by("rank"), |         queryset=PermissionMask.objects.order_by("-rank"), | ||||||
|         empty_label=None, |         empty_label=None, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
| @@ -44,6 +44,7 @@ class ProfileForm(forms.ModelForm): | |||||||
|     """ |     """ | ||||||
|     A form for the extras field provided by the :model:`member.Profile` model. |     A form for the extras field provided by the :model:`member.Profile` model. | ||||||
|     """ |     """ | ||||||
|  |     # Remove widget=forms.HiddenInput() if you want to use report frequency. | ||||||
|     report_frequency = forms.IntegerField(required=False, initial=0, label=_("Report frequency")) |     report_frequency = forms.IntegerField(required=False, initial=0, label=_("Report frequency")) | ||||||
|  |  | ||||||
|     last_report = forms.DateTimeField(required=False, disabled=True, label=_("Last report date")) |     last_report = forms.DateTimeField(required=False, disabled=True, label=_("Last report date")) | ||||||
| @@ -76,7 +77,8 @@ class ProfileForm(forms.ModelForm): | |||||||
|     class Meta: |     class Meta: | ||||||
|         model = Profile |         model = Profile | ||||||
|         fields = '__all__' |         fields = '__all__' | ||||||
|         exclude = ('user', 'email_confirmed', 'registration_valid', ) |         # Remove ml_[asso]_registration from exclude if the concerned association uses nk20 to manage its mailing list. | ||||||
|  |         exclude = ('user', 'email_confirmed', 'registration_valid', 'ml_sport_registration', ) | ||||||
|  |  | ||||||
|  |  | ||||||
| class ImageForm(forms.Form): | class ImageForm(forms.Form): | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import hashlib | import hashlib | ||||||
|   | |||||||
							
								
								
									
										46
									
								
								apps/member/migrations/0014_create_bda.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								apps/member/migrations/0014_create_bda.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | |||||||
|  | from django.db import migrations | ||||||
|  |  | ||||||
|  | def create_bda(apps, schema_editor): | ||||||
|  |     """ | ||||||
|  |     The club BDA is now pre-injected. | ||||||
|  |     """ | ||||||
|  |     Club = apps.get_model("member", "club") | ||||||
|  |     NoteClub = apps.get_model("note", "noteclub") | ||||||
|  |     Alias = apps.get_model("note", "alias") | ||||||
|  |     ContentType = apps.get_model('contenttypes', 'ContentType') | ||||||
|  |     polymorphic_ctype_id = ContentType.objects.get_for_model(NoteClub).id | ||||||
|  |      | ||||||
|  |     Club.objects.get_or_create( | ||||||
|  |         id=10, | ||||||
|  |         name="BDA", | ||||||
|  |         email="bda.ensparissaclay@gmail.com", | ||||||
|  |         require_memberships=True, | ||||||
|  |         membership_fee_paid=750, | ||||||
|  |         membership_fee_unpaid=750, | ||||||
|  |         membership_duration=396, | ||||||
|  |         membership_start="2024-08-01", | ||||||
|  |         membership_end="2025-09-30", | ||||||
|  |     ) | ||||||
|  |     NoteClub.objects.get_or_create( | ||||||
|  |         id=1937, | ||||||
|  |         club_id=10, | ||||||
|  |         polymorphic_ctype_id=polymorphic_ctype_id, | ||||||
|  |     ) | ||||||
|  |     Alias.objects.get_or_create( | ||||||
|  |         id=1937, | ||||||
|  |         note_id=1937, | ||||||
|  |         name="BDA", | ||||||
|  |         normalized_name="bda", | ||||||
|  |     ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Migration(migrations.Migration): | ||||||
|  |  | ||||||
|  |     dependencies = [ | ||||||
|  |         ('member', '0013_auto_20240801_1436'), | ||||||
|  |     ] | ||||||
|  |      | ||||||
|  |     operations = [ | ||||||
|  |         migrations.RunPython(create_bda), | ||||||
|  |     ] | ||||||
|  |  | ||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import datetime | import datetime | ||||||
| @@ -438,8 +438,6 @@ class Membership(models.Model): | |||||||
|             ) |             ) | ||||||
|             if hasattr(self, '_force_renew_parent') and self._force_renew_parent: |             if hasattr(self, '_force_renew_parent') and self._force_renew_parent: | ||||||
|                 new_membership._force_renew_parent = True |                 new_membership._force_renew_parent = True | ||||||
|             if hasattr(self, '_soge') and self._soge: |  | ||||||
|                 new_membership._soge = True |  | ||||||
|             if hasattr(self, '_force_save') and self._force_save: |             if hasattr(self, '_force_save') and self._force_save: | ||||||
|                 new_membership._force_save = True |                 new_membership._force_save = True | ||||||
|             new_membership.save() |             new_membership.save() | ||||||
| @@ -458,8 +456,6 @@ class Membership(models.Model): | |||||||
|             # Renew the previous membership of the parent club |             # Renew the previous membership of the parent club | ||||||
|             parent_membership = parent_membership.first() |             parent_membership = parent_membership.first() | ||||||
|             parent_membership._force_renew_parent = True |             parent_membership._force_renew_parent = True | ||||||
|             if hasattr(self, '_soge'): |  | ||||||
|                 parent_membership._soge = True |  | ||||||
|             if hasattr(self, '_force_save'): |             if hasattr(self, '_force_save'): | ||||||
|                 parent_membership._force_save = True |                 parent_membership._force_save = True | ||||||
|             parent_membership.renew() |             parent_membership.renew() | ||||||
| @@ -471,8 +467,6 @@ class Membership(models.Model): | |||||||
|                 date_start=self.date_start, |                 date_start=self.date_start, | ||||||
|             ) |             ) | ||||||
|             parent_membership._force_renew_parent = True |             parent_membership._force_renew_parent = True | ||||||
|             if hasattr(self, '_soge'): |  | ||||||
|                 parent_membership._soge = True |  | ||||||
|             if hasattr(self, '_force_save'): |             if hasattr(self, '_force_save'): | ||||||
|                 parent_membership._force_save = True |                 parent_membership._force_save = True | ||||||
|             parent_membership.save() |             parent_membership.save() | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -13,3 +13,25 @@ def save_user_profile(instance, created, raw, **_kwargs): | |||||||
|             instance.profile.email_confirmed = True |             instance.profile.email_confirmed = True | ||||||
|             instance.profile.registration_valid = True |             instance.profile.registration_valid = True | ||||||
|         instance.profile.save() |         instance.profile.save() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def update_wei_registration_fee_on_membership_creation(sender, instance, created, **kwargs): | ||||||
|  |     if created: | ||||||
|  |         from wei.models import WEIRegistration | ||||||
|  |         if instance.club.id == 1 or instance.club.id == 2: | ||||||
|  |             registrations = WEIRegistration.objects.filter( | ||||||
|  |                 user=instance.user, | ||||||
|  |                 wei__year=instance.date_start.year, | ||||||
|  |             ) | ||||||
|  |             for r in registrations: | ||||||
|  |                 r.save() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def update_wei_registration_fee_on_club_change(sender, instance, **kwargs): | ||||||
|  |     from wei.models import WEIRegistration | ||||||
|  |     if instance.id == 1 or instance.id == 2: | ||||||
|  |         registrations = WEIRegistration.objects.filter( | ||||||
|  |             wei__year=instance.membership_start.year, | ||||||
|  |         ) | ||||||
|  |         for r in registrations: | ||||||
|  |             r.save() | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from datetime import date | from datetime import date | ||||||
|   | |||||||
| @@ -60,7 +60,10 @@ | |||||||
| {% if user_object.pk == user.pk %} | {% if user_object.pk == user.pk %} | ||||||
|     <div class="text-center"> |     <div class="text-center"> | ||||||
|         <a class="small badge badge-secondary" href="{% url 'member:auth_token' %}"> |         <a class="small badge badge-secondary" href="{% url 'member:auth_token' %}"> | ||||||
|             <i class="fa fa-cogs"></i>{% trans 'API token' %} |             <i class="fa fa-cogs"></i> {% trans 'API token' %} | ||||||
|  |         </a> | ||||||
|  |         <a class="small badge badge-secondary" href="{% url 'member:qr_code' user_object.pk %}"> | ||||||
|  |             <i class="fa fa-qrcode"></i> {% trans 'QR Code' %} | ||||||
|         </a> |         </a> | ||||||
|     </div> |     </div> | ||||||
| {% endif %} | {% endif %} | ||||||
|   | |||||||
| @@ -20,11 +20,13 @@ SPDX-License-Identifier: GPL-3.0-or-later | |||||||
|       </form> |       </form> | ||||||
|     </div> |     </div> | ||||||
|     <!-- MODAL TO CROP THE IMAGE --> |     <!-- MODAL TO CROP THE IMAGE --> | ||||||
|     <div class="modal fade" id="modalCrop"> |     <div class="modal fade" id="modalCrop" data-backdrop="static"> | ||||||
|       <div class="modal-dialog"> |       <div class="modal-dialog"> | ||||||
|         <div class="modal-content"> |         <div class="modal-content"> | ||||||
|           <div class="modal-body"> |             <div class="modal-body-wrapper" style="width: 500px; height: 500px; padding: 16px;"> | ||||||
|             <img src="" id="modal-image" style="max-width: 100%;"> |               <div class="modal-body" style="width: 100%; height: 100%; padding: 0"> | ||||||
|  |                 <img src="" id="modal-image" style="display: block; max-width: 100%;"> | ||||||
|  |               </div> | ||||||
|             </div> |             </div> | ||||||
|           <div class="modal-footer"> |           <div class="modal-footer"> | ||||||
|             <div class="btn-group pull-left" role="group"> |             <div class="btn-group pull-left" role="group"> | ||||||
|   | |||||||
							
								
								
									
										36
									
								
								apps/member/templates/member/qr_code.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								apps/member/templates/member/qr_code.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | |||||||
|  | {% extends "base.html" %} | ||||||
|  | {% comment %} | ||||||
|  | SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  | {% endcomment %} | ||||||
|  | {% load i18n %} | ||||||
|  |  | ||||||
|  | {% block content %} | ||||||
|  | <div class="card bg-light"> | ||||||
|  |   	<h3 class="card-header text-center"> | ||||||
|  | 		{% trans "QR Code for" %} {{ user_object.username }} ({{ user_object.first_name }} {{user_object.last_name }}) | ||||||
|  |   	</h3> | ||||||
|  |   	<div class="text-center" id="qrcode"> | ||||||
|  |   	</div> | ||||||
|  | </div> | ||||||
|  |  | ||||||
|  |  | ||||||
|  | {% endblock %} | ||||||
|  |  | ||||||
|  | {% block extrajavascript %} | ||||||
|  | <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js" integrity="sha512-CNgIRecGo7nphbeZ04Sc13ka07paqdeTu0WR1IM4kNcpmBAUSHSQX0FslNhTDadL4O5SAGapGt4FodqL8My0mA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> | ||||||
|  | <script> | ||||||
|  | 	var qrc = new QRCode(document.getElementById("qrcode"), { | ||||||
|  | 		text: "{{ user_object.pk }}\0", | ||||||
|  | 		width: 1024, | ||||||
|  | 		height: 1024 | ||||||
|  | 	}); | ||||||
|  | </script> | ||||||
|  | {% endblock %} | ||||||
|  |  | ||||||
|  | {% block extracss %} | ||||||
|  | <style> | ||||||
|  | img { | ||||||
|  |     width: 100% | ||||||
|  | } | ||||||
|  | </style> | ||||||
|  | {% endblock %} | ||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from datetime import date | from datetime import date | ||||||
|   | |||||||
| @@ -44,7 +44,7 @@ class TemplateLoggedInTests(TestCase): | |||||||
|         self.assertRedirects(response, settings.LOGIN_REDIRECT_URL, 302, 302) |         self.assertRedirects(response, settings.LOGIN_REDIRECT_URL, 302, 302) | ||||||
|  |  | ||||||
|     def test_logout(self): |     def test_logout(self): | ||||||
|         response = self.client.get(reverse("logout")) |         response = self.client.post(reverse("logout")) | ||||||
|         self.assertEqual(response.status_code, 200) |         self.assertEqual(response.status_code, 200) | ||||||
|  |  | ||||||
|     def test_admin_index(self): |     def test_admin_index(self): | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.urls import path | from django.urls import path | ||||||
| @@ -25,4 +25,5 @@ urlpatterns = [ | |||||||
|     path('user/<int:pk>/aliases/', views.ProfileAliasView.as_view(), name="user_alias"), |     path('user/<int:pk>/aliases/', views.ProfileAliasView.as_view(), name="user_alias"), | ||||||
|     path('user/<int:pk>/trust', views.ProfileTrustView.as_view(), name="user_trust"), |     path('user/<int:pk>/trust', views.ProfileTrustView.as_view(), name="user_trust"), | ||||||
|     path('manage-auth-token/', views.ManageAuthTokens.as_view(), name='auth_token'), |     path('manage-auth-token/', views.ManageAuthTokens.as_view(), name='auth_token'), | ||||||
|  |     path('user/<int:pk>/qr_code/', views.QRCodeView.as_view(), name='qr_code'), | ||||||
| ] | ] | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from datetime import timedelta, date | from datetime import timedelta, date | ||||||
| @@ -26,6 +26,7 @@ from note_kfet.middlewares import _set_current_request | |||||||
| from permission.backends import PermissionBackend | from permission.backends import PermissionBackend | ||||||
| from permission.models import Role | from permission.models import Role | ||||||
| from permission.views import ProtectQuerysetMixin, ProtectedCreateView | from permission.views import ProtectQuerysetMixin, ProtectedCreateView | ||||||
|  | from django import forms | ||||||
|  |  | ||||||
| from .forms import UserForm, ProfileForm, ImageForm, ClubForm, MembershipForm, \ | from .forms import UserForm, ProfileForm, ImageForm, ClubForm, MembershipForm, \ | ||||||
|     CustomAuthenticationForm, MembershipRolesForm |     CustomAuthenticationForm, MembershipRolesForm | ||||||
| @@ -72,11 +73,24 @@ class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): | |||||||
|         form.fields['email'].required = True |         form.fields['email'].required = True | ||||||
|         form.fields['email'].help_text = _("This address must be valid.") |         form.fields['email'].help_text = _("This address must be valid.") | ||||||
|  |  | ||||||
|         if PermissionBackend.check_perm(self.request, "member.change_profile", context['user_object'].profile): |         profile_form = self.profile_form(instance=context['user_object'].profile, | ||||||
|             context['profile_form'] = self.profile_form(instance=context['user_object'].profile, |  | ||||||
|                                          data=self.request.POST if self.request.POST else None) |                                          data=self.request.POST if self.request.POST else None) | ||||||
|  |  | ||||||
|         if not self.object.profile.report_frequency: |         if not self.object.profile.report_frequency: | ||||||
|                 del context['profile_form'].fields["last_report"] |             del profile_form.fields["last_report"] | ||||||
|  |  | ||||||
|  |         fields_to_check = list(profile_form.fields.keys()) | ||||||
|  |         fields_modifiable = False | ||||||
|  |  | ||||||
|  |         # Delete the fields for which the user does not have the permission to modify | ||||||
|  |         for field_name in fields_to_check: | ||||||
|  |             if not PermissionBackend.check_perm(self.request, f"member.change_profile_{field_name}", context['user_object'].profile): | ||||||
|  |                 profile_form.fields[field_name].widget = forms.HiddenInput() | ||||||
|  |             else: | ||||||
|  |                 fields_modifiable = True | ||||||
|  |  | ||||||
|  |         if fields_modifiable: | ||||||
|  |             context['profile_form'] = profile_form | ||||||
|  |  | ||||||
|         return context |         return context | ||||||
|  |  | ||||||
| @@ -388,6 +402,14 @@ class ManageAuthTokens(LoginRequiredMixin, TemplateView): | |||||||
|         context['token'] = Token.objects.get_or_create(user=self.request.user)[0] |         context['token'] = Token.objects.get_or_create(user=self.request.user)[0] | ||||||
|         return context |         return context | ||||||
|  |  | ||||||
|  | class QRCodeView(LoginRequiredMixin, DetailView): | ||||||
|  |     """ | ||||||
|  |     Affiche le QR Code | ||||||
|  |     """ | ||||||
|  |     model = User | ||||||
|  |     context_object_name = "user_object" | ||||||
|  |     template_name = "member/qr_code.html" | ||||||
|  |     extra_context = {"title": _("QR Code")} | ||||||
|  |  | ||||||
| # ******************************* # | # ******************************* # | ||||||
| #              CLUB               # | #              CLUB               # | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| default_app_config = 'note.apps.NoteConfig' | default_app_config = 'note.apps.NoteConfig' | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.contrib import admin | from django.contrib import admin | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from .views import NotePolymorphicViewSet, AliasViewSet, ConsumerViewSet, \ | from .views import NotePolymorphicViewSet, AliasViewSet, ConsumerViewSet, \ | ||||||
| @@ -13,7 +13,7 @@ def register_note_urls(router, path): | |||||||
|     router.register(path + '/note', NotePolymorphicViewSet) |     router.register(path + '/note', NotePolymorphicViewSet) | ||||||
|     router.register(path + '/alias', AliasViewSet) |     router.register(path + '/alias', AliasViewSet) | ||||||
|     router.register(path + '/trust', TrustViewSet) |     router.register(path + '/trust', TrustViewSet) | ||||||
|     router.register(path + '/consumer', ConsumerViewSet) |     router.register(path + '/consumer', ConsumerViewSet, basename='alias2') | ||||||
|  |  | ||||||
|     router.register(path + '/transaction/category', TemplateCategoryViewSet) |     router.register(path + '/transaction/category', TemplateCategoryViewSet) | ||||||
|     router.register(path + '/transaction/transaction', TransactionViewSet) |     router.register(path + '/transaction/transaction', TransactionViewSet) | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.apps import AppConfig | from django.apps import AppConfig | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser, Trust | from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser, Trust | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import unicodedata | import unicodedata | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.core.exceptions import ValidationError | from django.core.exceptions import ValidationError | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| from django.utils import timezone | from django.utils import timezone | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| // Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | // Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| // SPDX-License-Identifier: GPL-3.0-or-later | // SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| // When a transaction is performed, lock the interface to prevent spam clicks. | // When a transaction is performed, lock the interface to prevent spam clicks. | ||||||
| @@ -245,7 +245,7 @@ function consume (source, source_alias, dest, quantity, amount, reason, type, ca | |||||||
|           invalidity_reason: 'Solde insuffisant', |           invalidity_reason: 'Solde insuffisant', | ||||||
|           polymorphic_ctype: type, |           polymorphic_ctype: type, | ||||||
|           resourcetype: 'RecurrentTransaction', |           resourcetype: 'RecurrentTransaction', | ||||||
|           source: source, |           source: source.id, | ||||||
|           source_alias: source_alias, |           source_alias: source_alias, | ||||||
|           destination: dest, |           destination: dest, | ||||||
|           template: template |           template: template | ||||||
| @@ -294,3 +294,10 @@ searchbar.addEventListener("keyup", function (e) { | |||||||
|   if (firstMatch && e.key === "Enter") |   if (firstMatch && e.key === "Enter") | ||||||
|     firstMatch.click() |     firstMatch.click() | ||||||
| }); | }); | ||||||
|  |  | ||||||
|  | function createshiny() { | ||||||
|  |   const list_btn = document.querySelectorAll('.btn-outline-dark') | ||||||
|  |   const shiny_class = list_btn[Math.floor(Math.random() * list_btn.length)].classList | ||||||
|  |   shiny_class.replace('btn-outline-dark', 'btn-outline-dark-shiny') | ||||||
|  | } | ||||||
|  | createshiny() | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay | # Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||||
| # SPDX-License-Identifier: GPL-3.0-or-later | # SPDX-License-Identifier: GPL-3.0-or-later | ||||||
|  |  | ||||||
| import html | import html | ||||||
|   | |||||||
| @@ -89,7 +89,7 @@ SPDX-License-Identifier: GPL-2.0-or-later | |||||||
|                 </ul> |                 </ul> | ||||||
|                 <div class="card-body"> |                 <div class="card-body"> | ||||||
|                     <select id="debit_type" class="form-control custom-select d-none"> |                     <select id="debit_type" class="form-control custom-select d-none"> | ||||||
|                         {% for special_type in special_types %} |                         {% for special_type in special_types|slice:"::-1" %} | ||||||
|                             <option value="{{ special_type.id }}">{{ special_type.special_type }}</option> |                             <option value="{{ special_type.id }}">{{ special_type.special_type }}</option> | ||||||
|                         {% endfor %} |                         {% endfor %} | ||||||
|                     </select> |                     </select> | ||||||
|   | |||||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user