From 33bc3c588280de7c3870449f5966a737ac5b2ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 5 Oct 2019 15:58:11 +0200 Subject: [PATCH] Events: simple csv participants export --- cof/urls.py | 6 ++++++ events/urls.py | 11 +++++++++++ events/views.py | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 events/urls.py diff --git a/cof/urls.py b/cof/urls.py index 82d047e7..1baa2a8e 100644 --- a/cof/urls.py +++ b/cof/urls.py @@ -123,6 +123,12 @@ urlpatterns = [ path("config", gestioncof_views.ConfigUpdate.as_view(), name="config.edit"), ] +if "events" in settings.INSTALLED_APPS: + # The new event application is still in development + # → for now it is namespaced below events_v2 + # → when the old events system is out, move this above in the others apps + urlpatterns += [path("event_v2/", include("events.urls"))] + if "debug_toolbar" in settings.INSTALLED_APPS: import debug_toolbar diff --git a/events/urls.py b/events/urls.py new file mode 100644 index 00000000..84054448 --- /dev/null +++ b/events/urls.py @@ -0,0 +1,11 @@ +from django.urls import path +from events import views + +app_name = "events" +urlpatterns = [ + path( + "csv/participants/", + views.participants_csv, + name="csv-participants", + ) +] diff --git a/events/views.py b/events/views.py index e69de29b..22fe6e50 100644 --- a/events/views.py +++ b/events/views.py @@ -0,0 +1,24 @@ +import csv + +from django.contrib.auth.decorators import login_required, permission_required +from django.http import HttpResponse +from django.shortcuts import get_object_or_404 +from django.utils.text import slugify +from events.models import Event + + +@login_required +@permission_required("events.view_event") +def participants_csv(request, event_id): + event = get_object_or_404(Event, id=event_id) + + filename = "{}-participants.csv".format(slugify(event.title)) + response = HttpResponse(content_type="text/csv") + response["Content-Disposition"] = 'attachment; filename="{}"'.format(filename) + + writer = csv.writer(response) + writer.writerow(["username", "email", "prénom", "nom de famille"]) + for user in event.subscribers.all(): + writer.writerow([user.username, user.email, user.first_name, user.last_name]) + + return response