|
|
|
@ -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 |
|
|
|
|
|
|
|
|
|
|
|
|