Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added csv option #228

Merged
merged 7 commits into from
Nov 11, 2023
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 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,27 @@ 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()
for row in serializer.data:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tu by sa dalo použiť writerows miesto for loopu

writer.writerow(row)
return response

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