Coverage for events/views/station.py: 29%

35 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-11-02 14:35 -0600

1import datetime 

2from rest_framework.decorators import action 

3from rest_framework.exceptions import ValidationError 

4from rest_framework.mixins import CreateModelMixin as Create 

5from rest_framework.response import Response 

6from rest_framework.viewsets import GenericViewSet 

7from app.pusher import PusherClient 

8from ..models import StationCheckIn, Station, Participant 

9 

10 

11class StationViewSet(GenericViewSet, Create): 

12 

13 @action(methods=["PATCH"], detail=True) 

14 def check_in(self, request, pk, *args, **kwargs): 

15 station = Station.objects.get(uuid=pk) 

16 if station.challenge.status != station.challenge.Status.CURRENT: 

17 return Response({ 

18 'reason': "Challenge not active", 

19 'success': False 

20 }) 

21 participant = Participant.objects.get( 

22 user=request.user, event=station.challenge.event 

23 ) 

24 try: 

25 checkin_instance = StationCheckIn.objects.get( 

26 team=participant.team, station=station 

27 ) 

28 except StationCheckIn.DoesNotExist: 

29 return Response({ 

30 'reason': "You're not allowed to scan this qr yet", 

31 'success': False 

32 }) 

33 if checkin_instance.has_checkin: 

34 return Response({ 

35 'reason': "Already Scanned QR", 

36 'success': False 

37 }) 

38 if station.type_of != station.StationCheckInType.QR: 

39 return Response({ 

40 'reason': "This checkin is not allowed with QR", 

41 'success': False 

42 }) 

43 

44 checkin_instance.checkin_timestamp = datetime.datetime.now() 

45 checkin_instance.has_checkin = True 

46 checkin_instance.participant = participant 

47 if station.type_of == station.StationCheckInType.QR: 

48 checkin_instance.approve_timestamp = datetime.datetime.now() 

49 checkin_instance.is_approved = True 

50 checkin_instance.save() 

51 

52 pusher = PusherClient() 

53 pusher.update_challenge_detail(station.challenge) 

54 pusher.update_event_detail(station.challenge.event) 

55 

56 data = { 

57 "success": True, 

58 "station": checkin_instance.station.name, 

59 "random_slug": checkin_instance.random_slug, 

60 "datetime": checkin_instance.checkin_timestamp.strftime( 

61 "%b %d, %Y at %H:%M" 

62 ), 

63 } 

64 

65 return Response(data)