gestioCOF/events/views.py
2019-10-08 22:26:30 +02:00

24 lines
854 B
Python

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