From 8b55b2f6329e1a177f55c287de40bdcfed5650b5 Mon Sep 17 00:00:00 2001 From: scayac Date: Tue, 30 Sep 2025 18:20:46 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20de=20l'eport=20PDF/CSV=20en=20fonctio?= =?UTF-8?q?n=20des=20donn=C3=A9es=20affich=C3=A9es=20Bug=20affichage=20tab?= =?UTF-8?q?leau=20sur=20petits=20=C3=A9cr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- courses/views.py | 54 ++++++++++++++++------ templates/course_detail.html | 90 +++++++++++++++++++++++------------- 2 files changed, 98 insertions(+), 46 deletions(-) diff --git a/courses/views.py b/courses/views.py index cd2c26c..967c347 100644 --- a/courses/views.py +++ b/courses/views.py @@ -3,20 +3,30 @@ from django.http import HttpResponse from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import A4 from django.contrib.auth.decorators import login_required + def export_csv(request, course_id): course = get_object_or_404(Course, id=course_id) - arrivees = course.arrivees.select_related('coureur').order_by('rang') response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = f'attachment; filename="course_{course_id}_resultats.csv"' writer = csv.writer(response) writer.writerow(['Rang', 'Nom', 'Classe', 'Temps']) - for a in arrivees: - writer.writerow([a.rang, a.coureur.nom, a.coureur.classe, str(a.temps)]) + import json + rows_json = request.POST.get('rows') + if request.method == "POST" and rows_json: + try: + rows = json.loads(rows_json) + for row in rows: + writer.writerow(row) + except Exception: + pass # fallback below if erreur + else: + arrivees = course.arrivees.select_related('coureur').order_by('rang') + for a in arrivees: + writer.writerow([a.rang, a.coureur.nom, a.coureur.classe, str(a.temps)]) return response def export_pdf(request, course_id): course = get_object_or_404(Course, id=course_id) - arrivees = course.arrivees.select_related('coureur').order_by('rang') response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = f'attachment; filename="course_{course_id}_resultats.pdf"' p = canvas.Canvas(response, pagesize=A4) @@ -31,15 +41,33 @@ def export_pdf(request, course_id): p.drawString(300, y, "Classe") p.drawString(400, y, "Temps") y -= 20 - for a in arrivees: - p.drawString(50, y, str(a.rang)) - p.drawString(100, y, a.coureur.nom) - p.drawString(300, y, a.coureur.classe) - p.drawString(400, y, str(a.temps)) - y -= 20 - if y < 50: - p.showPage() - y = height - 50 + import json + rows_json = request.POST.get('rows') + if request.method == "POST" and rows_json: + try: + rows = json.loads(rows_json) + for row in rows: + p.drawString(50, y, str(row[0])) + p.drawString(100, y, str(row[1])) + p.drawString(300, y, str(row[2])) + p.drawString(400, y, str(row[3])) + y -= 20 + if y < 50: + p.showPage() + y = height - 50 + except Exception: + pass # fallback below si erreur + else: + arrivees = course.arrivees.select_related('coureur').order_by('rang') + for a in arrivees: + p.drawString(50, y, str(a.rang)) + p.drawString(100, y, a.coureur.nom) + p.drawString(300, y, a.coureur.classe) + p.drawString(400, y, str(a.temps)) + y -= 20 + if y < 50: + p.showPage() + y = height - 50 p.save() return response diff --git a/templates/course_detail.html b/templates/course_detail.html index 4e103fa..37fdf64 100644 --- a/templates/course_detail.html +++ b/templates/course_detail.html @@ -54,42 +54,52 @@
Arrivées
- - - - - - +
+ {% csrf_token %} + + +
+
+ {% csrf_token %} + + +
- - - - - - - - - - - {% for a in arrivees %} - - - - - - - {% empty %} - - - - - - - {% endfor %} - -
RangNomClasseTemps
{{ a.rang }}{{ a.coureur.nom }}{{ a.coureur.classe }}{% if a.temps %}{{ a.temps|seconds_to_hms }}{% endif %}
Aucun coureur arrivé.
+
+ + + + + + + + + + + {% for a in arrivees %} + + + + + + + {% empty %} + + + + + + + {% endfor %} + +
RangNomClasseTemps
{{ a.rang }}{{ a.coureur.nom }}{{ a.coureur.classe }}{% if a.temps %}{{ a.temps|seconds_to_hms }}{% endif %}
Aucun coureur arrivé.
+
@@ -140,5 +150,19 @@ document.getElementById('btnFinish').onclick = function() { $(document).ready(function() { $('#arriveesTable').DataTable(); }); +// Export CSV/PDF des données filtrées +function getVisibleRows() { + var dt = $('#arriveesTable').DataTable(); + var rows = dt.rows({search: 'applied'}).data().toArray(); + return JSON.stringify(rows); +} + +$('#exportCsvForm').on('submit', function(e) { + $('#csvRowsInput').val(getVisibleRows()); +}); + +$('#exportPdfForm').on('submit', function(e) { + $('#pdfRowsInput').val(getVisibleRows()); +}); {% endblock %} \ No newline at end of file