session-close-fix
This commit is contained in:
parent
3742528e3a
commit
da89b20e5c
|
@ -63,7 +63,8 @@ async def get_author(_, _info, slug="", author_id=0):
|
|||
author_query = author_query.filter(Author.id == author_id)
|
||||
else:
|
||||
raise ValueError("Author not found")
|
||||
lookup_result = local_session().execute(author_query).first()
|
||||
with local_session() as session:
|
||||
lookup_result = session.execute(author_query).first()
|
||||
if lookup_result:
|
||||
[found_author] = lookup_result
|
||||
# logger.debug(found_author)
|
||||
|
@ -150,7 +151,8 @@ async def load_authors_by(_, _info, by, limit, offset):
|
|||
before = int(time.time()) - by["created_at"]
|
||||
authors_query = authors_query.filter(Author.created_at > before)
|
||||
authors_query = authors_query.limit(limit).offset(offset)
|
||||
authors_nostat = local_session().execute(authors_query)
|
||||
with local_session() as session:
|
||||
authors_nostat = session.execute(authors_query)
|
||||
authors = []
|
||||
if authors_nostat:
|
||||
for [a] in authors_nostat:
|
||||
|
@ -185,7 +187,8 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0):
|
|||
author_query = author_query.filter(Author.id == author_id)
|
||||
else:
|
||||
return {"error": "One of slug, user, or author_id must be provided"}
|
||||
result = local_session().execute(author_query)
|
||||
with local_session() as session:
|
||||
result = session.execute(author_query)
|
||||
if result:
|
||||
# logger.debug(result)
|
||||
[author] = result
|
||||
|
@ -310,7 +313,8 @@ async def get_author_followers(_, _info, slug: str):
|
|||
try:
|
||||
author_alias = aliased(Author)
|
||||
author_query = select(author_alias).filter(author_alias.slug == slug)
|
||||
result = local_session().execute(author_query).first()
|
||||
with local_session() as session:
|
||||
result = session.execute(author_query).first()
|
||||
followers = []
|
||||
if result:
|
||||
[author] = result
|
||||
|
|
|
@ -73,8 +73,8 @@ async def follow(_, info, what, slug):
|
|||
_topic_dict = await cache_by_slug(what, slug)
|
||||
|
||||
elif what == "COMMUNITY":
|
||||
# FIXME: when more communities
|
||||
follows = local_session().execute(select(Community))
|
||||
with local_session() as session:
|
||||
follows = session.execute(select(Community))
|
||||
|
||||
elif what == "SHOUT":
|
||||
error = reactions_follow(follower_id, slug)
|
||||
|
@ -122,7 +122,8 @@ async def unfollow(_, info, what, slug):
|
|||
_topic_dict = await cache_by_slug(what, slug)
|
||||
|
||||
elif what == "COMMUNITY":
|
||||
follows = local_session().execute(select(Community))
|
||||
with local_session() as session:
|
||||
follows = session.execute(select(Community))
|
||||
|
||||
elif what == "SHOUT":
|
||||
error = reactions_unfollow(follower_id, slug)
|
||||
|
|
|
@ -52,13 +52,13 @@ def get_topic_shouts_stat(topic_id: int):
|
|||
)
|
||||
)
|
||||
)
|
||||
result = local_session().execute(q).first()
|
||||
with local_session() as session:
|
||||
result = session.execute(q).first()
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
def get_topic_authors_stat(topic_id: int):
|
||||
# authors
|
||||
q = (
|
||||
count_query = (
|
||||
select(func.count(distinct(ShoutAuthor.author)))
|
||||
.select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id))
|
||||
.join(ShoutAuthor, ShoutAuthor.shout == Shout.id)
|
||||
|
@ -70,7 +70,10 @@ def get_topic_authors_stat(topic_id: int):
|
|||
)
|
||||
)
|
||||
)
|
||||
result = local_session().execute(q).first()
|
||||
|
||||
# Выполняем запрос и получаем результат
|
||||
with local_session() as session:
|
||||
result = session.execute(count_query).first()
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
|
@ -79,7 +82,8 @@ def get_topic_followers_stat(topic_id: int):
|
|||
q = select(func.count(distinct(aliased_followers.follower))).filter(
|
||||
aliased_followers.topic == topic_id
|
||||
)
|
||||
result = local_session().execute(q).first()
|
||||
with local_session() as session:
|
||||
result = session.execute(q).first()
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
|
@ -106,8 +110,8 @@ def get_topic_comments_stat(topic_id: int):
|
|||
ShoutTopic.topic == topic_id
|
||||
)
|
||||
q = q.outerjoin(sub_comments, ShoutTopic.shout == sub_comments.c.shout_id)
|
||||
|
||||
result = local_session().execute(q).first()
|
||||
with local_session() as session:
|
||||
result = session.execute(q).first()
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
|
@ -141,7 +145,8 @@ def get_author_authors_stat(author_id: int):
|
|||
aliased_authors.author != author_id,
|
||||
)
|
||||
)
|
||||
result = local_session().execute(q).first()
|
||||
with local_session() as session:
|
||||
result = session.execute(q).first()
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
|
@ -150,7 +155,8 @@ def get_author_followers_stat(author_id: int):
|
|||
q = select(func.count(distinct(aliased_followers.follower))).filter(
|
||||
aliased_followers.author == author_id
|
||||
)
|
||||
result = local_session().execute(q).first()
|
||||
with local_session() as session:
|
||||
result = session.execute(q).first()
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
|
@ -172,8 +178,8 @@ def get_author_comments_stat(author_id: int):
|
|||
.subquery()
|
||||
)
|
||||
q = select(sub_comments.c.comments_count).filter(sub_comments.c.id == author_id)
|
||||
|
||||
result = local_session().execute(q).first()
|
||||
with local_session() as session:
|
||||
result = session.execute(q).first()
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user