Skip to content

Commit

Permalink
Add 0018_reset_button.patch: an administrator will find a button on t…
Browse files Browse the repository at this point in the history
…he homepage next to the race name that allows to do a full reset
  • Loading branch information
francesco-ballarin committed Aug 18, 2024
1 parent 5cb7abf commit 1f80c91
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
183 changes: 183 additions & 0 deletions patches/turing/0018_reset_button.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
diff --git a/engine/context_processors.py b/engine/context_processors.py
index ab617b5..a9a719c 100644
--- a/engine/context_processors.py
+++ b/engine/context_processors.py
@@ -5,7 +5,7 @@ from engine.models import Gara


def get_gare():
- gare = Gara.objects.filter(inizio__isnull=False)
+ gare = Gara.objects.filter(inizio__isnull=False).order_by("-inizio", "-id")
loraesatta = timezone.now()
attive = []
archivio = []
@@ -14,7 +14,7 @@ def get_gare():
archivio.append(g)
else:
attive.append(g)
- da_iniziare = Gara.objects.filter(inizio__isnull=True)
+ da_iniziare = Gara.objects.filter(inizio__isnull=True).order_by("-id")
return attive, archivio, da_iniziare


diff --git a/engine/templates/gara/reset.html b/engine/templates/gara/reset.html
new file mode 100644
index 0000000..72fc6ed
--- /dev/null
+++ b/engine/templates/gara/reset.html
@@ -0,0 +1,16 @@
+{% extends "basegara.html" %}
+{% block title %}{{object.nome}} reset{% endblock %}
+
+{% block content %}
+<h3>Gara: <a href="{% url 'engine:gara-detail' object.pk %}">{{ object.nome }}</a></h3>
+
+
+<p>
+Questa azione cancellerà tutte le consegne, tutti i jolly e tutti i bonus registrati nella gara, e ne azzererà la data ed ora di inizio. Sei sicuro di voler procedere?<br/>
+<form method="post">{% csrf_token %}
+ <input type="hidden" name="reset" value=1>
+ <input type="submit" value="Sì, procedi al reset della gara!" id="submit">
+</form>
+</p>
+
+{% endblock %}
diff --git a/engine/templates/index.html b/engine/templates/index.html
index fa86faa..0a8c812 100644
--- a/engine/templates/index.html
+++ b/engine/templates/index.html
@@ -21,6 +21,10 @@
<th>Testo</th>
<th>Inizio</th>
<th>Fine</th>
+ {% if user.is_staff %}
+ <th>Download</th>
+ <th>Reset</th>
+ {% endif %}
</tr>
</thead>
<tbody>
@@ -31,6 +35,10 @@
<td>{% if gara.testo %}<a href="{{gara.testo}}" target=_blank><i class="fas fa-file-pdf"></i></a>{% else %}<i class="fas fa-ban"></i>{% endif %}</td>
<td>{{gara.inizio}}</td>
<td>{{gara.get_ora_fine}}</td>
+ {% if user.is_staff %}
+ <td><a href="{% url 'engine:gara-download' gara.pk%}" target=_blank><i class="fas fa-download"></i></a></td>
+ <td><a href="{% url 'engine:gara-reset' gara.pk%}" target=_blank><i class="fas fa-undo"></i></a></td>
+ {% endif %}
</tr>
{% endfor %}
</tbody>
@@ -58,6 +66,10 @@
<tr>
<th>Id</th>
<th>Nome</th>
+ {% if user.is_staff %}
+ <th>Download</th>
+ <th>Reset</th>
+ {% endif %}
</tr>
</thead>
<tbody>
@@ -65,6 +77,10 @@
<tr>
<td>{{gara.pk}}</td>
<td><a href="{% url 'engine:gara-detail' gara.pk%}">{{gara.nome}}</a></td>
+ {% if user.is_staff %}
+ <td><a href="{% url 'engine:gara-download' gara.pk%}" target=_blank><i class="fas fa-download"></i></a></td>
+ <td><a href="{% url 'engine:gara-reset' gara.pk%}" target=_blank><i class="fas fa-undo"></i></a></td>
+ {% endif %}
</tr>
{% endfor %}
</tbody>
@@ -93,7 +109,10 @@
<th>Nome</th>
<th>Inizio</th>
<th>Fine</th>
+ {% if user.is_staff %}
<th>Download</th>
+ <th>Reset</th>
+ {% endif %}
</tr>
</thead>
<tbody>
@@ -103,7 +122,10 @@
<td><a href="{% url 'engine:gara-detail' gara.pk%}">{{gara.nome}}</a></td>
<td>{{gara.inizio}}</td>
<td>{{gara.get_ora_fine}}</td>
+ {% if user.is_staff %}
<td><a href="{% url 'engine:gara-download' gara.pk%}" target=_blank><i class="fas fa-download"></i></a></td>
+ <td><a href="{% url 'engine:gara-reset' gara.pk%}" target=_blank><i class="fas fa-undo"></i></a></td>
+ {% endif %}
</tr>
{% endfor %}
</tbody>
diff --git a/engine/urls.py b/engine/urls.py
index 1c91adc..f49cf45 100644
--- a/engine/urls.py
+++ b/engine/urls.py
@@ -17,6 +17,7 @@ urlpatterns = [
path('gara/<int:pk>/risposte', GaraRisposteView.as_view(), name='gara-risposte'),
path('gara/<int:pk>/squadre', GaraSquadreView.as_view(), name='gara-squadre'),
path('gara/<int:pk>/download', DownloadGaraView.as_view(), name='gara-download'),
+ path('gara/<int:pk>/reset', GaraResetView.as_view(), name='gara-reset'),
path('query/<int:pk>', QueryView.as_view(), name='query'),
path('inserisci/<int:pk>', InserimentoView.as_view(), name='inserimento'),
path('evento/<int:pk>/modifica', ModificaEventoView.as_view(), name='evento-modifica'),
diff --git a/engine/views.py b/engine/views.py
index 1e3cf2e..39ba350 100644
--- a/engine/views.py
+++ b/engine/views.py
@@ -9,6 +9,7 @@ from django.http import JsonResponse, HttpResponse
from django.utils import timezone
from django.db import transaction
from django.contrib.auth import login, authenticate
+from django import forms

from engine.models import User, Gara, Soluzione, Squadra, Evento, Consegna, Jolly, Bonus
from engine.forms import SignUpForm, RispostaFormset, SquadraFormset, InserimentoForm,\
@@ -272,15 +273,41 @@ class DownloadGaraView(DetailView):

def get(self, request, *args, **kwargs):
super().get(request, *args, **kwargs)
- loraesatta = timezone.now()
- if self.object.get_ora_fine() >= loraesatta:
- return HttpResponse('Non puoi scaricare una gara in corso', status=404)
data = self.object.dump_to_json()
response = HttpResponse(data, content_type="application/json")
response['Content-Disposition'] = 'attachment; filename={}.json'.format(self.object.nome)
return response


+class GaraResetView(CheckPermissionsMixin, SuccessMessageMixin, FormView, DetailView):
+ """ View per il reset di una gara """
+ model = Gara
+ form_class = forms.Form
+ template_name = "gara/reset.html"
+ success_message = 'Reset gara avvenuto con successo!'
+
+ def test_func(self):
+ # Salva la gara dentro self.object, così siamo sicuri
+ # che sia stata validata e non viene chiamato get_object() due volte.
+ self.object = self.get_object()
+ return self.request.user.can_administrate(self.object)
+
+ def form_valid(self, form):
+ gara = self.object
+ Jolly.objects.filter(gara=gara).delete()
+ Jolly.history.filter(gara=gara).delete()
+ Consegna.objects.filter(gara=gara).delete()
+ Consegna.history.filter(gara=gara).delete()
+ Bonus.objects.filter(gara=gara).delete()
+ Bonus.history.filter(gara=gara).delete()
+ gara.inizio = None
+ gara.save()
+ return super().form_valid(form)
+
+ def get_success_url(self, **kwargs):
+ return reverse("engine:gara-admin", kwargs={'pk': self.object.pk})
+
+
#######################################
# VIEW QUERY #
#######################################
1 change: 1 addition & 0 deletions patches/turing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ This directory contains a few local patches. Most of them are related to changes
15. `0015_manual_bonus.patch`: add feature to assign (positive or negative) bonus to a team. As a side effect, for simplicity of the implementation of the new feature the commentary has been disabled.
16. `0016_improve_admin_panel.patch`: improve admin panel by enabling filtering on relevant model fields.
17. `0017_clarify_insertion_form_error.patch`: clarify error message in insertion form, explaining where the error details are.
18. `0018_reset_button.patch`: administrator can now fully reset a race from the homepage.
1 change: 1 addition & 0 deletions patches/turing/apply_patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ else
apply_patch 0015_manual_bonus.patch
apply_patch 0016_improve_admin_panel.patch
apply_patch 0017_clarify_insertion_form_error.patch
apply_patch 0018_reset_button.patch
fi

0 comments on commit 1f80c91

Please sign in to comment.