from telebot import types import db.queries.user_queries as queries import keyboards import settings from admin_tools import Admin from db.db_worker import DBSession from db.models.base import BaseModel from db.models.user import User from keyboards import main_menu engine = create_engine('sqlite:///test.db?check_same_thread=False', echo=True) BaseModel.create_base(engine) db = DBSession(sessionmaker(bind=engine)()) bot = telebot.TeleBot(settings.__TELEGRAM_TOKEN__, parse_mode=None) admin = Admin(db=db, bot=bot) address = 'Не указано' @bot.message_handler(commands=['start']) def start(message): user_id = message.from_user.id print(user_id) response = queries.check_user_in_db(db, user_id) if response is None: db.add_model(User(tg_id=user_id))
def check_user_in_db(session: DBSession, user_id: int): result = session.query(User).filter(User.tg_id == user_id).scalar() return result
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from db.db_worker import DBSession from db.models.base import BaseModel from db.models.category import Category from db.models.item import Item from db.models.user import User engine = create_engine('sqlite:///test.db', echo=True) BaseModel.create_base(engine) db = DBSession(sessionmaker(bind=engine)()) user1 = User(tg_id=1) db.add_model(user1) category1 = Category(category_name='Hoodie') db.add_model(category1) item1 = Item(name='item1', clothe_size='XS', description='Крутой товар', category=category1, picture=None) item2 = Item(name='item2', clothe_size='S', description='Крутой товар но размером больше', category=category1, picture=None) db.add_model(item1) db.add_model(item2) user1.items.append(item1)
def get_count_items(session: DBSession, user: User, item_id: int): records = session.query(User).join( User.items).filter(Item.id == item_id).count() return records
def get_user(session: DBSession, tg_id: int): record = session.query(User).filter(User.tg_id == tg_id).scalar() return record
def get_cagegories(session: DBSession): result = session.query(Category).all() return result
def get_items_by_cat_id(session: DBSession, cat_id: int): items = session.query(Item).filter(Item.category_id == cat_id).all() return items
def get_item_by_id(session: DBSession, item_id: int): item = session.query(Item).filter(Item.id == item_id).scalar() return item
def get_category_by_id(session: DBSession, cat_id: int): category = session.query(Category).filter(Category.id == cat_id).scalar() return category
def get_category(session: DBSession, cat_name: str): category = session.query(Category).filter( Category.category_name == cat_name).scalar() return category