def test_generate_task_occurrences(self): """ Test generate_task_occurrences works correctly """ task1 = mommy.make("tasking.Task", timing_rule="RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5") task2 = mommy.make( "tasking.Task", timing_rule="RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5000") # pylint: disable=line-too-long rule1 = "DTSTART:20180501T070000Z RRULE:FREQ=YEARLY;BYDAY=SU;BYSETPOS=1;BYMONTH=1;UNTIL=20480521T210000Z" # noqa task3 = mommy.make("tasking.Task", timing_rule=rule1) # remove any auto-generated occurrences # pylint: disable=no-member TaskOccurrence.objects.all().delete() # we should get 5 occurrences occurrences1 = generate_task_occurrences(task=task1, timing_rule=task1.timing_rule) self.assertEqual(5, occurrences1.count()) # we should get {MAX_OCCURRENCES} occurrences occurrences2 = generate_task_occurrences(task=task2, timing_rule=task2.timing_rule) self.assertEqual(MAX_OCCURRENCES, occurrences2.count()) # the start times should all be from the timing_rule, whic in this # case is the time the task was created # the end_times should all be 23:59:59 for item in occurrences1: self.assertEqual( item.start_time.hour, task1.start.astimezone( timezone.get_current_timezone()).time().hour, ) self.assertEqual( item.start_time.minute, task1.start.astimezone( timezone.get_current_timezone()).time().minute, ) self.assertEqual(item.end_time, time(23, 59, 59, 999999)) # we should have 30 occurrences occurrences3 = generate_task_occurrences(task=task3, timing_rule=task3.timing_rule) self.assertEqual(30, occurrences3.count()) # the start times should all be from the timing_rule, whicih in this # case 7am # the end_times should all be 23:59:59 apart from the last one which # should be from the timing rule, which in this case is 9pm for item in occurrences3: self.assertEqual(item.start_time, time(7, 0, 0, 0)) if item == occurrences3.last(): self.assertEqual(item.end_time, time(21, 0, 0, 0)) else: self.assertEqual(item.end_time, time(23, 59, 59, 999999))
def create_occurrences(sender, instance, created, **kwargs): """ Create occurrences when a task timing_rule changes """ if instance.timing_rule: # delete any existing occurrences instance.taskoccurrence_set.all().delete() # generate new occurrences generate_task_occurrences(instance)
def create_occurrences(task): """ Updates a task's occurrences Deletes a task's future occurrences and recreates them This task is meant to be called by a post_save signal on the Task model """ # pylint: disable=no-member # delete all future occurrences future_occurrences = TaskOccurrence.objects.filter( task=task, date__gt=timezone.now().date()) future_occurrences.delete() # now generate new occurrences if any # first, occurrences based on the task timing rule if task.timing_rule: generate_task_occurrences(task=task, timing_rule=task.timing_rule, OccurrenceModelClass=TaskOccurrence) # next, occurrences based on TaskLocations task_locations = TaskLocation.objects.filter(task=task) for task_location in task_locations: generate_tasklocation_occurrences(task_location, OccurrenceModelClass=TaskOccurrence)
def test_no_same_start_and_end(self): """ Test that no occurrences are created when start and end times are the same (because it does not make sense) """ # pylint: disable=line-too-long rule1 = 'DTSTART:20180501T210000Z RRULE:FREQ=YEARLY;BYDAY=SU;BYSETPOS=1;BYMONTH=1;UNTIL=20280521T210000Z' # noqa task = mommy.make('tasking.Task', timing_rule=rule1) # Delete any autogenerated occurrences # pylint: disable=no-member TaskOccurrence.objects.all().delete() # we should have 9 instead of 10 occurrences because the very last # one would start at 9pm and end at 9pm self.assertEqual(9, generate_task_occurrences(task).count())