Skip to content

Commit

Permalink
with Session (#35)
Browse files Browse the repository at this point in the history
Ну поглядим сколько проработает
  • Loading branch information
annndruha authored Aug 26, 2023
1 parent e59e767 commit 64ab5c9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
20 changes: 12 additions & 8 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# 2023

import requests
from sqlalchemy.orm import Session

import src.vk as vk
from src.db import VkUser, session
from src.db import Session, VkUser
from src.settings import settings


Expand All @@ -20,7 +21,8 @@ def check(user: vk.EventUser) -> None | tuple:
:param user: Object of vk.EventUser
:return: db_requisites tuple or None if user not authenticated
"""
data: VkUser | None = session.query(VkUser).filter(VkUser.vk_id == user.user_id).one_or_none()
with Session() as session:
data: VkUser | None = session.query(VkUser).filter(VkUser.vk_id == user.user_id).one_or_none()
if data is not None:
r = requests.get(
url=settings.PRINT_URL + '/is_union_member', params=dict(surname=data.surname, number=data.number, v=1)
Expand All @@ -31,12 +33,14 @@ def check(user: vk.EventUser) -> None | tuple:


def add_user(user: vk.EventUser, surname, number) -> None:
session.add(VkUser(vk_id=user.user_id, surname=surname, number=number))
session.commit()
with Session() as session:
session.add(VkUser(vk_id=user.user_id, surname=surname, number=number))
session.commit()


def update_user(user: vk.EventUser, surname, number) -> None:
data: VkUser | None = session.query(VkUser).filter(VkUser.vk_id == user.user_id).one_or_none()
data.surname = surname
data.number = number
session.commit()
with Session() as session:
data: VkUser | None = session.query(VkUser).filter(VkUser.vk_id == user.user_id).one_or_none()
data.surname = surname
data.number = number
session.commit()
9 changes: 3 additions & 6 deletions src/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import src.marketing as marketing
import src.vk as vk
from src.answers import ans
from src.db import reconnect_session
from src.settings import settings


Expand All @@ -25,7 +24,6 @@ def event_loop():
try:
for event in vk.longpoll.listen():
vk.reconnect()
reconnect_session()
if event.type != VkBotEventType.MESSAGE_NEW:
return
user = vk.EventUser(event)
Expand Down Expand Up @@ -139,10 +137,9 @@ def order_print(user: vk.EventUser, db_requisites):
# Get pin
content, title = file_content_title
vk_id, surname, number = db_requisites
r = requests.post(settings.PRINT_URL + '/file', json={'surname': surname,
'number': number,
'filename': title,
'source': 'vkbot'})
r = requests.post(
settings.PRINT_URL + '/file', json={'surname': surname, 'number': number, 'filename': title, 'source': 'vkbot'}
)

# If get pin error
if r.status_code != 200:
Expand Down
13 changes: 0 additions & 13 deletions src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,3 @@ class VkUser(Base):

engine = create_engine(url=str(settings.DB_DSN), pool_pre_ping=True, isolation_level="AUTOCOMMIT")
Session = sessionmaker(bind=engine)
session = Session()


def reconnect_session():
global engine
global Session
global session

session.rollback()
engine = create_engine(url=str(settings.DB_DSN), pool_pre_ping=True, isolation_level="AUTOCOMMIT")
Session = sessionmaker(bind=engine)
session = Session()
session.rollback()
2 changes: 1 addition & 1 deletion src/keybords.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import src.auth as auth
import src.vk as vk
from src.settings import settings
from src.answers import ans
from src.settings import settings


def file_settings(pin):
Expand Down

0 comments on commit 64ab5c9

Please sign in to comment.