Initial commit: Schedule service for son
This commit is contained in:
commit
af2ea7be06
19 changed files with 2270 additions and 0 deletions
64
backend/database.py
Normal file
64
backend/database.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text
|
||||
import os
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_PATH", "data/schedule.db")
|
||||
# SQLite через aiosqlite требует sqlite+aiosqlite:///
|
||||
DATABASE_URL_ASYNC = f"sqlite+aiosqlite:///{DATABASE_URL}"
|
||||
|
||||
engine = create_async_engine(DATABASE_URL_ASYNC, echo=False)
|
||||
AsyncSessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
|
||||
class Task(Base):
|
||||
__tablename__ = "tasks"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
date = Column(String, index=True) # YYYY-MM-DD
|
||||
title = Column(String, nullable=False)
|
||||
repeat_weekly = Column(Boolean, default=False)
|
||||
weekday = Column(Integer) # 0=Monday, 6=Sunday, только для weekly tasks
|
||||
|
||||
|
||||
class Event(Base):
|
||||
__tablename__ = "events"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
date = Column(String, index=True) # YYYY-MM-DD
|
||||
start_time = Column(String) # HH:MM
|
||||
duration_min = Column(Integer, nullable=False)
|
||||
title = Column(String, nullable=False)
|
||||
|
||||
|
||||
class WeeklyTaskException(Base):
|
||||
__tablename__ = "weekly_task_exceptions"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
weekday = Column(Integer, nullable=False) # 0=Monday, 6=Sunday
|
||||
date = Column(String, nullable=False) # YYYY-MM-DD
|
||||
action = Column(String, nullable=False) # "delete" или "replace"
|
||||
replacement_title = Column(String) # для action="replace"
|
||||
|
||||
|
||||
class TelegramUser(Base):
|
||||
__tablename__ = "telegram_users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
chat_id = Column(String, unique=True, nullable=False, index=True)
|
||||
|
||||
|
||||
async def init_db():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
|
||||
async def get_db():
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue