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
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)
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)
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)
def all_user_plans(self, user_id): return list( map(self.to_plan_instance, list(TaskPlan.select().where(TaskPlan.user_id == user_id))))