def test_alert(self, utcnow_patch, send_patch): self.assertNumInstancesForSchedule(0) # Schedule the alert utcnow_patch.return_value = datetime(2017, 3, 16, 6, 42, 21) refresh_alert_schedule_instances( self.schedule, (('CommCareUser', self.user1.get_id), )) self.assertNumInstancesForSchedule(1) [instance] = get_alert_schedule_instances_for_schedule(self.schedule) self.assertAlertScheduleInstance(instance, 0, 1, datetime(2017, 3, 16, 6, 42, 21), True, self.user1) self.assertEqual(send_patch.call_count, 0) # Send first event instance.handle_current_event() save_alert_schedule_instance(instance) self.assertNumInstancesForSchedule(1) self.assertAlertScheduleInstance(instance, 0, 2, datetime(2017, 3, 16, 6, 42, 21), False, self.user1) self.assertEqual(send_patch.call_count, 1) # Once an alert has been sent, we don't allow scheduling new instances of old alerts refresh_alert_schedule_instances( self.schedule, (('CommCareUser', self.user2.get_id), )) self.assertNumInstancesForSchedule(1) [instance] = get_alert_schedule_instances_for_schedule(self.schedule) self.assertAlertScheduleInstance(instance, 0, 2, datetime(2017, 3, 16, 6, 42, 21), False, self.user1) self.assertEqual(send_patch.call_count, 1)
def refresh_alert_schedule_instances(schedule, recipients): """ :param schedule: the AlertSchedule :param recipients: a list of (recipient_type, recipient_id) tuples; the recipient type should be one of the values checked in ScheduleInstance.recipient """ existing_instances = { (instance.recipient_type, instance.recipient_id): instance for instance in get_alert_schedule_instances_for_schedule(schedule) } if existing_instances: # Don't refresh AlertSchedules that have already been sent # to avoid sending old alerts to new recipients return recipients = set(convert_to_tuple_of_tuples(recipients)) for recipient_type, recipient_id in recipients: instance = AlertScheduleInstance.create_for_recipient( schedule, recipient_type, recipient_id, move_to_next_event_not_in_the_past=False, ) save_alert_schedule_instance(instance)
def message_form(self): if self.request.method == 'POST': return MessageForm(self.request.POST, **self.form_kwargs) broadcast = self.broadcast schedule = broadcast.schedule schedule_instances = get_alert_schedule_instances_for_schedule(schedule) recipients = [ (instance.recipient_type, instance.recipient_id) for instance in schedule_instances ] ret = [] if recipients: for doc_type, doc_id in recipients: user = CommCareUser.wrap(CommCareUser.get_db().get(doc_id)) ret.append({"id": doc_id, "text": user.raw_username}) initial = { 'schedule_name': broadcast.name, 'send_frequency': 'immediately', 'recipients': ret, 'content': 'sms', # only works for SMS 'message': schedule.memoized_events[0].content.message, } return MessageForm(initial=initial, **self.form_kwargs)
def refresh_alert_schedule_instances(schedule_id, recipients): """ :param schedule_id: the AlertSchedule schedule_id :param recipients: a list of (recipient_type, recipient_id) tuples; the recipient type should be one of the values checked in ScheduleInstance.recipient """ with CriticalSection( ['refresh-alert-schedule-instances-for-%s' % schedule_id.hex], timeout=5 * 60): schedule = AlertSchedule.objects.get(schedule_id=schedule_id) AlertScheduleInstanceRefresher( schedule, recipients, get_alert_schedule_instances_for_schedule(schedule)).refresh()
def refresh_alert_schedule_instances(schedule_id, recipients): """ :param schedule_id: the AlertSchedule schedule_id :param recipients: a list of (recipient_type, recipient_id) tuples; the recipient type should be one of the values checked in ScheduleInstance.recipient """ with CriticalSection(['refresh-alert-schedule-instances-for-%s' % schedule_id.hex], timeout=5 * 60): schedule = AlertSchedule.objects.get(schedule_id=schedule_id) AlertScheduleInstanceRefresher( schedule, recipients, get_alert_schedule_instances_for_schedule(schedule) ).refresh()
def test_get_alert_schedule_instances_for_schedule(self): self.assertItemsEqual( get_alert_schedule_instances_for_schedule( AlertSchedule(schedule_id=self.schedule_id1)), [self.alert_instance2_p2, self.alert_instance3_p1])
def assertNumInstancesForSchedule(self, num): self.assertEqual(len(list(get_alert_schedule_instances_for_schedule(self.schedule))), num)
def tearDown(self): for instance in get_alert_schedule_instances_for_schedule(self.schedule): delete_alert_schedule_instance(instance)
def test_get_alert_schedule_instances_for_schedule(self): self.assertItemsEqual( get_alert_schedule_instances_for_schedule(AlertSchedule(schedule_id=self.schedule_id1)), [self.alert_instance2_p2, self.alert_instance3_p1] )
def test_get_alert_schedule_instances_for_schedule(self): self.assertEqual( set( get_alert_schedule_instances_for_schedule( AlertSchedule(schedule_id=self.schedule_id1))), set([self.alert_instance2, self.alert_instance3]))