fmt
All checks were successful
Deploy on push / deploy (push) Successful in 3m45s

This commit is contained in:
2024-02-24 21:45:38 +03:00
parent 12137eccda
commit eaaace4d28
6 changed files with 263 additions and 215 deletions

View File

@@ -1,14 +1,19 @@
import json
import time
from sqlalchemy import and_, desc, select, or_, distinct, func
from sqlalchemy import desc, select, or_, distinct, func
from sqlalchemy.orm import aliased
from orm.author import Author, AuthorFollower, AuthorRating
from orm.author import Author, AuthorFollower
from orm.shout import ShoutAuthor, ShoutTopic
from orm.topic import Topic
from resolvers.follower import query_follows
from resolvers.stat import get_authors_with_stat, execute_with_ministat, author_follows_authors, author_follows_topics
from resolvers.stat import (
get_authors_with_stat,
execute_with_ministat,
author_follows_authors,
author_follows_topics,
)
from services.auth import login_required
from services.db import local_session
from services.rediscache import redis
@@ -47,7 +52,7 @@ def get_author(_, _info, slug='', author_id=None):
if author_id:
q = select(Author).where(Author.id == author_id)
[author, ] = get_authors_with_stat(q, ratings=True)
[author] = get_authors_with_stat(q, ratings=True)
except Exception as exc:
logger.error(exc)
return author
@@ -67,7 +72,7 @@ async def get_author_by_user_id(user_id: str, ratings=False):
logger.info(f'getting author id for {user_id}')
q = select(Author).filter(Author.user == user_id)
[author, ] = get_authors_with_stat(q, ratings)
[author] = get_authors_with_stat(q, ratings)
except Exception as exc:
logger.error(exc)
return author
@@ -115,7 +120,11 @@ def load_authors_by(_, _info, by, limit, offset):
def get_author_follows(_, _info, slug='', user=None, author_id=None):
with local_session() as session:
if user or slug:
author_id_result = session.query(Author.id).filter(or_(Author.user == user, Author.slug == slug)).first()
author_id_result = (
session.query(Author.id)
.filter(or_(Author.user == user, Author.slug == slug))
.first()
)
author_id = author_id_result[0] if author_id_result else None
if author_id:
follows = query_follows(author_id)
@@ -128,7 +137,11 @@ def get_author_follows(_, _info, slug='', user=None, author_id=None):
def get_author_follows_topics(_, _info, slug='', user=None, author_id=None):
with local_session() as session:
if user or slug:
author_id_result = session.query(Author.id).filter(or_(Author.user == user, Author.slug == slug)).first()
author_id_result = (
session.query(Author.id)
.filter(or_(Author.user == user, Author.slug == slug))
.first()
)
author_id = author_id_result[0] if author_id_result else None
if author_id:
follows = author_follows_authors(author_id)
@@ -141,7 +154,11 @@ def get_author_follows_topics(_, _info, slug='', user=None, author_id=None):
def get_author_follows_authors(_, _info, slug='', user=None, author_id=None):
with local_session() as session:
if user or slug:
author_id_result = session.query(Author.id).filter(or_(Author.user == user, Author.slug == slug)).first()
author_id_result = (
session.query(Author.id)
.filter(or_(Author.user == user, Author.slug == slug))
.first()
)
author_id = author_id_result[0] if author_id_result else None
if author_id:
follows = author_follows_topics(author_id)
@@ -150,42 +167,6 @@ def get_author_follows_authors(_, _info, slug='', user=None, author_id=None):
raise ValueError('Author not found')
@mutation.field('rate_author')
@login_required
def rate_author(_, info, rated_slug, value):
user_id = info.context['user_id']
with local_session() as session:
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
rater = session.query(Author).filter(Author.slug == user_id).first()
if rater and rated_author:
rating: AuthorRating = (
session.query(AuthorRating)
.filter(
and_(
AuthorRating.rater == rater.id,
AuthorRating.author == rated_author.id,
)
)
.first()
)
if rating:
rating.plus = value > 0
session.add(rating)
session.commit()
return {}
else:
try:
rating = AuthorRating(
rater=rater.id, author=rated_author.id, plus=value > 0
)
session.add(rating)
session.commit()
except Exception as err:
return {'error': err}
return {}
def create_author(user_id: str, slug: str, name: str = ''):
with local_session() as session:
new_author = Author(user=user_id, slug=slug, name=name)