core/resolvers/proposals.py

54 lines
2.1 KiB
Python
Raw Normal View History

2024-11-02 10:35:30 +00:00
from sqlalchemy import and_
2024-11-02 10:49:22 +00:00
2024-11-02 10:35:30 +00:00
from orm.rating import is_negative, is_positive
from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout
from services.db import local_session
from utils.diff import apply_diff, get_diff
def handle_proposing(kind: ReactionKind, reply_to: int, shout_id: int):
with local_session() as session:
if is_positive(kind):
2024-11-02 10:49:22 +00:00
replied_reaction = (
session.query(Reaction).filter(Reaction.id == reply_to, Reaction.shout == shout_id).first()
)
2024-11-02 10:35:30 +00:00
2025-05-16 06:23:48 +00:00
if (
replied_reaction
and replied_reaction.kind is ReactionKind.PROPOSE.value
and replied_reaction.quote
):
2024-11-02 10:35:30 +00:00
# patch all the proposals' quotes
proposals = (
session.query(Reaction)
.filter(
and_(
Reaction.shout == shout_id,
Reaction.kind == ReactionKind.PROPOSE.value,
)
)
.all()
)
# patch shout's body
shout = session.query(Shout).filter(Shout.id == shout_id).first()
body = replied_reaction.quote
2024-11-02 10:49:22 +00:00
Shout.update(shout, {body})
2024-11-02 10:35:30 +00:00
session.add(shout)
session.commit()
2024-11-02 10:49:22 +00:00
# реакция содержит цитату -> обновляются все предложения
2024-11-02 10:35:30 +00:00
# (proposals) для соответствующего Shout.
for proposal in proposals:
if proposal.quote:
proposal_diff = get_diff(shout.body, proposal.quote)
proposal_dict = proposal.dict()
proposal_dict["quote"] = apply_diff(replied_reaction.quote, proposal_diff)
Reaction.update(proposal, proposal_dict)
session.add(proposal)
if is_negative(kind):
# TODO: rejection logic
pass