89 lines
3.5 KiB
Python
89 lines
3.5 KiB
Python
import datetime as dt
|
|
|
|
from sqlalchemy import BigInteger, DateTime, ForeignKey, UniqueConstraint, func
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
|
|
|
from .enums import UserEventStatus, UserRole
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "user"
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
tg_id: Mapped[int] = mapped_column(BigInteger, nullable=True, unique=True)
|
|
fullname: Mapped[str] = mapped_column()
|
|
role: Mapped[str] = mapped_column(default=UserRole.USER.value)
|
|
phone: Mapped[str] = mapped_column(nullable=True)
|
|
email: Mapped[str] = mapped_column(nullable=True)
|
|
events = relationship("UserEvent", back_populates="user_rel")
|
|
certificates = relationship("Certificate", back_populates="user_rel")
|
|
addresses = relationship("Address", back_populates="user_rel")
|
|
|
|
|
|
class Event(Base):
|
|
__tablename__ = "event"
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
title: Mapped[str] = mapped_column()
|
|
description: Mapped[str] = mapped_column(default="")
|
|
start_date: Mapped[dt.datetime] = mapped_column(nullable=True)
|
|
end_date: Mapped[dt.datetime] = mapped_column(nullable=True)
|
|
created_at: Mapped[dt.datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now()
|
|
)
|
|
is_active: Mapped[bool] = mapped_column(default=True)
|
|
user_events = relationship("UserEvent", back_populates="event_rel")
|
|
|
|
|
|
class UserEvent(Base):
|
|
__tablename__ = "user_event"
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
|
event_id: Mapped[int] = mapped_column(ForeignKey("event.id"))
|
|
date: Mapped[dt.datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now()
|
|
)
|
|
status: Mapped[str] = mapped_column(default=UserEventStatus.NOT_CONFIRMED.value)
|
|
|
|
user_rel = relationship("User", back_populates="events")
|
|
event_rel = relationship("Event", back_populates="user_events")
|
|
|
|
__table_args__ = (UniqueConstraint("user_id", "event_id", name="uq_user_event"),)
|
|
|
|
|
|
class Course(Base):
|
|
__tablename__ = "course"
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
title: Mapped[str] = mapped_column()
|
|
description: Mapped[str] = mapped_column(nullable=True)
|
|
|
|
certificates = relationship("Certificate", back_populates="course_rel")
|
|
|
|
|
|
class Certificate(Base):
|
|
__tablename__ = "certificate"
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
|
course_id: Mapped[int] = mapped_column(ForeignKey("course.id"))
|
|
date: Mapped[dt.datetime] = mapped_column()
|
|
|
|
course_rel = relationship("Course", back_populates="certificates")
|
|
user_rel = relationship("User", back_populates="certificates")
|
|
|
|
__table_args__ = (UniqueConstraint("user_id", "course_id", name="uq_user_course"),)
|
|
|
|
|
|
class Address(Base):
|
|
__tablename__ = "address"
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
|
country: Mapped[str] = mapped_column()
|
|
region: Mapped[str] = mapped_column()
|
|
locality: Mapped[str] = mapped_column()
|
|
street_address: Mapped[str] = mapped_column()
|
|
extended_address: Mapped[str] = mapped_column()
|
|
postal_code: Mapped[str] = mapped_column()
|
|
|
|
user_rel = relationship("User", back_populates="addresses") |