format and lint orm
This commit is contained in:
@@ -10,11 +10,17 @@ from datetime import datetime
|
||||
from services.auth.users import UserStorage
|
||||
from services.stat.reacted import ReactedStorage
|
||||
|
||||
|
||||
def reactions_follow(user, slug, auto=False):
|
||||
with local_session() as session:
|
||||
fw = session.query(ShoutReactionsFollower).\
|
||||
filter(ShoutReactionsFollower.follower == user.slug, ShoutReactionsFollower.shout == slug).\
|
||||
first()
|
||||
fw = (
|
||||
session.query(ShoutReactionsFollower)
|
||||
.filter(
|
||||
ShoutReactionsFollower.follower == user.slug,
|
||||
ShoutReactionsFollower.shout == slug,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if auto and fw:
|
||||
return
|
||||
elif not auto and fw:
|
||||
@@ -25,17 +31,19 @@ def reactions_follow(user, slug, auto=False):
|
||||
return
|
||||
# print("[resolvers.reactions] was followed before")
|
||||
|
||||
ShoutReactionsFollower.create(
|
||||
follower=user.slug,
|
||||
shout=slug,
|
||||
auto=auto)
|
||||
ShoutReactionsFollower.create(follower=user.slug, shout=slug, auto=auto)
|
||||
|
||||
|
||||
def reactions_unfollow(user, slug):
|
||||
with local_session() as session:
|
||||
following = session.query(ShoutReactionsFollower).\
|
||||
filter(ShoutReactionsFollower.follower == user.slug, ShoutReactionsFollower.shout == slug).\
|
||||
first()
|
||||
following = (
|
||||
session.query(ShoutReactionsFollower)
|
||||
.filter(
|
||||
ShoutReactionsFollower.follower == user.slug,
|
||||
ShoutReactionsFollower.shout == slug,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not following:
|
||||
# print("[resolvers.reactions] was not followed", slug)
|
||||
return
|
||||
@@ -56,7 +64,7 @@ async def create_reaction(_, info, inp):
|
||||
reaction = Reaction.create(**inp)
|
||||
ReactedStorage.increment(reaction.shout, reaction.replyTo)
|
||||
try:
|
||||
reactions_follow(user, inp['shout'], True)
|
||||
reactions_follow(user, inp["shout"], True)
|
||||
except Exception as e:
|
||||
print(f"[resolvers.reactions] error on reactions autofollowing: {e}")
|
||||
|
||||
@@ -76,13 +84,13 @@ async def update_reaction(_, info, inp):
|
||||
return {"error": "invalid reaction id"}
|
||||
if reaction.createdBy != user.slug:
|
||||
return {"error": "access denied"}
|
||||
reaction.body = inp['body']
|
||||
reaction.body = inp["body"]
|
||||
reaction.updatedAt = datetime.now()
|
||||
if reaction.kind != inp['kind']:
|
||||
if reaction.kind != inp["kind"]:
|
||||
# NOTE: change mind detection can be here
|
||||
pass
|
||||
if inp.get('range'):
|
||||
reaction.range = inp.get('range')
|
||||
if inp.get("range"):
|
||||
reaction.range = inp.get("range")
|
||||
session.commit()
|
||||
|
||||
return {"reaction": reaction}
|
||||
@@ -104,29 +112,39 @@ async def delete_reaction(_, info, id):
|
||||
session.commit()
|
||||
return {}
|
||||
|
||||
|
||||
@query.field("reactionsByShout")
|
||||
async def get_shout_reactions(_, info, slug, page, size):
|
||||
offset = page * size
|
||||
reactions = []
|
||||
with local_session() as session:
|
||||
reactions = session.query(Reaction).\
|
||||
filter(Reaction.shout == slug).\
|
||||
limit(size).offset(offset).all()
|
||||
reactions = (
|
||||
session.query(Reaction)
|
||||
.filter(Reaction.shout == slug)
|
||||
.limit(size)
|
||||
.offset(offset)
|
||||
.all()
|
||||
)
|
||||
for r in reactions:
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or 'discours')
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
||||
return reactions
|
||||
|
||||
|
||||
@query.field("reactionsForSlugs")
|
||||
async def get_shout_reactions(_, info, slugs, page, size):
|
||||
offset = page * size
|
||||
reactions = []
|
||||
with local_session() as session:
|
||||
for slug in slugs:
|
||||
reactions += session.query(Reaction).\
|
||||
filter(Reaction.shout == slug).\
|
||||
limit(size).offset(offset).all()
|
||||
for slug in slugs:
|
||||
reactions += (
|
||||
session.query(Reaction)
|
||||
.filter(Reaction.shout == slug)
|
||||
.limit(size)
|
||||
.offset(offset)
|
||||
.all()
|
||||
)
|
||||
for r in reactions:
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or 'discours')
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
||||
return reactions
|
||||
|
||||
|
||||
@@ -135,22 +153,31 @@ async def get_all_reactions(_, info, page=1, size=10):
|
||||
offset = page * size
|
||||
reactions = []
|
||||
with local_session() as session:
|
||||
reactions = session.query(Reaction).\
|
||||
filter(Reaction.deletedAt == None).\
|
||||
order_by(desc("createdAt")).\
|
||||
offset(offset).limit(size)
|
||||
reactions = (
|
||||
session.query(Reaction)
|
||||
.filter(Reaction.deletedAt == None)
|
||||
.order_by(desc("createdAt"))
|
||||
.offset(offset)
|
||||
.limit(size)
|
||||
)
|
||||
for r in reactions:
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or 'discours')
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
||||
reactions = list(reactions)
|
||||
reactions.sort(key=lambda x: x.createdAt, reverse=True)
|
||||
return reactions
|
||||
|
||||
|
||||
@query.field("reactionsByAuthor")
|
||||
async def get_reactions_by_author(_, info, slug, page=1, size=50):
|
||||
offset = page * size
|
||||
reactions = []
|
||||
with local_session() as session:
|
||||
reactions = session.query(Reaction).filter(Reaction.createdBy == slug).limit(size).offset(offset)
|
||||
reactions = (
|
||||
session.query(Reaction)
|
||||
.filter(Reaction.createdBy == slug)
|
||||
.limit(size)
|
||||
.offset(offset)
|
||||
)
|
||||
for r in reactions:
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or 'discours')
|
||||
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
||||
return reactions
|
||||
|
Reference in New Issue
Block a user