Coverage for events/signals.py: 19%

59 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-10-26 14:43 -0500

1from random import choice 

2from django.dispatch import receiver 

3from django.db.models.signals import pre_save, post_save 

4from app.pusher import PusherClient 

5from app.twilio import TwilioClient 

6from .models import Challenge, StationCheckIn, Participant, Score 

7from .services import send_rsvp_email 

8from .utils import scavenger_hunt_order_teams 

9 

10 

11@receiver(pre_save, sender=Challenge) 

12def send_challenge_finished_notification(sender, instance, **kwargs): 

13 

14 try: 

15 sender.objects.get(pk=instance.pk) 

16 except sender.DoesNotExist: 

17 return 

18 

19 previous = Challenge.objects.get(pk=instance.pk) 

20 if ( 

21 previous.status == sender.Status.CURRENT 

22 and instance.status == sender.Status.PAST 

23 ): 

24 pusher = PusherClient() 

25 pusher.trigger( 

26 instance.event.random_slug, 

27 "challenge-finished", 

28 {"message": instance.random_slug}, 

29 ) 

30 

31 

32@receiver(post_save, sender=Participant) 

33def send_rsvp_email_signal(sender, instance, created, **kwargs): 

34 if created: 

35 send_rsvp_email(instance) 

36 

37 

38@receiver(post_save, sender=StationCheckIn) 

39def set_scavenger_hunt_score(sender, instance, created, **kwargs): 

40 if instance.is_approved: 

41 challenge = instance.station.challenge 

42 order = scavenger_hunt_order_teams(instance) 

43 

44 position = 1 

45 score = challenge.event.teams.count() 

46 

47 for team in order: 

48 Score.objects.update_or_create( 

49 team=team, 

50 challenge=challenge, 

51 defaults={"position": position, "score": score}, 

52 ) 

53 position += 1 

54 score -= 1 

55 PusherClient().update_challenge_detail(challenge) 

56 PusherClient().update_event_detail(challenge.event) 

57 

58 

59@receiver(post_save, sender=StationCheckIn) 

60def unlock_next_station_and_send_sms_and_call(sender, instance, created, **kwargs): 

61 if instance.is_approved: 

62 current_station = instance.station 

63 challenge = current_station.challenge 

64 next_station = None 

65 if challenge.scavenger_order in [ 

66 challenge.StationsOrder.STAGGERED, 

67 challenge.StationsOrder.DEFAULT, 

68 ]: 

69 next_station_order = ( 

70 current_station.order % challenge.stations.count() 

71 ) + 1 

72 next_station = challenge.stations.get(order=next_station_order) 

73 StationCheckIn.objects.get_or_create( 

74 station=next_station, team=instance.participant.team 

75 ) 

76 elif challenge.scavenger_order == challenge.StationsOrder.RANDOM: 

77 choices = challenge.stations.exclude( 

78 check_ins__team=instance.participant.team 

79 ) 

80 if choices: 

81 next_station = choice(choices) 

82 StationCheckIn.objects.create( 

83 station=next_station, team=instance.participant.team 

84 ) 

85 

86 if next_station is not None: 

87 next_station_check_in = next_station.check_ins.get(team=instance.team) 

88 

89 if not next_station_check_in.has_checkin: 

90 members = instance.team.members.all() 

91 twilio = TwilioClient() 

92 for member in members: 

93 if member.user.phone is not None: 

94 twilio.send_message( 

95 body=f"The next clue is: \n{next_station.hint}", 

96 to=member.user.phone.as_e164, 

97 ) 

98 twilio.make_call( 

99 message=f"The next clue is, {next_station.hint}", 

100 to=member.user.phone.as_e164, 

101 )