generate tasks when needed, still need to consider more cases
This commit is contained in:
parent
3279dd9e8c
commit
ccd0178de1
@ -1,5 +1,7 @@
|
|||||||
from ..db import db, TaskGenerator, GeneratorType
|
|
||||||
import json
|
import json
|
||||||
|
from datetime import date, timedelta
|
||||||
|
from ..db import db, TaskGenerator, GeneratorType
|
||||||
|
from .tasks import add_task
|
||||||
|
|
||||||
|
|
||||||
def get_generator(item_id: int) -> TaskGenerator:
|
def get_generator(item_id: int) -> TaskGenerator:
|
||||||
@ -27,6 +29,28 @@ def add_generator(
|
|||||||
return task
|
return task
|
||||||
|
|
||||||
|
|
||||||
|
def generate_needed_tasks():
|
||||||
|
to_create = []
|
||||||
|
for g in get_generators():
|
||||||
|
next = g.next_at()
|
||||||
|
if not next:
|
||||||
|
continue
|
||||||
|
# TODO: make configurable
|
||||||
|
if date.today() - next > timedelta(days=14):
|
||||||
|
to_create.append(
|
||||||
|
{
|
||||||
|
"text": g.template.format(next=next),
|
||||||
|
"category": "recurring",
|
||||||
|
"due": next,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
for c in to_create:
|
||||||
|
add_task(**c)
|
||||||
|
|
||||||
|
return to_create
|
||||||
|
|
||||||
|
|
||||||
def update_generator(
|
def update_generator(
|
||||||
item_id: int,
|
item_id: int,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
15
src/tt/db.py
15
src/tt/db.py
@ -96,12 +96,14 @@ class TaskGenerator(BaseModel):
|
|||||||
year, month, day = today.year, today.month, today.day
|
year, month, day = today.year, today.month, today.day
|
||||||
day_of_month = val
|
day_of_month = val
|
||||||
|
|
||||||
|
# if we need to go forward a month
|
||||||
if day_of_month < day:
|
if day_of_month < day:
|
||||||
month += 1
|
month += 1
|
||||||
if month == 13:
|
if month == 13:
|
||||||
month = 1
|
month = 1
|
||||||
year += 1
|
year += 1
|
||||||
|
|
||||||
|
# recurring tasks on 29-31 in Feb will just happen on 28th
|
||||||
if day_of_month >= 29 and month == 2:
|
if day_of_month >= 29 and month == 2:
|
||||||
maybe_next = date(year, month, 28)
|
maybe_next = date(year, month, 28)
|
||||||
else:
|
else:
|
||||||
@ -110,7 +112,8 @@ class TaskGenerator(BaseModel):
|
|||||||
if not self.last_generated_at or self.last_generated_at < maybe_next:
|
if not self.last_generated_at or self.last_generated_at < maybe_next:
|
||||||
return maybe_next
|
return maybe_next
|
||||||
|
|
||||||
# need to go forward one more month
|
# TODO: this doesn't handle if a month was missed somehow, just advances one
|
||||||
|
# same logic as above, if we're stepping another month forward
|
||||||
month += 1
|
month += 1
|
||||||
if month == 13:
|
if month == 13:
|
||||||
month = 1
|
month = 1
|
||||||
@ -121,12 +124,6 @@ class TaskGenerator(BaseModel):
|
|||||||
maybe_next = date(year, month, day_of_month)
|
maybe_next = date(year, month, day_of_month)
|
||||||
return maybe_next
|
return maybe_next
|
||||||
|
|
||||||
def should_generate(self) -> bool:
|
|
||||||
next = self.next_at()
|
|
||||||
if not next:
|
|
||||||
return False
|
|
||||||
return next <= date.today()
|
|
||||||
|
|
||||||
|
|
||||||
def initialize_db():
|
def initialize_db():
|
||||||
db.connect()
|
db.connect()
|
||||||
@ -134,7 +131,3 @@ def initialize_db():
|
|||||||
if not Category.select().exists():
|
if not Category.select().exists():
|
||||||
Category.create(name="default")
|
Category.create(name="default")
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
initialize_db()
|
|
||||||
|
@ -5,6 +5,7 @@ from ..controller.generators import (
|
|||||||
get_generators,
|
get_generators,
|
||||||
add_generator,
|
add_generator,
|
||||||
update_generator,
|
update_generator,
|
||||||
|
generate_needed_tasks,
|
||||||
)
|
)
|
||||||
from ..db import GeneratorType
|
from ..db import GeneratorType
|
||||||
from .editor import (
|
from .editor import (
|
||||||
@ -34,6 +35,9 @@ class TaskGenEditor(TableEditor):
|
|||||||
self.get_item_callback = get_generator
|
self.get_item_callback = get_generator
|
||||||
|
|
||||||
def refresh_items(self):
|
def refresh_items(self):
|
||||||
|
generated = generate_needed_tasks()
|
||||||
|
if num := len(generated):
|
||||||
|
self.notify(f"created {num} tasks")
|
||||||
items = get_generators()
|
items = get_generators()
|
||||||
for item in items:
|
for item in items:
|
||||||
self.table.add_row(
|
self.table.add_row(
|
||||||
|
Loading…
Reference in New Issue
Block a user