
- Add User model with email-based authentication - Add Tag model with many-to-many relationship to todos - Add TodoTag junction table for todo-tag relationships - Enhance Todo model with priority levels (low, medium, high) - Add due_date field with datetime support - Add recurrence_pattern field for recurring todos - Add parent-child relationship for subtasks support - Create comprehensive alembic migration for all changes - Add proper indexes for performance optimization - Use Text type for todo descriptions - Implement proper SQLAlchemy relationships and foreign keys
75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Enum, Text
|
|
from sqlalchemy.sql import func
|
|
from sqlalchemy.orm import relationship
|
|
from enum import Enum as PyEnum
|
|
from .db.base import Base
|
|
|
|
|
|
class PriorityLevel(PyEnum):
|
|
LOW = "low"
|
|
MEDIUM = "medium"
|
|
HIGH = "high"
|
|
|
|
|
|
class RecurrencePattern(PyEnum):
|
|
NONE = "none"
|
|
DAILY = "daily"
|
|
WEEKLY = "weekly"
|
|
MONTHLY = "monthly"
|
|
YEARLY = "yearly"
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
email = Column(String, unique=True, index=True, nullable=False)
|
|
name = Column(String, nullable=False)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
|
|
|
# Relationship with todos
|
|
todos = relationship("Todo", back_populates="user")
|
|
|
|
|
|
class Tag(Base):
|
|
__tablename__ = "tags"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String, unique=True, index=True, nullable=False)
|
|
color = Column(String, nullable=True) # Optional color for UI
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
|
|
class TodoTag(Base):
|
|
__tablename__ = "todo_tags"
|
|
|
|
todo_id = Column(Integer, ForeignKey("todos.id"), primary_key=True)
|
|
tag_id = Column(Integer, ForeignKey("tags.id"), primary_key=True)
|
|
|
|
|
|
class Todo(Base):
|
|
__tablename__ = "todos"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
title = Column(String, index=True, nullable=False)
|
|
description = Column(Text, nullable=True)
|
|
completed = Column(Boolean, default=False, nullable=False)
|
|
priority = Column(Enum(PriorityLevel), default=PriorityLevel.MEDIUM, nullable=False, index=True)
|
|
due_date = Column(DateTime(timezone=True), nullable=True, index=True)
|
|
recurrence_pattern = Column(Enum(RecurrencePattern), default=RecurrencePattern.NONE, nullable=False)
|
|
|
|
# User relationship
|
|
user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
|
|
user = relationship("User", back_populates="todos")
|
|
|
|
# Parent-child relationship for subtasks
|
|
parent_id = Column(Integer, ForeignKey("todos.id"), nullable=True, index=True)
|
|
parent = relationship("Todo", remote_side=[id], back_populates="subtasks")
|
|
subtasks = relationship("Todo", back_populates="parent")
|
|
|
|
# Many-to-many relationship with tags
|
|
tags = relationship("Tag", secondary="todo_tags", backref="todos")
|
|
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) |