load_reactions_by-fix

This commit is contained in:
tonyrewin 2022-11-22 18:32:58 +03:00
parent e34f54f865
commit 9740d31867
2 changed files with 41 additions and 33 deletions

View File

@ -1,6 +1,6 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from sqlalchemy import and_, desc, select, text, func from sqlalchemy import and_, asc, desc, select, text, func, selectinload
from auth.authenticate import login_required from auth.authenticate import login_required
from base.orm import local_session from base.orm import local_session
from base.resolvers import mutation, query from base.resolvers import mutation, query
@ -199,29 +199,31 @@ async def delete_reaction(_, info, rid):
return {} return {}
def prepare_reactions(q, by, user=None): def prepare_reactions(q, by):
""" query filters and order """
if by.get("shout"): if by.get("shout"):
q = q.filter(Shout.slug == by["shout"]) q = q.filter(Shout.slug == by["shout"])
else: elif by.get("shouts"):
if by.get("reacted"): q = q.filter(Shout.slug.in_(by["shouts"]))
if user: if by.get("createdBy"):
q = q.filter(Reaction.createdBy == user.slug) q = q.filter(Reaction.createdBy == by.get("createdBy"))
if by.get("author"): if by.get("topic"):
q = q.filter(Reaction.createdBy == by["author"]) q = q.filter(Shout.topics.contains(by["topic"]))
if by.get("topic"): if by.get("body"):
q = q.filter(Shout.topics.contains(by["topic"])) if by["body"] is True:
if by.get("body"): q = q.filter(func.length(Reaction.body) > 0)
if by["body"] is True: else:
q = q.filter(func.length(Reaction.body) > 0) q = q.filter(Reaction.body.ilike(f'%{by["body"]}%'))
else: if by.get("days"):
q = q.filter(Reaction.body.ilike(f'%{by["body"]}%')) before = datetime.now() - timedelta(days=int(by["days"]) or 30)
if by.get("days"): q = q.filter(Reaction.createdAt > before)
before = datetime.now() - timedelta(days=int(by["days"]) or 30) order_way = asc if by.get("sort", "").startswith("-") else desc
q = q.filter(Reaction.createdAt > before) order_field = by.get("sort") or Reaction.createdAt
q = q.group_by(
q = q.group_by(Reaction.id).order_by( Reaction.id
desc(by.get("sort") or Reaction.createdAt) ).order_by(
) order_way(order_field)
)
return q return q
@ -229,12 +231,13 @@ def prepare_reactions(q, by, user=None):
async def load_reactions_by(_, info, by, limit=50, offset=0): async def load_reactions_by(_, info, by, limit=50, offset=0):
""" """
:param by: { :param by: {
shout: 'some-slug' :shout - filter by slug
author: 'discours', :shouts - filer by shouts luglist
topic: 'culture', :createdBy - to filter by author
body: 'something else' | true, :topic - to filter by topic
sort: 'rating' | 'comments' | 'reacted' | 'views', :body - to search by body
days: 30 :days - a number of days ago
:sort - a fieldname to sort desc by default
} }
:param limit: int amount of shouts :param limit: int amount of shouts
:param offset: int offset in this order :param offset: int offset in this order
@ -246,9 +249,15 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
except Exception: except Exception:
pass pass
q = select(Reaction).join( q = select(
Shout, Reaction
Reaction.shout == Shout.slug ).options(
selectinload(Reaction.createdBy),
selectinload(Reaction.shout)
).join(
User, Reaction.createdBy == User.slug
).join(
Shout, Reaction.shout == Shout.slug
).where( ).where(
Reaction.deletedAt.is_(None) Reaction.deletedAt.is_(None)
) )

View File

@ -252,8 +252,7 @@ input ReactionBy {
shouts: [String] shouts: [String]
body: String body: String
topic: String topic: String
author: String createdBy: String
order: String
days: Int days: Int
sort: String sort: String
} }