def create_new_instance_for_recipient(self, recipient_type, recipient_id): if self.model_instance: return AlertScheduleInstance.copy_for_recipient(self.model_instance, recipient_type, recipient_id) else: return AlertScheduleInstance.create_for_recipient( self.schedule, recipient_type, recipient_id, move_to_next_event_not_in_the_past=False, )
def create_new_instance_for_recipient(self, recipient_type, recipient_id): if self.model_instance: return AlertScheduleInstance.copy_for_recipient( self.model_instance, recipient_type, recipient_id) else: return AlertScheduleInstance.create_for_recipient( self.schedule, recipient_type, recipient_id, move_to_next_event_not_in_the_past=False, )
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 make_email_event_for_test(domain, schedule_name, user_ids, utcnow=None): content = EmailContent( subject={'*': 'New messaging API goes live!'}, message={'*': 'Check out the new API.'}, ) schedule = AlertSchedule.create_simple_alert(domain, content) broadcast = ImmediateBroadcast.objects.create( domain=domain, name=schedule_name, schedule=schedule, recipients=[(ScheduleInstance.RECIPIENT_TYPE_MOBILE_WORKER, user_id) for user_id in user_ids], ) for user_id in user_ids: instance = AlertScheduleInstance.create_for_recipient( schedule, ScheduleInstance.RECIPIENT_TYPE_MOBILE_WORKER, user_id, move_to_next_event_not_in_the_past=False, ) instance.send_current_event_content_to_recipients() subevents = {} for event in MessagingEvent.objects.filter(source_id=broadcast.id): for subevent in MessagingSubEvent.objects.filter(parent=event): handle_email_messaging_subevent( { "eventType": "Delivery", "delivery": { "timestamp": "2021-05-27T07:09:42.318Z" } }, subevent.id) subevents[subevent.recipient_id] = subevent return subevents
def migrate_schedule_instances(self): if not isinstance(self.schedule, AlertSchedule): raise TypeError("Expected AlertSchedule") for recipient in self.broadcast.recipients: instance = AlertScheduleInstance( domain=self.broadcast.domain, recipient_type=recipient[0], recipient_id=recipient[1], current_event_num=0, schedule_iteration_num=2, next_event_due=self.handler.start_datetime, active=False, alert_schedule_id=self.schedule.schedule_id, ) instance.save(force_insert=True)
def make_alert_schedule_instance(cls, schedule_instance_id=None, schedule_id=None, active=True): return AlertScheduleInstance( schedule_instance_id=schedule_instance_id or uuid.uuid4(), domain=cls.domain, recipient_type='CommCareUser', recipient_id='user-id', current_event_num=0, schedule_iteration_num=1, next_event_due=datetime(2017, 3, 1), active=active, alert_schedule_id=schedule_id or uuid.uuid4(), )
def test_message_scheduling(self): AlertScheduleInstance( schedule_instance_id=uuid.uuid4(), domain=self.domain_name, recipient_type='CommCareUser', recipient_id=uuid.uuid4().hex, current_event_num=0, schedule_iteration_num=1, next_event_due=datetime(2017, 3, 1), active=True, alert_schedule_id=uuid.uuid4(), ).save() self._dump_and_load({AlertScheduleInstance: 1})
def test_access_restricted(self): with self.assertRaises(AccessRestricted): AlertScheduleInstance().save() with self.assertRaises(AccessRestricted): AlertScheduleInstance().delete() with self.assertRaises(AccessRestricted): [obj for obj in AlertScheduleInstance.objects.all()] with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.all()[0] with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.all()[0:10] with self.assertRaises(AccessRestricted): len(AlertScheduleInstance.objects.all()) with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.count() with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.filter(schedule_instance_id=None) with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.exclude(schedule_instance_id=None) with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.get(schedule_instance_id=None) with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.create() with self.assertRaises(AccessRestricted): AlertScheduleInstance.objects.get_or_create( schedule_instance_id=None)