init-prepare-comments
This commit is contained in:
parent
e8cacac1f8
commit
b144a6ab8b
|
@ -1,7 +1,6 @@
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from sqlalchemy import and_, asc, desc, select, text, func
|
from sqlalchemy import and_, asc, desc, select, text, func
|
||||||
from sqlalchemy.orm import 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
|
||||||
|
@ -203,21 +202,20 @@ async def delete_reaction(_, info, rid):
|
||||||
def prepare_reactions(q, by):
|
def prepare_reactions(q, by):
|
||||||
""" query filters and order """
|
""" query filters and order """
|
||||||
if by.get("shout"):
|
if by.get("shout"):
|
||||||
q = q.filter(Shout.slug == by["shout"])
|
q = q.filter(Reaction.shout == by["shout"])
|
||||||
elif by.get("shouts"):
|
elif by.get("shouts"):
|
||||||
q = q.filter(Shout.slug.in_(by["shouts"]))
|
q = q.filter(Reaction.shout.in_(by["shouts"]))
|
||||||
if by.get("createdBy"):
|
if by.get("createdBy"):
|
||||||
q = q.filter(Reaction.createdBy == by.get("createdBy"))
|
q = q.filter(Reaction.createdBy == by.get("createdBy"))
|
||||||
if by.get("topic"):
|
if by.get("topic"):
|
||||||
q = q.filter(Shout.topics.contains(by["topic"]))
|
q = q.filter(Shout.topics.contains(by["topic"]))
|
||||||
if by.get("body"):
|
if by.get("comment"):
|
||||||
if by["body"] is True:
|
|
||||||
q = q.filter(func.length(Reaction.body) > 0)
|
q = q.filter(func.length(Reaction.body) > 0)
|
||||||
else:
|
if by.get('search', 0) > 2:
|
||||||
q = q.filter(Reaction.body.ilike(f'%{by["body"]}%'))
|
q = q.filter(Reaction.body.ilike(f'%{by["body"]}%'))
|
||||||
if by.get("days"):
|
if by.get("days"):
|
||||||
before = datetime.now() - timedelta(days=int(by["days"]) or 30)
|
after = datetime.now() - timedelta(days=int(by["days"]) or 30)
|
||||||
q = q.filter(Reaction.createdAt > before)
|
q = q.filter(Reaction.createdAt > after)
|
||||||
order_way = asc if by.get("sort", "").startswith("-") else desc
|
order_way = asc if by.get("sort", "").startswith("-") else desc
|
||||||
order_field = by.get("sort") or Reaction.createdAt
|
order_field = by.get("sort") or Reaction.createdAt
|
||||||
q = q.group_by(
|
q = q.group_by(
|
||||||
|
@ -229,14 +227,15 @@ def prepare_reactions(q, by):
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadReactionsBy")
|
@query.field("loadReactionsBy")
|
||||||
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 - filter by slug
|
:shout - filter by slug
|
||||||
:shouts - filer by shouts luglist
|
:shouts - filer by shouts luglist
|
||||||
:createdBy - to filter by author
|
:createdBy - to filter by author
|
||||||
:topic - to filter by topic
|
:topic - to filter by topic
|
||||||
:body - to search by body
|
:search - to search by reactions' body
|
||||||
|
:comment - true if body.length > 0
|
||||||
:days - a number of days ago
|
:days - a number of days ago
|
||||||
:sort - a fieldname to sort desc by default
|
:sort - a fieldname to sort desc by default
|
||||||
}
|
}
|
||||||
|
@ -244,30 +243,15 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
|
||||||
:param offset: int offset in this order
|
:param offset: int offset in this order
|
||||||
:return: Reaction[]
|
:return: Reaction[]
|
||||||
"""
|
"""
|
||||||
user = None
|
|
||||||
try:
|
|
||||||
user = info.context["request"].user
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
q = select(
|
q = select(
|
||||||
Reaction
|
Reaction
|
||||||
).options(
|
|
||||||
selectinload(Reaction.createdBy),
|
|
||||||
selectinload(Reaction.shout)
|
|
||||||
).join(
|
).join(
|
||||||
User, Reaction.createdBy == User.slug
|
User, Reaction.createdBy == User.slug
|
||||||
).join(
|
|
||||||
Shout, Reaction.shout == Shout.slug
|
|
||||||
).where(
|
|
||||||
Reaction.deletedAt.is_(None)
|
|
||||||
)
|
)
|
||||||
q = prepare_reactions(q, by)
|
q = prepare_reactions(q, by).where(Reaction.deletedAt.is_(None))
|
||||||
q = q.limit(limit).offset(offset)
|
q = q.limit(limit).offset(offset)
|
||||||
|
|
||||||
rrr = []
|
rrr = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
# post query stats and author's captions
|
|
||||||
for r in list(map(lambda r: r.Reaction, session.execute(q))):
|
for r in list(map(lambda r: r.Reaction, session.execute(q))):
|
||||||
r.stat = await get_reaction_stat(r.id)
|
r.stat = await get_reaction_stat(r.id)
|
||||||
rrr.append(r)
|
rrr.append(r)
|
||||||
|
|
|
@ -248,13 +248,14 @@ input LoadShoutsOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
input ReactionBy {
|
input ReactionBy {
|
||||||
shout: String
|
shout: String # slug
|
||||||
shouts: [String]
|
shouts: [String]
|
||||||
body: String
|
search: String # fts on body
|
||||||
topic: String
|
comment: Boolean
|
||||||
createdBy: String
|
topic: String # topic.slug
|
||||||
days: Int
|
createdBy: String # user.slug
|
||||||
sort: String
|
days: Int # before
|
||||||
|
sort: String # how to sort, default createdAt
|
||||||
}
|
}
|
||||||
################################### Query
|
################################### Query
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,7 @@ class ReactedStorage:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get_shout_stat(slug):
|
async def get_shout_stat(slug):
|
||||||
return {
|
return {
|
||||||
# TODO
|
"viewed": await ViewedStorage.get_shout(slug),
|
||||||
"viewed": 0, # await ViewedStorage.get_shout(slug),
|
|
||||||
"reacted": len(await ReactedStorage.get_shout(slug)),
|
"reacted": len(await ReactedStorage.get_shout(slug)),
|
||||||
"commented": len(await ReactedStorage.get_comments(slug)),
|
"commented": len(await ReactedStorage.get_comments(slug)),
|
||||||
"rating": await ReactedStorage.get_rating(slug),
|
"rating": await ReactedStorage.get_rating(slug),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user