Coverage for events/views/__init__.py: 43%
75 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-10-27 08:47 -0500
« prev ^ index » next coverage.py v6.4.4, created at 2022-10-27 08:47 -0500
1from .challenge import ChallengeViewSet
2from .checkin import CheckInViewSet
3from .station import StationViewSet
6from random import shuffle
7from rest_framework.decorators import action
8from rest_framework.mixins import (CreateModelMixin as Create, ListModelMixin as List, RetrieveModelMixin as Detail,
9 UpdateModelMixin as Update)
10from rest_framework.permissions import AllowAny
11from rest_framework.response import Response
12from rest_framework.viewsets import GenericViewSet
13from app.mixins import ActionSerializerMixin
14from ..models import Event, Challenge, Participant
15from ..serializers import (EventDisplaySerializer, EventDetailSerializer, SaveScoresSerializer, ParticipantSerializer,
16 TeamSerializer, EventSerializer, ParticipantDetailSerializer)
19class EventViewSet(ActionSerializerMixin, GenericViewSet, List, Detail):
20 serializer_class = EventSerializer
21 action_serializers = {'display': EventDisplaySerializer, 'retrieve': EventDetailSerializer}
23 def get_queryset(self):
24 return Event.objects.filter(participants__user=self.request.user)
26 def get_serializer_context(self):
27 context = super(EventViewSet, self).get_serializer_context()
28 context['user'] = self.request.user
29 return context
31 @action(detail=True, methods=['GET'])
32 def display(self, request, *args, **kwargs):
33 return super(EventViewSet, self).retrieve(request, *args, **kwargs)
35 @action(detail=True, methods=['POST'])
36 def challenges_order(self, request, *args, **kwargs):
37 challenge_list = request.data.get('challenges')
38 order = 1
39 for item in challenge_list:
40 challenge = Challenge.objects.get(random_slug=item.get('random_slug'))
41 challenge.order = order
42 challenge.save()
43 order += 1
44 return Response()
46 @action(detail=True, methods=['POST'])
47 def randomize_participants(self, request, pk):
48 event = Event.objects.get(random_slug=pk)
49 teams_count = event.teams.count()
50 teams = list(event.teams.all())
51 participants = list(event.participants.all())
53 shuffle(participants)
55 team_number = 0
56 while len(participants) > 0:
57 participant = participants.pop()
58 participant.team = teams[team_number % teams_count]
59 participant.save()
60 team_number += 1
62 serializer = TeamSerializer(event.teams.all(), many=True)
64 return Response(serializer.data)
67class ScoresViewSet(GenericViewSet):
68 def create(self, request):
69 serializer = SaveScoresSerializer(data=request.data)
70 serializer.is_valid(True)
71 serializer.save()
72 challenge = Challenge.objects.get(random_slug=request.data.get('challenge'))
73 scores = challenge.scores.order_by('position')
74 max_position = max([s.position for s in scores] + [0])
75 positions = []
76 for i in range(max_position):
77 positions.append(None)
78 for s in scores:
79 positions[s.position - 1] = s.score
80 return Response(positions)
83class ParticipantViewSet(ActionSerializerMixin, GenericViewSet, Create, Detail, Update):
84 serializer_class = ParticipantDetailSerializer
85 queryset = Participant.objects.all()
86 action_serializers = {'create': ParticipantSerializer}
88 def get_permissions(self):
89 if self.action in ['partial_update', 'retrieve']:
90 return [AllowAny()]
91 return super(ParticipantViewSet, self).get_permissions()
93 def perform_create(self, serializer):
94 serializer.save(invited_by=self.request.user)
97class TeamViewSet(GenericViewSet, Create):
98 serializer_class = TeamSerializer