create-reaction-fix-4
All checks were successful
Deploy on push / deploy (push) Successful in 6s

This commit is contained in:
Untone 2024-11-02 22:34:20 +03:00
parent bcb602d3cf
commit 6d61e038e7
2 changed files with 32 additions and 43 deletions

View File

@ -25,7 +25,7 @@ from utils.logger import root_logger as logger
@mutation.field("follow") @mutation.field("follow")
@login_required @login_required
async def follow(_, info, what, slug): async def follow(_, info, what, slug="", entity_id=0):
logger.debug("Начало выполнения функции 'follow'") logger.debug("Начало выполнения функции 'follow'")
user_id = info.context.get("user_id") user_id = info.context.get("user_id")
follower_dict = info.context.get("author") follower_dict = info.context.get("author")
@ -50,8 +50,6 @@ async def follow(_, info, what, slug):
entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what] entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what]
entity_type = what.lower() entity_type = what.lower()
entity_id = None
entity_dict = None entity_dict = None
try: try:
@ -63,7 +61,8 @@ async def follow(_, info, what, slug):
if not entity: if not entity:
logger.warning(f"{what.lower()} не найден по slug: {slug}") logger.warning(f"{what.lower()} не найден по slug: {slug}")
return {"error": f"{what.lower()} not found"} return {"error": f"{what.lower()} not found"}
entity_id = entity.id if not entity_id and entity:
entity_id = entity.id
entity_dict = entity.dict() entity_dict = entity.dict()
logger.debug(f"entity_id: {entity_id}, entity_dict: {entity_dict}") logger.debug(f"entity_id: {entity_id}, entity_dict: {entity_dict}")
@ -108,7 +107,7 @@ async def follow(_, info, what, slug):
@mutation.field("unfollow") @mutation.field("unfollow")
@login_required @login_required
async def unfollow(_, info, what, slug): async def unfollow(_, info, what, slug="", entity_id=0):
logger.debug("Начало выполнения функции 'unfollow'") logger.debug("Начало выполнения функции 'unfollow'")
user_id = info.context.get("user_id") user_id = info.context.get("user_id")
follower_dict = info.context.get("author") follower_dict = info.context.get("author")
@ -134,9 +133,6 @@ async def unfollow(_, info, what, slug):
entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what] entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what]
entity_type = what.lower() entity_type = what.lower()
# logger.debug(f"entity_class: {entity_class}, follower_class: {follower_class}, entity_type: {entity_type}")
entity_id = None
follows = [] follows = []
error = None error = None
@ -148,8 +144,9 @@ async def unfollow(_, info, what, slug):
if not entity: if not entity:
logger.warning(f"{what.lower()} не найден по slug: {slug}") logger.warning(f"{what.lower()} не найден по slug: {slug}")
return {"error": f"{what.lower()} not found"} return {"error": f"{what.lower()} not found"}
entity_id = entity.id if entity and not entity_id:
logger.debug(f"entity_id: {entity_id}") entity_id = entity.id
logger.debug(f"entity_id: {entity_id}")
sub = ( sub = (
session.query(follower_class) session.query(follower_class)

View File

@ -6,7 +6,7 @@ from sqlalchemy.orm import aliased
from orm.author import Author from orm.author import Author
from orm.rating import PROPOSAL_REACTIONS, RATING_REACTIONS, is_negative, is_positive from orm.rating import PROPOSAL_REACTIONS, RATING_REACTIONS, is_negative, is_positive
from orm.reaction import Reaction, ReactionKind from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout from orm.shout import Shout, ShoutAuthor
from resolvers.follower import follow from resolvers.follower import follow
from resolvers.proposals import handle_proposing from resolvers.proposals import handle_proposing
from resolvers.stat import update_author_stat from resolvers.stat import update_author_stat
@ -286,42 +286,34 @@ async def create_reaction(_, info, reaction):
try: try:
with local_session() as session: with local_session() as session:
shout = session.query(Shout).filter(Shout.id == shout_id).first() authors = session.query(ShoutAuthor.author).filter(ShoutAuthor.shout == shout_id).scalar()
authors = [a.id for a in shout.authors] is_author = (
is_author = bool(list(filter(lambda x: x == int(author_id), authors))) bool(list(filter(lambda x: x == int(author_id), authors))) if isinstance(authors, list) else False
logger.debug(f"Loaded shout: {shout and shout.id}") )
reaction_input["created_by"] = author_id
kind = reaction_input.get("kind")
if shout: # handle ratings
shout_dict = shout.dict() if kind in RATING_REACTIONS:
reaction_input["created_by"] = author_id logger.debug(f"creating rating reaction: {kind}")
kind = reaction_input.get( error_result = prepare_new_rating(reaction_input, shout_id, session, author_id)
"kind", ReactionKind.COMMENT.value if isinstance(reaction_input.get("body"), str) else None if error_result:
) logger.error(f"Rating preparation error: {error_result}")
return error_result
logger.debug(f"Reaction kind: {kind}") # handle all reactions
rdict = await _create_reaction(session, shout_id, is_author, author_id, reaction_input)
logger.debug(f"Created reaction result: {rdict}")
if kind in RATING_REACTIONS: # follow if liked
error_result = prepare_new_rating(reaction_input, shout_id, session, author_id) if kind == ReactionKind.LIKE.value:
if error_result: try:
logger.error(f"Rating preparation error: {error_result}") follow(None, info, "shout", shout_id=shout_id)
return error_result except Exception:
pass
logger.debug(f"Creating reaction for shout: {shout_dict['id']}") rdict["created_by"] = author_dict
rdict = await _create_reaction(session, shout_id, is_author, author_id, reaction_input) return {"reaction": rdict}
logger.debug(f"Created reaction result: {rdict}")
# follow if liked
if kind == ReactionKind.LIKE.value:
try:
follow(None, info, "shout", shout_dict["slug"])
except Exception:
pass
rdict["created_by"] = author_dict
return {"reaction": rdict}
else:
logger.error(f"Shout not found with ID: {shout_id}")
return {"error": "Shout not found"}
except Exception as e: except Exception as e:
import traceback import traceback