Coverage for events/views/challenge.py: 29%
64 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-11-02 14:35 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2022-11-02 14:35 -0600
1from random import choice
2from django.db import transaction
3from rest_framework.decorators import action
4from rest_framework.mixins import CreateModelMixin as Create, RetrieveModelMixin as Detail, UpdateModelMixin as Update
5from rest_framework.response import Response
6from rest_framework.viewsets import GenericViewSet
7from app.mixins import ActionSerializerMixin
8from app.pusher import PusherClient
9from app.twilio import TwilioClient
10from ..models import Challenge, StationCheckIn
11from ..serializers import (ChallengeDetailSerializer, ChallengeSerializer, StationCheckInDetailSerializer,
12 SimpleTeamSerializer)
15class ChallengeViewSet(ActionSerializerMixin, GenericViewSet, Detail, Create, Update):
16 action_serializers = {'retrieve': ChallengeDetailSerializer, 'create': ChallengeSerializer}
18 def get_serializer_context(self):
19 context = super(ChallengeViewSet, self).get_serializer_context()
20 context['user'] = self.request.user
21 return context
23 def get_queryset(self):
24 qs = Challenge.objects.filter(event__participants__user=self.request.user)
25 return qs
27 @action(detail=True, methods=['PATCH'])
28 def activate(self, request, *args, **kwargs):
29 challenge = self.get_object()
30 challenge.status = challenge.Status.CURRENT
31 challenge.save()
33 # create stations for teams
34 if challenge.type_of == challenge.Type.SCAVENGER_HUNT:
35 stations_list = list(challenge.stations.all())
36 with transaction.atomic():
37 for team in challenge.event.teams.all():
38 if challenge.scavenger_order == challenge.StationsOrder.UNORDERED:
39 for station in stations_list:
40 StationCheckIn.objects.get_or_create(
41 team=team,
42 station=station,
43 )
44 else:
45 if challenge.scavenger_order == challenge.StationsOrder.DEFAULT:
46 first_station = challenge.stations.get(order=1)
47 else:
48 first_station = choice(stations_list)
49 StationCheckIn.objects.get_or_create(
50 team=team,
51 station=first_station,
52 )
54 members = team.members.all()
55 twilio = TwilioClient()
56 for member in members:
57 if member.user.phone is not None:
58 twilio.send_message(
59 body=f"The next clue is: \n{first_station.hint}",
60 to=member.user.phone.as_e164,
61 )
62 twilio.make_call(
63 message=f"The next clue is, {first_station.hint}",
64 to=member.user.phone.as_e164,
65 )
67 pusher = PusherClient()
68 pusher.update_event_detail(challenge.event)
69 pusher.update_challenge_detail(challenge)
70 return Response()
72 @action(detail=True, methods=['PATCH'])
73 def deactivate(self, request, *args, **kwargs):
74 challenge = self.get_object()
75 challenge.status = challenge.Status.PAST
76 challenge.save()
77 pusher = PusherClient()
78 pusher.update_event_detail(challenge.event)
79 pusher.update_challenge_detail(challenge)
80 return Response()
82 @action(detail=True, methods=['GET'])
83 def checkins(self, request, pk):
84 team_slug = request.query_params.get('team')
85 pending_approval = request.query_params.get('pending_approval', False)
86 challenge = self.get_object()
87 check_ins = StationCheckIn.objects.completed(challenge, team_slug=team_slug, pending_approval=pending_approval)
88 serializer = StationCheckInDetailSerializer(check_ins, many=True)
89 team_serializer = SimpleTeamSerializer(challenge.event.teams.all(), many=True)
90 return Response({
91 'event': challenge.event.name,
92 'challenge': challenge.name,
93 'checkins': serializer.data,
94 'teams': team_serializer.data,
95 })