53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
"""Идемпотентно добавляет ежегодные напоминания: ДР 12 мая и Новый год."""
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.db.base import SessionLocal
|
|
from app.db.models import Reminder
|
|
from app.reminders.service import RECURRENCE_YEARLY, RemindersService
|
|
|
|
DEFAULTS = (
|
|
{
|
|
"title": "День рождения",
|
|
"due_at": "2027-05-12T09:00:00+03:00",
|
|
"notes": "С днём рождения!",
|
|
"all_day": True,
|
|
"recurrence": RECURRENCE_YEARLY,
|
|
},
|
|
{
|
|
"title": "Новый год",
|
|
"due_at": "2026-12-31T23:55:00+03:00",
|
|
"notes": "С наступающим Новым годом!",
|
|
"all_day": False,
|
|
"recurrence": RECURRENCE_YEARLY,
|
|
},
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
db = SessionLocal()
|
|
try:
|
|
service = RemindersService(db)
|
|
for item in DEFAULTS:
|
|
exists = db.scalar(
|
|
select(Reminder.id)
|
|
.where(
|
|
Reminder.title == item["title"],
|
|
Reminder.recurrence == RECURRENCE_YEARLY,
|
|
Reminder.enabled.is_(True),
|
|
)
|
|
.limit(1)
|
|
)
|
|
if exists:
|
|
print(f"skip: {item['title']} (id={exists})")
|
|
continue
|
|
result = service.create(**item)
|
|
reminder = result["reminder"]
|
|
print(f"created: {reminder['title']} · {reminder['due_at_local']} · yearly")
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|