models for recurring tasks
This commit is contained in:
parent
802d8f2db4
commit
32e0aa593b
46
src/tt/db.py
46
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()
|
||||
|
Loading…
Reference in New Issue
Block a user