Skip to content

Commit

Permalink
Merge pull request #228 from ZdruzenieSTROM/Profile-export-serializer
Browse files Browse the repository at this point in the history
Added csv option
  • Loading branch information
kovacspe authored Nov 11, 2023
2 parents 02320e0 + ed8031a commit c249fab
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions competition/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
import os
import zipfile
from io import BytesIO
Expand Down Expand Up @@ -671,9 +672,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"""
def __get_participants(self):
semester = self.get_object()
participants_id = []

Expand All @@ -688,8 +687,26 @@ 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"""
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"""
serializer = self.__get_participants()
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()
writer.writerows(serializer.data)
return response

def post(self, request, format_post):
"""Založí nový semester"""
serializer = SemesterSerializer(data=request.data)
Expand Down

0 comments on commit c249fab

Please sign in to comment.