From 14e8d963d6b7e074a9350064440f8c0bdb758a9f Mon Sep 17 00:00:00 2001 From: Maurice Debray Date: Tue, 15 Mar 2022 01:04:01 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20date=20de=20cr=C3=A9ation/mis?= =?UTF-8?q?e=20=C3=A0=20jour=20dans=20le=20mod=C3=A8le=20Participants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calendrier/admin.py | 6 +++++ .../migrations/0007_auto_20220314_2320.py | 25 +++++++++++++++++++ calendrier/models.py | 2 ++ calendrier/views.py | 12 ++++----- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 calendrier/migrations/0007_auto_20220314_2320.py diff --git a/calendrier/admin.py b/calendrier/admin.py index a8477d0..616027a 100644 --- a/calendrier/admin.py +++ b/calendrier/admin.py @@ -2,5 +2,11 @@ from django.contrib import admin from .models import Event, Participants +class ParticipantsAdmin(admin.ModelAdmin): + fields = ["event", "participant", "reponse", "instrument", "instrument_autre", "dont_play_main", "details", "creationDate", "updateDate" ] + readonly_fields = [ "creationDate", "updateDate" ] + list_display = ["participant", "event", "reponse", "creationDate", "updateDate" ] + # Add event by admin page return a 502 error admin.site.register(Event) +admin.site.register(Participants, ParticipantsAdmin) diff --git a/calendrier/migrations/0007_auto_20220314_2320.py b/calendrier/migrations/0007_auto_20220314_2320.py new file mode 100644 index 0000000..50f830c --- /dev/null +++ b/calendrier/migrations/0007_auto_20220314_2320.py @@ -0,0 +1,25 @@ +# Generated by Django 2.2.25 on 2022-03-14 23:20 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('calendrier', '0006_auto_20210929_1629'), + ] + + operations = [ + migrations.AddField( + model_name='participants', + name='creationDate', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now, verbose_name='Date de création'), + preserve_default=False, + ), + migrations.AddField( + model_name='participants', + name='updateDate', + field=models.DateTimeField(auto_now=True, verbose_name='Dernière mise à jour'), + ), + ] diff --git a/calendrier/models.py b/calendrier/models.py index f6754b3..c562094 100644 --- a/calendrier/models.py +++ b/calendrier/models.py @@ -73,3 +73,5 @@ class Participants(models.Model): choices=[("Non", _("Non")), ("Oui", _("Oui"))], ) details = models.CharField(max_length=50, blank=True) + creationDate = models.DateTimeField(auto_now_add=True, verbose_name = _("Date de création")) + updateDate = models.DateTimeField(auto_now=True, verbose_name = _("Dernière mise à jour")) diff --git a/calendrier/views.py b/calendrier/views.py index 702b4bd..c80ff0b 100644 --- a/calendrier/views.py +++ b/calendrier/views.py @@ -373,16 +373,16 @@ class ReponseEvent(LoginRequiredMixin, TemplateView): return context def post(self, request, *args, **kwargs): - form = self.form_class(request.POST) ev = get_object_or_404(Event, id=self.kwargs["id"]) part = request.user.profile + try: + p = Participants.objects.get(event=ev, participant=part) + except Participants.DoesNotExist: + p = None + form = self.form_class(request.POST, instance = p) if form.is_valid(): - try: - p = Participants.objects.get(event=ev, participant=part) - p.delete() - except Participants.DoesNotExist: - pass obj = form.save(commit=False) + # Si la participation existe déjà, ces 2 ligne sont redondantes obj.event = ev obj.participant = part obj.save()