Skip to content

Commit

Permalink
Divide json and csv endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacspe committed Nov 10, 2023
1 parent 06fbf5c commit 31eae28
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions competition/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import csv
import os
import zipfile
import csv
from io import BytesIO
from operator import itemgetter

Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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():
Expand 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)
Expand Down

0 comments on commit 31eae28

Please sign in to comment.