def refresh_timed_schedule_instances(schedule, recipients, start_date=None): """ :param schedule: the TimedSchedule :param start_date: the date to start the TimedSchedule :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_timed_schedule_instances_for_schedule(schedule) } recipients = convert_to_tuple_of_tuples(recipients) new_recipients = set(recipients) for recipient_type, recipient_id in new_recipients: if (recipient_type, recipient_id) not in existing_instances: instance = TimedScheduleInstance.create_for_recipient( schedule, recipient_type, recipient_id, start_date=start_date, move_to_next_event_not_in_the_past=True, ) save_timed_schedule_instance(instance) for key, schedule_instance in existing_instances.iteritems(): if key not in new_recipients: delete_timed_schedule_instance(schedule_instance) elif start_date and start_date != schedule_instance.start_date: schedule_instance.recalculate_schedule(schedule, new_start_date=start_date) save_timed_schedule_instance(schedule_instance)
def delete_instance(instance): if isinstance(instance, AlertScheduleInstance): delete_alert_schedule_instance(instance) elif isinstance(instance, TimedScheduleInstance): delete_timed_schedule_instance(instance) elif isinstance(instance, (CaseAlertScheduleInstance, CaseTimedScheduleInstance)): delete_case_schedule_instance(instance) else: raise TypeError("Unexpected type: %s" % type(instance))
def test_delete_timed_schedule_instance(self): self.assertEqual(AlertScheduleInstance.objects.using(self.db).count(), 3) self.assertEqual(TimedScheduleInstance.objects.using(self.db).count(), 3) delete_timed_schedule_instance(self.timed_instance1) self.assertEqual(AlertScheduleInstance.objects.using(self.db).count(), 3) self.assertEqual(TimedScheduleInstance.objects.using(self.db).count(), 2) with self.assertRaises(TimedScheduleInstance.DoesNotExist): get_timed_schedule_instance(self.uuid4)
def test_delete_timed_schedule_instance(self): self.assertEqual(AlertScheduleInstance.objects.using(self.db).count(), 3) self.assertEqual(TimedScheduleInstance.objects.using(self.db).count(), 3) delete_timed_schedule_instance(self.timed_instance1) self.assertEqual(AlertScheduleInstance.objects.using(self.db).count(), 3) self.assertEqual(TimedScheduleInstance.objects.using(self.db).count(), 2) with self.assertRaises(TimedScheduleInstance.DoesNotExist): get_timed_schedule_instance(self.uuid4)
def delete_instance(instance): if isinstance(instance, AlertScheduleInstance): delete_alert_schedule_instance(instance) elif isinstance(instance, TimedScheduleInstance): delete_timed_schedule_instance(instance) elif isinstance( instance, (CaseAlertScheduleInstance, CaseTimedScheduleInstance)): delete_case_schedule_instance(instance) else: raise TypeError("Unexpected type: %s" % type(instance))
def tearDown(self): for instance in get_timed_schedule_instances_for_schedule(self.schedule): delete_timed_schedule_instance(instance)