Files
CosScene/server/app/models/event.py
T
2026-05-09 16:40:29 +08:00

75 lines
3.5 KiB
Python

from datetime import datetime
from sqlalchemy import (
Boolean, DateTime, ForeignKey, Integer,
String, Text, func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
class Event(Base):
__tablename__ = "events"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
creator_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False, index=True)
title: Mapped[str] = mapped_column(String(200), nullable=False)
city: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
description: Mapped[str | None] = mapped_column(Text)
cover_url: Mapped[str | None] = mapped_column(String(500))
location_name: Mapped[str | None] = mapped_column(String(200))
start_time: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
end_time: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
max_participants: Mapped[int] = mapped_column(Integer, default=0)
spot_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("spots.id"), nullable=True)
status: Mapped[str] = mapped_column(String(20), default="upcoming", index=True)
audit_status: Mapped[str] = mapped_column(String(20), default="pending")
reject_reason: Mapped[str | None] = mapped_column(String(500))
registration_count: Mapped[int] = mapped_column(Integer, default=0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
creator = relationship("User", lazy="selectin")
spot = relationship("Spot", lazy="selectin")
registrations = relationship("EventRegistration", back_populates="event", lazy="selectin")
photos = relationship("EventPhoto", back_populates="event", lazy="selectin")
def __repr__(self) -> str:
return f"<Event {self.id} {self.title}>"
class EventRegistration(Base):
__tablename__ = "event_registrations"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
event_id: Mapped[int] = mapped_column(Integer, ForeignKey("events.id"), nullable=False, index=True)
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False, index=True)
status: Mapped[str] = mapped_column(String(20), default="registered")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
event = relationship("Event", back_populates="registrations")
user = relationship("User", lazy="selectin")
def __repr__(self) -> str:
return f"<EventRegistration {self.id}>"
class EventPhoto(Base):
__tablename__ = "event_photos"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
event_id: Mapped[int] = mapped_column(Integer, ForeignKey("events.id"), nullable=False, index=True)
uploader_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
image_url: Mapped[str] = mapped_column(String(500), nullable=False)
caption: Mapped[str | None] = mapped_column(String(200))
spot_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("spots.id"), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
event = relationship("Event", back_populates="photos")
uploader = relationship("User", lazy="selectin")
spot = relationship("Spot", lazy="selectin")
def __repr__(self) -> str:
return f"<EventPhoto {self.id}>"