From e53b57464f70754d9185ef54d4d5f6c0b15232ae Mon Sep 17 00:00:00 2001 From: csasq Date: Fri, 30 Aug 2024 17:09:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=B0=20=D0=BD=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B0=D1=80=D1=85=D0=B8=D1=82=D0=B5=D0=BA=D1=82=D1=83=D1=80?= =?UTF-8?q?=D0=B0=20=D0=B1=D0=B0=D0=B7=D1=8B=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.sql | 15 ++++++++------- database/main.py | 18 ++++++++++++------ models/main.py | 22 +++++++++++++++++++++- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/database.sql b/database.sql index 10ea734..f394f12 100644 --- a/database.sql +++ b/database.sql @@ -20,6 +20,7 @@ create table scripts ( owner_id bigint not null, name character varying (64) not null, start_time character varying (256) not null, + is_enabled boolean not null, primary key (id), foreign key (owner_id) references users on delete cascade on update cascade, unique (name) @@ -36,13 +37,13 @@ create table message_templates ( ); -- TODO(messages ссылается на шаблонную таблицу message_templates) -create table messages ( - id bigserial not null, - message_template_id bigint not null, - telegram_message_id bigint not null, - primary key (id), - foreign key (message_template_id) references message_templates on delete cascade on update cascade -); +-- create table messages ( +-- id bigserial not null, +-- message_template_id bigint not null, +-- telegram_message_id bigint not null, +-- primary key (id), +-- foreign key (message_template_id) references message_templates on delete cascade on update cascade +-- ); create table text_templates ( text character varying (4096) not null diff --git a/database/main.py b/database/main.py index 20f74d8..f243b62 100644 --- a/database/main.py +++ b/database/main.py @@ -20,7 +20,7 @@ class NotFoundError(Exception): class Users: @staticmethod async def insert_or_update_user( - telegram_id: int, + telegram_user_id: int, first_name: str, last_name: str = None, username: str = None, @@ -34,7 +34,7 @@ class Users: last_name, username ) values ( - %(telegram_id)s, + %(telegram_user_id)s, %(first_name)s, %(last_name)s, %(username)s @@ -43,24 +43,30 @@ class Users: last_name = excluded.last_name, username = excluded.username returning - users.id; + users.id, + users.telegram_user_id, + users.first_name, + users.last_name, + users.username, + users.role; ''' await cursor.execute( sql, { - 'telegram_id': telegram_id, + 'telegram_user_id': telegram_user_id, 'first_name': first_name, 'last_name': last_name, 'username': username, }, ) - user_id, = await cursor.fetchone() + user_id, telegram_user_id, first_name, last_name, username, role, = await cursor.fetchone() return models.User( id=user_id, - telegram_id=telegram_id, + telegram_id=telegram_user_id, first_name=first_name, last_name=last_name, username=username, + role=role, ) diff --git a/models/main.py b/models/main.py index 45d49a5..3176a33 100644 --- a/models/main.py +++ b/models/main.py @@ -1,17 +1,37 @@ from datetime import datetime +from enum import Enum from typing import Optional from pydantic import BaseModel, Field +class UserRole(Enum): + user = 'user' + moderator = 'moderator' + admin = 'admin' + + class User(BaseModel): id: int - telegram_id: int = Field( + telegram_user_id: int = Field( exclude=True, ) first_name: str last_name: Optional[str] username: Optional[str] + role: UserRole + + +class Scripts(BaseModel): + id: int + owner: User + name: str + start_time: str + messages = [] + is_enabled: bool + + +######################################################################################################################## class PollSchema(BaseModel):