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 @@
-
-
-
- | Rang |
- Nom |
- Classe |
- Temps |
-
-
-
- {% for a in arrivees %}
-
- | {{ a.rang }} |
- {{ a.coureur.nom }} |
- {{ a.coureur.classe }} |
- {% if a.temps %}{{ a.temps|seconds_to_hms }}{% endif %} |
-
- {% empty %}
-
- | Aucun coureur arrivé. |
- |
- |
- |
-
- {% endfor %}
-
-
+
+
+
+
+ | Rang |
+ Nom |
+ Classe |
+ Temps |
+
+
+
+ {% for a in arrivees %}
+
+ | {{ a.rang }} |
+ {{ a.coureur.nom }} |
+ {{ a.coureur.classe }} |
+ {% if a.temps %}{{ a.temps|seconds_to_hms }}{% endif %} |
+
+ {% empty %}
+
+ | Aucun coureur arrivé. |
+ |
+ |
+ |
+
+ {% endfor %}
+
+
+
@@ -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