Example #1
0
 def test_processes_task_plans(self):
     user_id = 10
     repeated_task = TaskFactory()
     repeated_task.status = Status.TEMPLATE.value
     repeated_task.user_id = user_id
     task_id = self.task_storage.create(repeated_task).id
     before_tasks_count = len(self.task_storage.user_tasks(user_id))
     interval = 300
     big_interval = interval * 10
     last_created_at = datetime.datetime.now() - datetime.timedelta(
         seconds=interval + 5)
     """
     repeated_task_plan after processing should create new task
     repeated_task_plan_big_interval should not create new task because of bit interval
     """
     repeated_task_plan = TaskPlan(user_id=user_id,
                                   task_id=task_id,
                                   last_created_at=last_created_at,
                                   interval=interval)
     repeated_task_plan_big_interval = TaskPlan(
         user_id=user_id,
         task_id=task_id,
         last_created_at=last_created_at,
         interval=big_interval)
     self.task_plan_storage.create(repeated_task_plan)
     self.task_plan_storage.create(repeated_task_plan_big_interval)
     self.task_plan_storage.process_plans(self.task_storage)
     self.assertEqual(len(self.task_storage.user_tasks(user_id)),
                      before_tasks_count + 1)
Example #2
0
 def create(self, plan):
     return self.to_plan_instance(
         TaskPlan.create(id=plan.id,
                         interval=plan.interval,
                         user_id=plan.user_id,
                         task_id=plan.task_id,
                         last_created_at=plan.last_created_at))
Example #3
0
    def process_plans(self, task_storage):
        """Creates tasks according to task plans."""

        for plan in TaskPlan.select():
            if (plan.last_created_at + datetime.timedelta(
                    seconds=plan.interval)) < datetime.datetime.now():
                try:
                    task = task_storage.get_by_id(
                        plan.task_id)  # template task
                    task.id = None
                    task.status = Status.TODO.value  # change status from TEMPLATE to TODO
                    task.plan_id = plan.id
                    task_storage.create(task)
                    """
                    last_created_at shouldn't offset the interval.
                    E.g. user wants to plan task for every monday on 10:00.
                    If this method called on tuesday in last_created_at should be monday
                    in order to save the rule that it creates task every monday on 10:00.
                    """
                    last_created_at = (
                        plan.last_created_at +
                        datetime.timedelta(seconds=plan.interval))
                    while (last_created_at + datetime.timedelta(
                            seconds=plan.interval)) < datetime.datetime.now():
                        last_created_at += datetime.timedelta(
                            seconds=plan.interval)
                    plan.last_created_at = last_created_at
                    plan.save()
                except DoesNotExist:
                    pass
Example #4
0
 def test_updates_task_plan(self):
     new_interval = 500
     new_datetime = datetime.datetime.now()
     task_plan_with_id = self.task_plan_storage.create(self.task_plan)
     task_plan_with_id.interval = new_interval
     task_plan_with_id.last_created_at = new_datetime
     self.task_plan_storage.update(task_plan_with_id)
     task_plan_from_db = TaskPlan.get(TaskPlan.id == task_plan_with_id.id)
     self.assertEqual(task_plan_from_db.interval, new_interval)
     self.assertEqual(task_plan_from_db.last_created_at, new_datetime)
Example #5
0
 def test_deletes_task_plan_by_id(self):
     task_plan_id = self.task_plan_storage.create(self.task_plan).id
     self.task_plan_storage.delete_by_id(task_plan_id)
     self.assertEqual(
         TaskPlan.select().where(TaskPlan.id == task_plan_id).count(), 0)
Example #6
0
 def test_creates_task_plan(self):
     before_plans_count = TaskPlan.select().count()
     self.task_plan_storage.create(self.task_plan)
     after_plans_count = TaskPlan.select().count()
     self.assertEqual(before_plans_count + 1, after_plans_count)
Example #7
0
 def delete_by_id(self, task_id):
     Task.delete().where(Task.id == task_id).execute()
     TaskPlan.delete().where(TaskPlan.task_id == task_id).execute()
Example #8
0
 def test_deletes_task_plan_by_id(self):
     task_plan_id = self.task_plans_controller.create(self.task_plan).id
     self.task_plans_controller.delete(task_plan_id)
     self.assertEqual(
         TaskPlan.select().where(TaskPlan.id == task_plan_id).count(), 0)
Example #9
0
 def all_user_plans(self, user_id):
     return list(
         map(self.to_plan_instance,
             list(TaskPlan.select().where(TaskPlan.user_id == user_id))))
Example #10
0
 def get_by_id(self, plan_id):
     try:
         return self.to_plan_instance(TaskPlan.get(TaskPlan.id == plan_id))
     except DoesNotExist:
         return None
Example #11
0
 def update(self, plan):
     TaskPlan.update(interval=plan.interval,
                     last_created_at=plan.last_created_at).where(
                         TaskPlan.id == plan.id).execute()
Example #12
0
 def delete_by_id(self, plan_id):
     TaskPlan.delete().where(TaskPlan.id == plan_id).execute()