- Migration 007: parent_event_id (self-ref FK CASCADE), is_recurring, original_start columns on calendar_events - CalendarEvent model: three new Mapped[] columns for recurrence tracking - RecurrenceRule Pydantic model: typed schema for every_n_days, weekly, monthly_nth_weekday, monthly_date - CalendarEventCreate/Update: accept structured RecurrenceRule (router serializes to JSON string for DB) - CalendarEventUpdate: edit_scope field (this | this_and_future) - CalendarEventResponse: exposes parent_event_id, is_recurring, original_start - recurrence.py service: generates unsaved child ORM objects from parent rule up to 365-day horizon - GET /: excludes parent template rows (children are displayed instead) - POST /: creates parent + bulk children when recurrence_rule provided - PUT /: scope=this detaches occurrence; scope=this_and_future deletes future siblings and regenerates - DELETE /: scope=this deletes one; scope=this_and_future deletes future siblings; no scope deletes all (CASCADE) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
48 lines
2.3 KiB
Python
48 lines
2.3 KiB
Python
from sqlalchemy import String, Text, Boolean, Integer, ForeignKey, func
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
from app.database import Base
|
|
|
|
|
|
class CalendarEvent(Base):
|
|
__tablename__ = "calendar_events"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
|
title: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
start_datetime: Mapped[datetime] = mapped_column(nullable=False)
|
|
end_datetime: Mapped[datetime] = mapped_column(nullable=False)
|
|
all_day: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
color: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)
|
|
location_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("locations.id"), nullable=True)
|
|
recurrence_rule: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
is_starred: Mapped[bool] = mapped_column(Boolean, default=False, server_default="false")
|
|
calendar_id: Mapped[int] = mapped_column(Integer, ForeignKey("calendars.id"), nullable=False)
|
|
|
|
# Recurrence fields
|
|
# parent_event_id: set on child events; NULL on the parent template row
|
|
parent_event_id: Mapped[Optional[int]] = mapped_column(
|
|
Integer,
|
|
ForeignKey("calendar_events.id", ondelete="CASCADE"),
|
|
nullable=True,
|
|
)
|
|
# is_recurring: True on both the parent template and all generated children
|
|
is_recurring: Mapped[bool] = mapped_column(Boolean, default=False, server_default="false")
|
|
# original_start: the originally computed occurrence datetime (children only)
|
|
original_start: Mapped[Optional[datetime]] = mapped_column(nullable=True)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(default=func.now())
|
|
updated_at: Mapped[datetime] = mapped_column(default=func.now(), onupdate=func.now())
|
|
|
|
location: Mapped[Optional["Location"]] = relationship(back_populates="events")
|
|
calendar: Mapped["Calendar"] = relationship(back_populates="events")
|
|
|
|
@property
|
|
def calendar_name(self) -> str:
|
|
return self.calendar.name if self.calendar else ""
|
|
|
|
@property
|
|
def calendar_color(self) -> str:
|
|
return self.calendar.color if self.calendar else ""
|