|
| 1 | +import csv |
| 2 | + |
1 | 3 | from django.db import transaction |
2 | 4 | from django.db.models import Count, Prefetch, Q |
3 | | -from django.http import Http404 |
| 5 | +from django.http import Http404, HttpResponse |
4 | 6 | from django.shortcuts import get_object_or_404 |
5 | 7 | from django.utils import timezone |
6 | 8 | from django_filters.rest_framework import DjangoFilterBackend |
@@ -203,6 +205,30 @@ def perform_update(self, serializer): |
203 | 205 | """ |
204 | 206 | serializer.save(is_ready=False) |
205 | 207 |
|
| 208 | + @decorators.action(detail=True, methods=["GET"]) |
| 209 | + def csv(self, *args, **kwargs): |
| 210 | + event = Event.objects.get(pk=kwargs["pk"]) |
| 211 | + response = HttpResponse(content_type="text/csv") |
| 212 | + response["Content-Disposition"] = ( |
| 213 | + f'attachment; filename="{event.title.replace(" ", "_") + "_attendees"}.csv"' |
| 214 | + ) |
| 215 | + |
| 216 | + writer = csv.writer(response) |
| 217 | + writer.writerow(["full_name", "username", "email", "phone", "grade"]) |
| 218 | + for pool in event.pools.all(): |
| 219 | + for participant in pool.registrations.all(): |
| 220 | + writer.writerow( |
| 221 | + [ |
| 222 | + participant.user.get_full_name(), |
| 223 | + participant.user.username, |
| 224 | + participant.user.email, |
| 225 | + participant.user.phone_number, |
| 226 | + participant.user.grade, |
| 227 | + ] |
| 228 | + ) |
| 229 | + |
| 230 | + return response |
| 231 | + |
206 | 232 | @decorators.action(detail=True, methods=["GET"]) |
207 | 233 | def administrate(self, request, *args, **kwargs): |
208 | 234 | event_id = self.kwargs.get("pk", None) |
@@ -334,8 +360,8 @@ def cover_image_gallery(self, request): |
334 | 360 | ) |
335 | 361 | serializer = self.get_serializer(queryset, many=True) |
336 | 362 | return Response(serializer.data) |
337 | | - |
338 | | - |
| 363 | + |
| 364 | + |
339 | 365 | class PoolViewSet( |
340 | 366 | mixins.CreateModelMixin, |
341 | 367 | mixins.UpdateModelMixin, |
|
0 commit comments