Browse Source

Gestion de l'eport PDF/CSV en fonction des données affichées

Bug affichage tableau sur petits écr
master
scayac 3 months ago
parent
commit
8b55b2f632
  1. 54
      courses/views.py
  2. 90
      templates/course_detail.html

54
courses/views.py

@ -3,20 +3,30 @@ from django.http import HttpResponse
from reportlab.pdfgen import canvas from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4 from reportlab.lib.pagesizes import A4
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
def export_csv(request, course_id): def export_csv(request, course_id):
course = get_object_or_404(Course, id=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 = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="course_{course_id}_resultats.csv"' response['Content-Disposition'] = f'attachment; filename="course_{course_id}_resultats.csv"'
writer = csv.writer(response) writer = csv.writer(response)
writer.writerow(['Rang', 'Nom', 'Classe', 'Temps']) writer.writerow(['Rang', 'Nom', 'Classe', 'Temps'])
for a in arrivees: import json
writer.writerow([a.rang, a.coureur.nom, a.coureur.classe, str(a.temps)]) 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 return response
def export_pdf(request, course_id): def export_pdf(request, course_id):
course = get_object_or_404(Course, id=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 = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="course_{course_id}_resultats.pdf"' response['Content-Disposition'] = f'attachment; filename="course_{course_id}_resultats.pdf"'
p = canvas.Canvas(response, pagesize=A4) p = canvas.Canvas(response, pagesize=A4)
@ -31,15 +41,33 @@ def export_pdf(request, course_id):
p.drawString(300, y, "Classe") p.drawString(300, y, "Classe")
p.drawString(400, y, "Temps") p.drawString(400, y, "Temps")
y -= 20 y -= 20
for a in arrivees: import json
p.drawString(50, y, str(a.rang)) rows_json = request.POST.get('rows')
p.drawString(100, y, a.coureur.nom) if request.method == "POST" and rows_json:
p.drawString(300, y, a.coureur.classe) try:
p.drawString(400, y, str(a.temps)) rows = json.loads(rows_json)
y -= 20 for row in rows:
if y < 50: p.drawString(50, y, str(row[0]))
p.showPage() p.drawString(100, y, str(row[1]))
y = height - 50 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() p.save()
return response return response

90
templates/course_detail.html

@ -54,42 +54,52 @@
<div class="card-header py-3 d-flex justify-content-between align-items-center"> <div class="card-header py-3 d-flex justify-content-between align-items-center">
<h6 class="m-0 font-weight-bold text-primary">Arrivées</h6> <h6 class="m-0 font-weight-bold text-primary">Arrivées</h6>
<div> <div>
<a href="{% url 'export_csv' course.id %}" class="btn btn-success mb-2"> <form id="exportCsvForm" method="post" action="{% url 'export_csv' course.id %}" style="display:inline;">
<i class="fas fa-file-csv" title="Exporter en CSV"></i> {% csrf_token %}
</a> <input type="hidden" name="rows" id="csvRowsInput">
<a href="{% url 'export_pdf' course.id %}" class="btn btn-danger mb-2"> <button type="submit" class="btn btn-success mb-2" id="btnExportCsv">
<i class="fas fa-file-pdf" title="Exporter en PDF"></i> <i class="fas fa-file-csv" title="Exporter en CSV"></i>
</a> </button>
</form>
<form id="exportPdfForm" method="post" action="{% url 'export_pdf' course.id %}" style="display:inline;">
{% csrf_token %}
<input type="hidden" name="rows" id="pdfRowsInput">
<button type="submit" class="btn btn-danger mb-2" id="btnExportPdf">
<i class="fas fa-file-pdf" title="Exporter en PDF"></i>
</button>
</form>
</div> </div>
</div> </div>
<div class="card-body"> <div class="card-body">
<table class="table table-striped" id="arriveesTable"> <div class="table-responsive">
<thead> <table class="table table-striped" id="arriveesTable">
<tr> <thead>
<th>Rang</th> <tr>
<th>Nom</th> <th>Rang</th>
<th>Classe</th> <th>Nom</th>
<th>Temps</th> <th>Classe</th>
</tr> <th>Temps</th>
</thead> </tr>
<tbody> </thead>
{% for a in arrivees %} <tbody>
<tr> {% for a in arrivees %}
<td>{{ a.rang }}</td> <tr>
<td>{{ a.coureur.nom }}</td> <td>{{ a.rang }}</td>
<td>{{ a.coureur.classe }}</td> <td>{{ a.coureur.nom }}</td>
<td>{% if a.temps %}{{ a.temps|seconds_to_hms }}{% endif %}</td> <td>{{ a.coureur.classe }}</td>
</tr> <td>{% if a.temps %}{{ a.temps|seconds_to_hms }}{% endif %}</td>
{% empty %} </tr>
<tr> {% empty %}
<td>Aucun coureur arrivé.</td> <tr>
<td></td> <td>Aucun coureur arrivé.</td>
<td></td> <td></td>
<td></td> <td></td>
</tr> <td></td>
{% endfor %} </tr>
</tbody> {% endfor %}
</table> </tbody>
</table>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -140,5 +150,19 @@ document.getElementById('btnFinish').onclick = function() {
$(document).ready(function() { $(document).ready(function() {
$('#arriveesTable').DataTable(); $('#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());
});
</script> </script>
{% endblock %} {% endblock %}
Loading…
Cancel
Save