update database
This commit is contained in:
8
app/__main__.py
Normal file
8
app/__main__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
from app.bot import main
|
||||
|
||||
if sys.platform == "win32":
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||
asyncio.run(main())
|
||||
@@ -1,15 +1,15 @@
|
||||
import asyncio
|
||||
|
||||
from aiogram import Bot, Dispatcher
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
from aiogram.enums import ParseMode
|
||||
from aiogram_dialog import setup_dialogs
|
||||
from structlog import get_logger
|
||||
|
||||
from app.bot.dialogs.flows import dialogs_router
|
||||
from app.bot.handlers.commands import commands_router
|
||||
from app.bot.middlewares import DbSessionMiddleware, GetUserMiddleware
|
||||
from config.config import settings
|
||||
|
||||
logger = get_logger()
|
||||
bot = Bot(
|
||||
token=settings.bot_token,
|
||||
default=DefaultBotProperties(parse_mode=ParseMode.MARKDOWN_V2),
|
||||
@@ -25,9 +25,5 @@ async def main():
|
||||
|
||||
dp.include_router(commands_router)
|
||||
dp.include_router(dialogs_router)
|
||||
|
||||
logger.info("Bot runned")
|
||||
await dp.start_polling(bot)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
@@ -12,6 +12,7 @@ from .states import ProfileSG
|
||||
def name_type_factory(text: str):
|
||||
if not 2 < len(text) < 20:
|
||||
raise ValueError("Имя должно быть от 2 до 20 символов")
|
||||
return text
|
||||
|
||||
def phone_type_factory(text: str):
|
||||
if len(re.findall("\d", text)) < 11:
|
||||
|
||||
14
app/infrastructure/database/enums.py
Normal file
14
app/infrastructure/database/enums.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class UserRole(Enum):
|
||||
USER = "user"
|
||||
ADMIN = "admin"
|
||||
TEACHER = "teacher"
|
||||
|
||||
|
||||
class EventUserStatus(Enum):
|
||||
NOT_CONFIRMED = "not confirmed"
|
||||
CONFIRMED = "confirmed"
|
||||
CANCELLED = "cancelled"
|
||||
COMPLETED = "completed"
|
||||
@@ -2,6 +2,7 @@ import asyncio
|
||||
|
||||
from .models import Base
|
||||
from .session import engine
|
||||
import sys
|
||||
|
||||
|
||||
async def init_db():
|
||||
@@ -10,4 +11,6 @@ async def init_db():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if sys.platform == "win32":
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||
asyncio.run(init_db())
|
||||
@@ -1,23 +1,26 @@
|
||||
import datetime as dt
|
||||
|
||||
from sqlalchemy import (
|
||||
BigInteger,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
func,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, declarative_base, mapped_column
|
||||
from sqlalchemy import BigInteger, DateTime, ForeignKey, UniqueConstraint, func
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
|
||||
Base = declarative_base()
|
||||
from .enums import EventUserStatus, 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, unique=True, nullable=False)
|
||||
tg_id: Mapped[int] = mapped_column(BigInteger, unique=True)
|
||||
fullname: Mapped[str] = mapped_column()
|
||||
role: Mapped[str] = mapped_column(default="user")
|
||||
role: Mapped[str] = mapped_column(default=UserRole.USER.value)
|
||||
phone: Mapped[str] = mapped_column(nullable=True)
|
||||
address: Mapped[str] = mapped_column(nullable=True)
|
||||
|
||||
events = relationship("UserEvent", back_populates="user_rel")
|
||||
certificates = relationship("Certificate", back_populates="user_rel")
|
||||
|
||||
|
||||
class Event(Base):
|
||||
@@ -25,16 +28,45 @@ class Event(Base):
|
||||
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.date] = mapped_column(nullable=True)
|
||||
end_date: Mapped[dt.date] = mapped_column(nullable=True)
|
||||
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: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
||||
event: Mapped[int] = mapped_column(ForeignKey("event.id"))
|
||||
date: Mapped[dt.date] = mapped_column(
|
||||
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="not confirmed")
|
||||
status: Mapped[str] = mapped_column(default=EventUserStatus.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()
|
||||
__table_args__ = (UniqueConstraint("user_id", "course_id", name="uq_user_course"),)
|
||||
|
||||
Reference in New Issue
Block a user