From 9e50a825e0f2f9287948631efeb0e679e5afd33b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 8 Jul 2016 00:18:58 +0200 Subject: [PATCH 1/8] Ajoute un switch sur les tirages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le switch permet d'autoriser/interdire le lancement d'un tirage. Il s'agit d'une sécurité face aux erreurs d'inattention. Le champ `token` du modèle `Tirage` devient `tokens` et stocke les graines des tirages déjà lancés si le tirage est lancé plusieurs fois. --- bda/admin.py | 5 ++-- bda/migrations/0004_add_tirage_switch.py | 30 ++++++++++++++++++++++++ bda/models.py | 4 +++- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 bda/migrations/0004_add_tirage_switch.py diff --git a/bda/admin.py b/bda/admin.py index 604ac883..8644a264 100644 --- a/bda/admin.py +++ b/bda/admin.py @@ -144,8 +144,9 @@ class SpectacleAdmin(admin.ModelAdmin): class TirageAdmin(admin.ModelAdmin): model = Tirage - list_display = ("title", "ouverture", "fermeture", "active") - readonly_fields = ("token", ) + list_display = ("title", "ouverture", "fermeture", "active", + "enable_do_tirage") + readonly_fields = ("tokens", ) list_filter = ("active", ) search_fields = ("title", ) diff --git a/bda/migrations/0004_add_tirage_switch.py b/bda/migrations/0004_add_tirage_switch.py new file mode 100644 index 00000000..1f566b6d --- /dev/null +++ b/bda/migrations/0004_add_tirage_switch.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +from django.utils import timezone + +def forwards_func(apps, schema_editor): + Tirage = apps.get_model("bda", "Tirage") + db_alias = schema_editor.connection.alias + for tirage in Tirage.objects.all(): + tirage.tokens = "Before %s\n\"\"\"%s\"\"\"\n" % ( + timezone.now().strftime("%y-%m-%d %H:%M:%S"), + tirage.tokens) + tirage.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('bda', '0003_update_tirage_and_spectacle'), + ] + + operations = [ + migrations.RenameField('tirage', 'token', 'tokens'), + migrations.AddField( + model_name='tirage', + name='enable_do_tirage', + field=models.BooleanField(default=False, verbose_name=b'Le tirage peut \xc3\xaatre lanc\xc3\xa9'), + ), + migrations.RunPython(forwards_func, migrations.RunPython.noop), + ] diff --git a/bda/models.py b/bda/models.py index 16ea4818..01429dc2 100644 --- a/bda/models.py +++ b/bda/models.py @@ -10,8 +10,10 @@ class Tirage(models.Model): title = models.CharField("Titre", max_length=300) ouverture = models.DateTimeField("Date et heure d'ouverture du tirage") fermeture = models.DateTimeField("Date et heure de fermerture du tirage") - token = models.TextField("Graine du tirage", blank=True) + tokens = models.TextField("Graine(s) du tirage", blank=True) active = models.BooleanField("Tirage actif", default=False) + enable_do_tirage = models.BooleanField("Le tirage peut être lancé", + default=False) def date_no_seconds(self): return self.fermeture.strftime('%d %b %Y %H:%M') From 45864fdcb00c20ff14a0c72ce80718b26890b8dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 8 Jul 2016 00:39:31 +0200 Subject: [PATCH 2/8] Adapte la vue du tirage. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Désormais, les tirages peuvent être lancés sans modifier le code et avec moult avertissements. --- bda/templates/bda-attrib-extra.html | 2 +- bda/templates/bda-attrib.html | 7 ++++++- bda/templates/bda-token.html | 2 +- bda/templates/tirage-failed.html | 9 +++++++++ bda/views.py | 23 +++++++++++------------ gestioncof/static/css/cof.css | 9 +++++++++ 6 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 bda/templates/tirage-failed.html diff --git a/bda/templates/bda-attrib-extra.html b/bda/templates/bda-attrib-extra.html index 8ffccf34..4286dfc9 100644 --- a/bda/templates/bda-attrib-extra.html +++ b/bda/templates/bda-attrib-extra.html @@ -2,7 +2,7 @@ {% block extracontent %} -

Attribution (détails)

+

Attributions (détails)

Token :

{{ token }}

Placés : {{ total_slots }} ; Déçus : {{ total_losers }}

diff --git a/bda/templates/bda-attrib.html b/bda/templates/bda-attrib.html index f0bfd955..81540594 100644 --- a/bda/templates/bda-attrib.html +++ b/bda/templates/bda-attrib.html @@ -7,7 +7,12 @@ {% block realcontent %} -

Attribution

+

Attributions

+ +
+

Pensez à désactiver le lancement du tirage dès maintenant dans +l'interface admin

+

Token :

{{ token }}

Placés : {{ total_slots }} ; Déçus : {{ total_losers }}

diff --git a/bda/templates/bda-token.html b/bda/templates/bda-token.html index cbe72a76..b05a5b0d 100644 --- a/bda/templates/bda-token.html +++ b/bda/templates/bda-token.html @@ -8,6 +8,6 @@
{{ form.token }}
- + {% endblock %} diff --git a/bda/templates/tirage-failed.html b/bda/templates/tirage-failed.html new file mode 100644 index 00000000..a9f7e7d4 --- /dev/null +++ b/bda/templates/tirage-failed.html @@ -0,0 +1,9 @@ +{% extends "base_title.html" %} + +{% block realcontent %} +

Raté, le tirage ne peut pas être lancé !

+ +

Si vous savez ce que vous faites, vous pouvez autoriser le lancement du +tirage dans l'interface admin. +Pensez à le désactiver à nouveau ensuite.

+{% endblock %} diff --git a/bda/views.py b/bda/views.py index 1eab066d..7c10e0f2 100644 --- a/bda/views.py +++ b/bda/views.py @@ -181,11 +181,11 @@ def inscription(request, tirage_id): def do_tirage(request, tirage_id): tirage_elt = get_object_or_404(Tirage, id=tirage_id) + if not tirage_elt.enable_do_tirage: + return render(request, "tirage-failed.html", {'tirage': tirage_elt}) form = TokenForm(request.POST) if not form.is_valid(): return tirage(request) - tirage_elt.token = form.cleaned_data['token'] - tirage_elt.save() start = time.time() data = {} shows = tirage_elt.spectacle_set.select_related().all() @@ -235,16 +235,15 @@ def do_tirage(request, tirage_id): members2 = members2.items() data["members2"] = sorted(members2, key=lambda m: m[0].user.last_name) # À partir d'ici, le tirage devient effectif - # FIXME: Établir les conditions de validations (formulaire ?) - # cf. issue #32 - if False: - Attribution.objects.filter( - spectacle__tirage=tirage_elt - ).delete() - for (show, members, _) in results: - for (member, _, _, _) in members: - attrib = Attribution(spectacle=show, participant=member) - attrib.save() + Attribution.objects.filter(spectacle__tirage=tirage_elt).delete() + tirage_elt.tokens += "%s\n\"\"\"%s\"\"\"\n" % ( + timezone.now().strftime("%y-%m-%d %H:%M:%S"), + form.cleaned_data['token']) + tirage_elt.save() + for (show, members, _) in results: + for (member, _, _, _) in members: + attrib = Attribution(spectacle=show, participant=member) + attrib.save() return render(request, "bda-attrib-extra.html", data) else: return render(request, "bda-attrib.html", data) diff --git a/gestioncof/static/css/cof.css b/gestioncof/static/css/cof.css index d59e6ded..6cbc6c5d 100644 --- a/gestioncof/static/css/cof.css +++ b/gestioncof/static/css/cof.css @@ -246,6 +246,15 @@ fieldset legend { background-color: transparent; } +.important { + font-weight: bold; + color: #F90; +} + +.important a { + color: #F90; +} + #main form ul.errorlist li { font-weight: bold; color: #B00000; From 443b7328596c0296184c1ff22c1be51433b83faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 8 Jul 2016 20:27:27 +0200 Subject: [PATCH 3/8] =?UTF-8?q?D=C3=A9sactive=20le=20lancement=20du=20tira?= =?UTF-8?q?ge=20automatiquement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Après le lancement du tirage, l'attribut `enable_do_tirage` est automatiquement passé à `False` --- bda/templates/bda-attrib.html | 5 +++-- bda/views.py | 1 + gestioncof/static/css/cof.css | 13 ++++--------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/bda/templates/bda-attrib.html b/bda/templates/bda-attrib.html index 81540594..ce3d0cf1 100644 --- a/bda/templates/bda-attrib.html +++ b/bda/templates/bda-attrib.html @@ -10,8 +10,9 @@

Attributions


-

Pensez à désactiver le lancement du tirage dès maintenant dans -l'interface admin

+

Pour raison de sécurité, le lancement du tirage + a été désactivé. Vous pouvez le réactiver dans + l'interface admin

Token :

{{ token }}
diff --git a/bda/views.py b/bda/views.py index 7c10e0f2..f97a6c70 100644 --- a/bda/views.py +++ b/bda/views.py @@ -239,6 +239,7 @@ def do_tirage(request, tirage_id): tirage_elt.tokens += "%s\n\"\"\"%s\"\"\"\n" % ( timezone.now().strftime("%y-%m-%d %H:%M:%S"), form.cleaned_data['token']) + tirage_elt.enable_do_tirage = False tirage_elt.save() for (show, members, _) in results: for (member, _, _, _) in members: diff --git a/gestioncof/static/css/cof.css b/gestioncof/static/css/cof.css index 6cbc6c5d..3fb48ffb 100644 --- a/gestioncof/static/css/cof.css +++ b/gestioncof/static/css/cof.css @@ -240,21 +240,16 @@ fieldset legend { background-color: transparent; } +.success a { + color: inherit; +} + .error { font-weight: bold; color: #B00000; background-color: transparent; } -.important { - font-weight: bold; - color: #F90; -} - -.important a { - color: #F90; -} - #main form ul.errorlist li { font-weight: bold; color: #B00000; From a3180fa479a9bf541b3158f100f22d8d6351b88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 8 Jul 2016 20:33:26 +0200 Subject: [PATCH 4/8] =?UTF-8?q?Pr=C3=A9vient=20plus=20t=C3=B4t=20qu'un=20t?= =?UTF-8?q?irage=20ne=20peut=20=C3=AAtre=20lanc=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GestioCOF ne présente pas le formulaire pour la graine du tirage si l'attribut `enable_do_tirage` du tirage est à `False`. --- bda/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bda/views.py b/bda/views.py index f97a6c70..39ed17e2 100644 --- a/bda/views.py +++ b/bda/views.py @@ -181,8 +181,6 @@ def inscription(request, tirage_id): def do_tirage(request, tirage_id): tirage_elt = get_object_or_404(Tirage, id=tirage_id) - if not tirage_elt.enable_do_tirage: - return render(request, "tirage-failed.html", {'tirage': tirage_elt}) form = TokenForm(request.POST) if not form.is_valid(): return tirage(request) @@ -251,6 +249,9 @@ def do_tirage(request, tirage_id): @buro_required def tirage(request, tirage_id): + tirage_elt = get_object_or_404(Tirage, id=tirage_id) + if not tirage_elt.enable_do_tirage: + return render(request, "tirage-failed.html", {'tirage': tirage_elt}) if request.POST: form = TokenForm(request.POST) if form.is_valid(): From fd5b22169fd0f1ca3ba3a00b9cc0aeec2a4f4537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Wed, 13 Jul 2016 10:46:46 +0200 Subject: [PATCH 5/8] Update migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use les `bd_alias` de Django - N'ajoute le 'before """"""' dans la liste des tokens si le champ token était précédement vide. --- bda/migrations/0004_add_tirage_switch.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bda/migrations/0004_add_tirage_switch.py b/bda/migrations/0004_add_tirage_switch.py index 1f566b6d..7429197b 100644 --- a/bda/migrations/0004_add_tirage_switch.py +++ b/bda/migrations/0004_add_tirage_switch.py @@ -4,14 +4,17 @@ from __future__ import unicode_literals from django.db import migrations, models from django.utils import timezone + def forwards_func(apps, schema_editor): Tirage = apps.get_model("bda", "Tirage") db_alias = schema_editor.connection.alias - for tirage in Tirage.objects.all(): - tirage.tokens = "Before %s\n\"\"\"%s\"\"\"\n" % ( - timezone.now().strftime("%y-%m-%d %H:%M:%S"), - tirage.tokens) - tirage.save() + for tirage in Tirage.objects.using(db_alias).all(): + if tirage.tokens: + tirage.tokens = "Before %s\n\"\"\"%s\"\"\"\n" % ( + timezone.now().strftime("%y-%m-%d %H:%M:%S"), + tirage.tokens) + tirage.save() + class Migration(migrations.Migration): From 1336e11eef8bbc9b086cdccd7b42be382e0c0b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 Jul 2016 01:28:25 +0200 Subject: [PATCH 6/8] Petites corrections --- bda/templates/bda-token.html | 2 +- bda/templates/tirage-failed.html | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/bda/templates/bda-token.html b/bda/templates/bda-token.html index b05a5b0d..c464fb97 100644 --- a/bda/templates/bda-token.html +++ b/bda/templates/bda-token.html @@ -8,6 +8,6 @@
{{ form.token }}
- + {% endblock %} diff --git a/bda/templates/tirage-failed.html b/bda/templates/tirage-failed.html index a9f7e7d4..78c547fd 100644 --- a/bda/templates/tirage-failed.html +++ b/bda/templates/tirage-failed.html @@ -4,6 +4,5 @@

Raté, le tirage ne peut pas être lancé !

Si vous savez ce que vous faites, vous pouvez autoriser le lancement du -tirage dans l'interface admin. -Pensez à le désactiver à nouveau ensuite.

+tirage dans l'interface admin.

{% endblock %} From 71b810695f95296abe1a3e9fb770c0bcd9744243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 Jul 2016 02:16:53 +0200 Subject: [PATCH 7/8] Interdit de lancer un tirage avant sa fermeture MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Et résout un conflit dans les migrations --- ...{0004_add_tirage_switch.py => 0005_add_tirage_switch.py} | 2 +- bda/templates/tirage-failed.html | 6 ++++-- bda/views.py | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) rename bda/migrations/{0004_add_tirage_switch.py => 0005_add_tirage_switch.py} (94%) diff --git a/bda/migrations/0004_add_tirage_switch.py b/bda/migrations/0005_add_tirage_switch.py similarity index 94% rename from bda/migrations/0004_add_tirage_switch.py rename to bda/migrations/0005_add_tirage_switch.py index 7429197b..fa3644f6 100644 --- a/bda/migrations/0004_add_tirage_switch.py +++ b/bda/migrations/0005_add_tirage_switch.py @@ -19,7 +19,7 @@ def forwards_func(apps, schema_editor): class Migration(migrations.Migration): dependencies = [ - ('bda', '0003_update_tirage_and_spectacle'), + ('bda', '0004_mails-rappel'), ] operations = [ diff --git a/bda/templates/tirage-failed.html b/bda/templates/tirage-failed.html index 78c547fd..74849487 100644 --- a/bda/templates/tirage-failed.html +++ b/bda/templates/tirage-failed.html @@ -3,6 +3,8 @@ {% block realcontent %}

Raté, le tirage ne peut pas être lancé !

-

Si vous savez ce que vous faites, vous pouvez autoriser le lancement du -tirage dans l'interface admin.

+

Soit les inscriptions ne sont en pas encore fermées, soit le lancement du +tirage est désactivé. Si vous savez ce que vous faites, vous pouvez autoriser +le lancement du tirage dans +l'interface admin.

{% endblock %} diff --git a/bda/views.py b/bda/views.py index d9f89b2d..dd53dbd2 100644 --- a/bda/views.py +++ b/bda/views.py @@ -263,7 +263,8 @@ def do_tirage(request, tirage_id): @buro_required def tirage(request, tirage_id): tirage_elt = get_object_or_404(Tirage, id=tirage_id) - if not tirage_elt.enable_do_tirage: + if not (tirage_elt.enable_do_tirage + and tirage_elt.fermeture < timezone.now()): return render(request, "tirage-failed.html", {'tirage': tirage_elt}) if request.POST: form = TokenForm(request.POST) From 92f672d573beb3033a234afd6fad5221ef01c3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 Jul 2016 13:48:51 +0200 Subject: [PATCH 8/8] Utilise `bulk_create` dans le tirage --- bda/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bda/views.py b/bda/views.py index dd53dbd2..df8274c4 100644 --- a/bda/views.py +++ b/bda/views.py @@ -251,10 +251,10 @@ def do_tirage(request, tirage_id): form.cleaned_data['token']) tirage_elt.enable_do_tirage = False tirage_elt.save() - for (show, members, _) in results: - for (member, _, _, _) in members: - attrib = Attribution(spectacle=show, participant=member) - attrib.save() + Attribution.objects.bulk_create([ + Attribution(spectacle=show, participant=member) + for show, members, _ in results + for member, _, _, _ in members]) return render(request, "bda-attrib-extra.html", data) else: return render(request, "bda-attrib.html", data)