From 32e0aa593b69c80845e6101c5aac8d591757a577 Mon Sep 17 00:00:00 2001 From: jpt Date: Sun, 5 Jan 2025 03:45:14 -0600 Subject: [PATCH] models for recurring tasks --- src/tt/db.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/tt/db.py b/src/tt/db.py index 8d19571..00801de 100644 --- a/src/tt/db.py +++ b/src/tt/db.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timedelta from enum import Enum from peewee import ( BooleanField, @@ -8,6 +8,7 @@ from peewee import ( Model, SqliteDatabase, TextField, + JSONField, ) db = SqliteDatabase( @@ -68,9 +69,50 @@ class SavedSearch(BaseModel): return self.name +class TaskGenerator(Model): + template = CharField() + type = CharField() + config = JSONField() + deleted = BooleanField(default=False) + last_generated_at = DateTimeField(null=True) + created_at = DateTimeField(default=datetime.now) + + def should_generate(self) -> bool: + """ + generator types: config keys + recurring: days_between + monthly: day_of_month + """ + if self.deleted: + return False + if not self.last_generated_at: + return True + + now = datetime.now() + if self.type == "recurring": + days_between = self.config["days_between"] + days_since = (now - self.last_generated_at).days + return days_since >= days_between + + elif self.type == "monthly": + day_of_month = self.generator_config["day_of_month"] + + # check each day until now to see if target day occurred + one_day = timedelta(days=1) + check_date = self.last_generated_at + one_day + while check_date <= now: + if check_date.day == day_of_month: + return True + check_date += one_day + + return False + + return False + + def initialize_db(): db.connect() - db.create_tables([Category, Task, SavedSearch]) + db.create_tables([Category, Task, SavedSearch, TaskGenerator]) if not Category.select().exists(): Category.create(name="default") db.close()