/
REDOSSSSS
/
tg-bot-dental
Обзор
Документация
Войти
/
REDOSSSSS
/
tg-bot-dental
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
database/models.py
116 строк
4 KB
iwan
done project
04 июн 2026, 00:09
04 июн 2026, 00:09
64f2830
Код
Авторство
О чём код?
from __future__ import annotations import enum from datetime import date, datetime, time from sqlalchemy import ( BigInteger, Boolean, Date, DateTime, Enum, ForeignKey, Integer, String, Text, Time, UniqueConstraint, ) from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship class Base(DeclarativeBase): pass class UserRole(str, enum.Enum): client = "client" admin = "admin" class AppointmentStatus(str, enum.Enum): active = "active" completed = "completed" cancelled = "cancelled" class User(Base): __tablename__ = "users" id: Mapped[int] = mapped_column(Integer, primary_key=True) telegram_id: Mapped[int] = mapped_column(BigInteger, unique=True, nullable=False) role: Mapped[UserRole] = mapped_column( Enum(UserRole), default=UserRole.client, nullable=False ) is_subscribed: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) appointments: Mapped[list["Appointment"]] = relationship(back_populates="user") class Service(Base): __tablename__ = "services" id: Mapped[int] = mapped_column(Integer, primary_key=True) name: Mapped[str] = mapped_column(String(255), nullable=False) description: Mapped[str] = mapped_column(Text, default="", nullable=False) price_rub: Mapped[int] = mapped_column(Integer, nullable=False) appointment_links: Mapped[list["AppointmentService"]] = relationship( back_populates="service" ) class Appointment(Base): __tablename__ = "appointments" id: Mapped[int] = mapped_column(Integer, primary_key=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False) date: Mapped[date] = mapped_column(Date, nullable=False) time_start: Mapped[time] = mapped_column(Time, nullable=False) status: Mapped[AppointmentStatus] = mapped_column( Enum(AppointmentStatus), default=AppointmentStatus.active, nullable=False ) time_edit_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False) updated_at: Mapped[datetime] = mapped_column( DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False ) user: Mapped["User"] = relationship(back_populates="appointments") service_links: Mapped[list["AppointmentService"]] = relationship( back_populates="appointment", cascade="all, delete-orphan" ) class AppointmentService(Base): __tablename__ = "appointment_service" __table_args__ = ( UniqueConstraint("appointment_id", "service_id", name="uq_appointment_service"), ) appointment_id: Mapped[int] = mapped_column( ForeignKey("appointments.id", ondelete="CASCADE"), primary_key=True ) service_id: Mapped[int] = mapped_column( ForeignKey("services.id", ondelete="CASCADE"), primary_key=True ) appointment: Mapped["Appointment"] = relationship(back_populates="service_links") service: Mapped["Service"] = relationship(back_populates="appointment_links") class Contact(Base): __tablename__ = "contacts" id: Mapped[int] = mapped_column(Integer, primary_key=True) phone: Mapped[str] = mapped_column(String(64), default="", nullable=False) address: Mapped[str] = mapped_column(String(512), default="", nullable=False) work_schedule_text: Mapped[str] = mapped_column(Text, default="", nullable=False) class AdminSettings(Base): __tablename__ = "admin_settings" id: Mapped[int] = mapped_column(Integer, primary_key=True) work_start: Mapped[time] = mapped_column(Time, nullable=False) work_end: Mapped[time] = mapped_column(Time, nullable=False) slot_interval_minutes: Mapped[int] = mapped_column(Integer, nullable=False)