granian+precommit

This commit is contained in:
2024-01-25 22:41:27 +03:00
parent ad3fd32a6e
commit 4a5f1d634a
35 changed files with 835 additions and 764 deletions

View File

@@ -1,4 +1,4 @@
from sqlalchemy import bindparam, distinct, or_, literal
from sqlalchemy import bindparam, distinct, or_
from sqlalchemy.orm import aliased, joinedload
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
from starlette.exceptions import HTTPException
@@ -7,32 +7,32 @@ from orm.author import Author, AuthorFollower
from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutVisibility
from orm.topic import Topic, TopicFollower
from resolvers.reaction import add_stat_columns
from resolvers.topic import get_random_topic
from services.auth import login_required
from services.db import local_session
from services.schema import query
from services.search import SearchService
from services.viewed import ViewedStorage
from resolvers.topic import get_random_topic
from resolvers.reaction import add_stat_columns
def apply_filters(q, filters, author_id=None): # noqa: C901
if filters.get("reacted") and author_id:
def apply_filters(q, filters, author_id=None):
if filters.get('reacted') and author_id:
q.join(Reaction, Reaction.created_by == author_id)
by_published = filters.get("published")
by_published = filters.get('published')
if by_published:
q = q.filter(Shout.visibility == ShoutVisibility.PUBLIC.value)
by_layouts = filters.get("layouts")
by_layouts = filters.get('layouts')
if by_layouts:
q = q.filter(Shout.layout.in_(by_layouts))
by_author = filters.get("author")
by_author = filters.get('author')
if by_author:
q = q.filter(Shout.authors.any(slug=by_author))
by_topic = filters.get("topic")
by_topic = filters.get('topic')
if by_topic:
q = q.filter(Shout.topics.any(slug=by_topic))
by_after = filters.get("after")
by_after = filters.get('after')
if by_after:
ts = int(by_after)
q = q.filter(Shout.created_at > ts)
@@ -40,7 +40,7 @@ def apply_filters(q, filters, author_id=None): # noqa: C901
return q
@query.field("get_shout")
@query.field('get_shout')
async def get_shout(_, _info, slug=None, shout_id=None):
with local_session() as session:
q = select(Shout).options(
@@ -61,13 +61,19 @@ async def get_shout(_, _info, slug=None, shout_id=None):
try:
results = session.execute(q).first()
if results:
[shout, commented_stat, likes_stat, dislikes_stat, _last_comment] = results
[
shout,
commented_stat,
likes_stat,
dislikes_stat,
_last_comment,
] = results
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug),
'viewed': await ViewedStorage.get_shout(shout.slug),
# "reacted": reacted_stat,
"commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
'commented': commented_stat,
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
}
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
@@ -78,7 +84,11 @@ async def get_shout(_, _info, slug=None, shout_id=None):
session.query(Topic.slug)
.join(
ShoutTopic,
and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True),
and_(
ShoutTopic.topic == Topic.id,
ShoutTopic.shout == shout.id,
ShoutTopic.main == True,
),
)
.first()
)
@@ -87,10 +97,10 @@ async def get_shout(_, _info, slug=None, shout_id=None):
shout.main_topic = main_topic[0]
return shout
except Exception:
raise HTTPException(status_code=404, detail=f"shout {slug or shout_id} not found")
raise HTTPException(status_code=404, detail=f'shout {slug or shout_id} not found')
@query.field("load_shouts_by")
@query.field('load_shouts_by')
async def load_shouts_by(_, _info, options):
"""
:param options: {
@@ -126,31 +136,39 @@ async def load_shouts_by(_, _info, options):
q = add_stat_columns(q, aliased_reaction)
# filters
q = apply_filters(q, options.get("filters", {}))
q = apply_filters(q, options.get('filters', {}))
# group
q = q.group_by(Shout.id)
# order
order_by = options.get("order_by", Shout.published_at)
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
order_by = options.get('order_by', Shout.published_at)
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
q = q.order_by(nulls_last(query_order_by))
# limit offset
offset = options.get("offset", 0)
limit = options.get("limit", 10)
offset = options.get('offset', 0)
limit = options.get('limit', 10)
q = q.limit(limit).offset(offset)
shouts = []
with local_session() as session:
for [shout, commented_stat, likes_stat, dislikes_stat, _last_comment] in session.execute(q).unique():
for [
shout,
commented_stat,
likes_stat,
dislikes_stat,
_last_comment,
] in session.execute(q).unique():
main_topic = (
session.query(Topic.slug)
.join(
ShoutTopic,
and_(
ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True
), # noqa: E712
ShoutTopic.topic == Topic.id,
ShoutTopic.shout == shout.id,
ShoutTopic.main == True,
),
)
.first()
)
@@ -158,19 +176,19 @@ async def load_shouts_by(_, _info, options):
if main_topic:
shout.main_topic = main_topic[0]
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug),
"commented": commented_stat,
"rating": int(likes_stat) - int(dislikes_stat),
'viewed': await ViewedStorage.get_shout(shout.slug),
'commented': commented_stat,
'rating': int(likes_stat) - int(dislikes_stat),
}
shouts.append(shout)
return shouts
@query.field("load_shouts_drafts")
@query.field('load_shouts_drafts')
@login_required
async def load_shouts_drafts(_, info):
user_id = info.context["user_id"]
user_id = info.context['user_id']
q = (
select(Shout)
@@ -193,7 +211,11 @@ async def load_shouts_drafts(_, info):
session.query(Topic.slug)
.join(
ShoutTopic,
and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True),
and_(
ShoutTopic.topic == Topic.id,
ShoutTopic.shout == shout.id,
ShoutTopic.main == True,
),
)
.first()
)
@@ -205,10 +227,10 @@ async def load_shouts_drafts(_, info):
return shouts
@query.field("load_shouts_feed")
@query.field('load_shouts_feed')
@login_required
async def load_shouts_feed(_, info, options):
user_id = info.context["user_id"]
user_id = info.context['user_id']
shouts = []
with local_session() as session:
@@ -221,7 +243,9 @@ async def load_shouts_feed(_, info, options):
select(Shout.id)
.where(Shout.id == ShoutAuthor.shout)
.where(Shout.id == ShoutTopic.shout)
.where((ShoutAuthor.author.in_(reader_followed_authors)) | (ShoutTopic.topic.in_(reader_followed_topics)))
.where(
(ShoutAuthor.author.in_(reader_followed_authors)) | (ShoutTopic.topic.in_(reader_followed_topics))
)
)
q = (
@@ -230,18 +254,24 @@ async def load_shouts_feed(_, info, options):
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None), Shout.id.in_(subquery)))
.where(
and_(
Shout.published_at.is_not(None),
Shout.deleted_at.is_(None),
Shout.id.in_(subquery),
)
)
)
aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, aliased_reaction)
q = apply_filters(q, options.get("filters", {}), reader.id)
q = apply_filters(q, options.get('filters', {}), reader.id)
order_by = options.get("order_by", Shout.published_at)
order_by = options.get('order_by', Shout.published_at)
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
offset = options.get("offset", 0)
limit = options.get("limit", 10)
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
offset = options.get('offset', 0)
limit = options.get('limit', 10)
q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset)
@@ -252,7 +282,11 @@ async def load_shouts_feed(_, info, options):
session.query(Topic.slug)
.join(
ShoutTopic,
and_(ShoutTopic.topic == Topic.id, ShoutTopic.shout == shout.id, ShoutTopic.main == True),
and_(
ShoutTopic.topic == Topic.id,
ShoutTopic.shout == shout.id,
ShoutTopic.main == True,
),
)
.first()
)
@@ -260,20 +294,20 @@ async def load_shouts_feed(_, info, options):
if main_topic:
shout.main_topic = main_topic[0]
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug),
"reacted": reacted_stat,
"commented": commented_stat,
'viewed': await ViewedStorage.get_shout(shout.slug),
'reacted': reacted_stat,
'commented': commented_stat,
}
shouts.append(shout)
return shouts
@query.field("load_shouts_search")
@query.field('load_shouts_search')
async def load_shouts_search(_, _info, text, limit=50, offset=0):
if text and len(text) > 2:
results = await SearchService.search(text, limit, offset)
results_dict = {r["slug"]: r for r in results}
results_dict = {r['slug']: r for r in results}
# print(results_dict)
q = (
@@ -289,13 +323,13 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
with local_session() as session:
results = list(session.execute(q).unique())
# print(results)
print(f"[resolvers.reader] searched, preparing {len(results)} results")
print(f'[resolvers.reader] searched, preparing {len(results)} results')
for x in results:
shout = x[0]
shout_slug = shout.dict().get("slug", "")
score = results_dict.get(shout_slug, {}).get("score", 0)
shout_slug = shout.dict().get('slug', '')
score = results_dict.get(shout_slug, {}).get('score', 0)
shout_data = shout.dict() # Convert the Shout instance to a dictionary
shout_data["score"] = score # Add the score to the dictionary
shout_data['score'] = score # Add the score to the dictionary
shouts_data.append(shout_data)
return shouts_data
@@ -304,7 +338,7 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
@login_required
@query.field("load_shouts_unrated")
@query.field('load_shouts_unrated')
async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
q = (
select(Shout)
@@ -320,7 +354,7 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
),
)
.outerjoin(Author, Author.user == bindparam("user_id"))
.outerjoin(Author, Author.user == bindparam('user_id'))
.where(
and_(
Shout.deleted_at.is_(None),
@@ -337,7 +371,7 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
q = add_stat_columns(q, aliased_reaction)
q = q.group_by(Shout.id).order_by(func.random()).limit(limit).offset(offset)
user_id = info.context.get("user_id")
user_id = info.context.get('user_id')
if user_id:
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
@@ -350,19 +384,24 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
async def get_shouts_from_query(q, author_id=None):
shouts = []
with local_session() as session:
for [shout,commented_stat, likes_stat, dislikes_stat, last_comment] in session.execute(
q, {"author_id": author_id}
).unique():
for [
shout,
commented_stat,
likes_stat,
dislikes_stat,
_last_comment,
] in session.execute(q, {'author_id': author_id}).unique():
shouts.append(shout)
shout.stat = {
"viewed": await ViewedStorage.get_shout(shout_slug=shout.slug),
"commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
'viewed': await ViewedStorage.get_shout(shout_slug=shout.slug),
'commented': commented_stat,
'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
}
return shouts
@query.field("load_shouts_random_top")
@query.field('load_shouts_random_top')
async def load_shouts_random_top(_, _info, options):
"""
:param _
@@ -383,21 +422,22 @@ async def load_shouts_random_top(_, _info, options):
subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
subquery = apply_filters(subquery, options.get("filters", {}))
subquery = subquery.group_by(Shout.id).order_by(desc(
subquery = apply_filters(subquery, options.get('filters', {}))
subquery = subquery.group_by(Shout.id).order_by(
desc(
func.sum(
case(
(Reaction.kind == ReactionKind.LIKE.value, 1),
(Reaction.kind == ReactionKind.AGREE.value, 1),
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
(Reaction.kind == ReactionKind.DISAGREE.value, -1),
else_=0
else_=0,
)
)
)
)
random_limit = options.get("random_limit")
random_limit = options.get('random_limit')
if random_limit:
subquery = subquery.limit(random_limit)
@@ -412,7 +452,7 @@ async def load_shouts_random_top(_, _info, options):
aliased_reaction = aliased(Reaction)
q = add_stat_columns(q, aliased_reaction)
limit = options.get("limit", 10)
limit = options.get('limit', 10)
q = q.group_by(Shout.id).order_by(func.random()).limit(limit)
# print(q.compile(compile_kwargs={"literal_binds": True}))
@@ -420,7 +460,7 @@ async def load_shouts_random_top(_, _info, options):
return await get_shouts_from_query(q)
@query.field("load_shouts_random_topic")
@query.field('load_shouts_random_topic')
async def load_shouts_random_topic(_, info, limit: int = 10):
topic = get_random_topic()
shouts = []
@@ -431,7 +471,13 @@ async def load_shouts_random_topic(_, info, limit: int = 10):
joinedload(Shout.authors),
joinedload(Shout.topics),
)
.filter(and_(Shout.deleted_at.is_(None), Shout.visibility == ShoutVisibility.PUBLIC.value, Shout.topics.any(slug=topic.slug)))
.filter(
and_(
Shout.deleted_at.is_(None),
Shout.visibility == ShoutVisibility.PUBLIC.value,
Shout.topics.any(slug=topic.slug),
)
)
)
aliased_reaction = aliased(Reaction)
@@ -441,4 +487,4 @@ async def load_shouts_random_topic(_, info, limit: int = 10):
shouts = get_shouts_from_query(q)
return {"topic": topic, "shouts": shouts}
return {'topic': topic, 'shouts': shouts}