Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 def create_new_instance_for_recipient(self, recipient_type, recipient_id):
     return TimedScheduleInstance.create_for_recipient(
         self.schedule,
         recipient_type,
         recipient_id,
         start_date=self.start_date,
         move_to_next_event_not_in_the_past=True,
         schedule_revision=self.schedule_revision,
     )
Ejemplo n.º 3
0
 def create_new_instance_for_recipient(self, recipient_type, recipient_id):
     return TimedScheduleInstance.create_for_recipient(
         self.schedule,
         recipient_type,
         recipient_id,
         start_date=self.start_date,
         move_to_next_event_not_in_the_past=True,
         schedule_revision=self.schedule_revision,
     )
Ejemplo n.º 4
0
 def make_timed_schedule_instance(cls, schedule_instance_id, schedule_id=None, active=True):
     return TimedScheduleInstance(
         schedule_instance_id=schedule_instance_id,
         domain=cls.domain,
         recipient_type='CommCareUser',
         recipient_id=uuid.uuid4().hex,
         current_event_num=0,
         schedule_iteration_num=1,
         next_event_due=datetime(2017, 3, 1),
         active=active,
         timed_schedule_id=schedule_id or uuid.uuid4(),
         start_date=date(2017, 3, 1),
     )