From 2f4e40d37f217ab6d146e770e5aa272e64b78df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjam=C3=ADn=20Mravec?= Date: Fri, 10 Nov 2023 19:46:41 +0100 Subject: [PATCH 1/6] Added csv option --- competition/views.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/competition/views.py b/competition/views.py index 676d8d41..8466211a 100644 --- a/competition/views.py +++ b/competition/views.py @@ -1,5 +1,6 @@ import os import zipfile +import csv from io import BytesIO from operator import itemgetter @@ -7,6 +8,7 @@ from django.core.mail import send_mail from django.http import FileResponse, HttpResponse from django.template.loader import render_to_string +from django.views.generic import View from rest_framework import exceptions, mixins, status, viewsets from rest_framework.decorators import action from rest_framework.exceptions import ValidationError @@ -688,6 +690,21 @@ def participants(self, request, pk=None): profiles = Profile.objects.only("user").filter(pk__in=participants_id) serializer = ProfileExportSerializer(profiles, many=True) + + #if request == 'csv': + + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="export.csv"' + + header = ProfileExportSerializer.Meta.fields + + writer = csv.DictWriter(response, fieldnames=header) + writer.writeheader() + for row in serializer.data: + writer.writerow(row) + + return response + return Response(serializer.data) def post(self, request, format_post): From 31eae289cf4150a61f09b5eed1f56d5778d0c499 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 10 Nov 2023 22:19:50 +0100 Subject: [PATCH 2/6] Divide json and csv endpoints --- competition/views.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/competition/views.py b/competition/views.py index 8466211a..c9ac4ea5 100644 --- a/competition/views.py +++ b/competition/views.py @@ -1,6 +1,6 @@ +import csv import os import zipfile -import csv from io import BytesIO from operator import itemgetter @@ -14,6 +14,8 @@ from rest_framework.exceptions import ValidationError from rest_framework.permissions import IsAdminUser, IsAuthenticated from rest_framework.response import Response +from rest_framework.settings import api_settings +from rest_framework_csv.renderers import CSVRenderer from base.utils import mime_type from competition import utils @@ -558,6 +560,8 @@ class SemesterViewSet(ModelViewSetWithSerializerContext): permission_classes = (CompetitionRestrictedPermission,) filterset_fields = ['competition'] http_method_names = ['get', 'post', 'head'] + renderer_classes = (CSVRenderer, ) + \ + tuple(api_settings.DEFAULT_RENDERER_CLASSES) def perform_create(self, serializer): """ @@ -673,10 +677,7 @@ def current_results(self, request, competition_id=None): current_results = SemesterViewSet.semester_results(current_semester) return Response(current_results, status=status.HTTP_201_CREATED) - @action(methods=['get'], detail=True) - def participants(self, request, pk=None): - """Vráti všetkých užívateľov zapojených do semestra""" - semester = self.get_object() + def __get_participants(self, semester): participants_id = [] for series in semester.series_set.all(): @@ -690,23 +691,29 @@ def participants(self, request, pk=None): profiles = Profile.objects.only("user").filter(pk__in=participants_id) serializer = ProfileExportSerializer(profiles, many=True) + return serializer + + @action(methods=['get'], detail=True) + def participants(self, request, pk=None): + """Vráti všetkých užívateľov zapojených do semestra""" + semester = self.get_object() + serializer = self.__get_participants(semester) + return Response(serializer.data) - #if request == 'csv': - + @action(methods=['get'], detail=True, url_path='participants-export') + def participants_export(self, request, pk=None): + """Vráti všetkých užívateľov zapojených do semestra""" + semester = self.get_object() + serializer = self.__get_participants(semester) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="export.csv"' - header = ProfileExportSerializer.Meta.fields - writer = csv.DictWriter(response, fieldnames=header) writer.writeheader() for row in serializer.data: writer.writerow(row) - return response - return Response(serializer.data) - def post(self, request, format_post): """Založí nový semester""" serializer = SemesterSerializer(data=request.data) From fe4b676924ea14e828f782b64a3c10b5d927f9f5 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 10 Nov 2023 22:23:21 +0100 Subject: [PATCH 3/6] Get semester in __get_participants --- competition/views.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/competition/views.py b/competition/views.py index c9ac4ea5..79e3dc2a 100644 --- a/competition/views.py +++ b/competition/views.py @@ -677,7 +677,8 @@ def current_results(self, request, competition_id=None): current_results = SemesterViewSet.semester_results(current_semester) return Response(current_results, status=status.HTTP_201_CREATED) - def __get_participants(self, semester): + def __get_participants(self): + semester = self.get_object() participants_id = [] for series in semester.series_set.all(): @@ -696,15 +697,13 @@ def __get_participants(self, semester): @action(methods=['get'], detail=True) def participants(self, request, pk=None): """Vráti všetkých užívateľov zapojených do semestra""" - semester = self.get_object() - serializer = self.__get_participants(semester) + serializer = self.__get_participants() return Response(serializer.data) @action(methods=['get'], detail=True, url_path='participants-export') def participants_export(self, request, pk=None): """Vráti všetkých užívateľov zapojených do semestra""" - semester = self.get_object() - serializer = self.__get_participants(semester) + serializer = self.__get_participants() response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="export.csv"' header = ProfileExportSerializer.Meta.fields From 583e5075ed0ba1664e4b9d3a5c17f384c40f1970 Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 10 Nov 2023 22:25:16 +0100 Subject: [PATCH 4/6] Remove CSVrenderer --- competition/views.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/competition/views.py b/competition/views.py index 79e3dc2a..03d7dc73 100644 --- a/competition/views.py +++ b/competition/views.py @@ -14,8 +14,6 @@ from rest_framework.exceptions import ValidationError from rest_framework.permissions import IsAdminUser, IsAuthenticated from rest_framework.response import Response -from rest_framework.settings import api_settings -from rest_framework_csv.renderers import CSVRenderer from base.utils import mime_type from competition import utils @@ -560,8 +558,6 @@ class SemesterViewSet(ModelViewSetWithSerializerContext): permission_classes = (CompetitionRestrictedPermission,) filterset_fields = ['competition'] http_method_names = ['get', 'post', 'head'] - renderer_classes = (CSVRenderer, ) + \ - tuple(api_settings.DEFAULT_RENDERER_CLASSES) def perform_create(self, serializer): """ From e61992240ae7b94de84ba0e8786fc50ef224577e Mon Sep 17 00:00:00 2001 From: kovacspe Date: Fri, 10 Nov 2023 22:27:19 +0100 Subject: [PATCH 5/6] lint --- competition/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/competition/views.py b/competition/views.py index 03d7dc73..e4e35c55 100644 --- a/competition/views.py +++ b/competition/views.py @@ -8,7 +8,6 @@ from django.core.mail import send_mail from django.http import FileResponse, HttpResponse from django.template.loader import render_to_string -from django.views.generic import View from rest_framework import exceptions, mixins, status, viewsets from rest_framework.decorators import action from rest_framework.exceptions import ValidationError From ed8031a2378544b577b8c0f3df66114f6180538f Mon Sep 17 00:00:00 2001 From: kovacspe Date: Sat, 11 Nov 2023 11:54:09 +0100 Subject: [PATCH 6/6] Use writerows --- competition/views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/competition/views.py b/competition/views.py index e4e35c55..9420ba66 100644 --- a/competition/views.py +++ b/competition/views.py @@ -704,8 +704,7 @@ def participants_export(self, request, pk=None): header = ProfileExportSerializer.Meta.fields writer = csv.DictWriter(response, fieldnames=header) writer.writeheader() - for row in serializer.data: - writer.writerow(row) + writer.writerows(serializer.data) return response def post(self, request, format_post):