This commit is contained in:
111
cache/triggers.py
vendored
111
cache/triggers.py
vendored
@@ -1,85 +1,68 @@
|
||||
from sqlalchemy import event
|
||||
from orm.author import Author, AuthorFollower
|
||||
from orm.reaction import Reaction
|
||||
from orm.shout import Shout, ShoutAuthor
|
||||
from orm.shout import Shout, ShoutAuthor, ShoutReactionsFollower
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from cache.revalidator import revalidation_manager
|
||||
from utils.logger import root_logger as logger
|
||||
|
||||
|
||||
def after_update_handler(mapper, connection, target):
|
||||
"""Обработчик обновления сущности."""
|
||||
entity_type = "authors" if isinstance(target, Author) else "topics" if isinstance(target, Topic) else "shouts"
|
||||
revalidation_manager.mark_for_revalidation(target.id, entity_type)
|
||||
def mark_for_revalidation(entity, *args):
|
||||
"""Отметка сущности для ревалидации."""
|
||||
entity_type = (
|
||||
"authors"
|
||||
if isinstance(entity, Author)
|
||||
else "topics"
|
||||
if isinstance(entity, Topic)
|
||||
else "reactions"
|
||||
if isinstance(entity, Reaction)
|
||||
else "shouts"
|
||||
if isinstance(entity, Shout)
|
||||
else None
|
||||
)
|
||||
if entity_type:
|
||||
revalidation_manager.mark_for_revalidation(entity.id, entity_type)
|
||||
|
||||
|
||||
def after_follower_insert_update_handler(mapper, connection, target):
|
||||
"""Обработчик добавления или обновления подписки."""
|
||||
def after_follower_handler(mapper, connection, target, is_delete=False):
|
||||
"""Обработчик добавления, обновления или удаления подписки."""
|
||||
entity_type = None
|
||||
if isinstance(target, AuthorFollower):
|
||||
# Пометить автора и подписчика для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(target.author_id, "authors")
|
||||
revalidation_manager.mark_for_revalidation(target.follower_id, "authors")
|
||||
entity_type = "authors"
|
||||
elif isinstance(target, TopicFollower):
|
||||
# Пометить тему и подписчика для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(target.topic_id, "topics")
|
||||
revalidation_manager.mark_for_revalidation(target.follower_id, "authors")
|
||||
entity_type = "topics"
|
||||
elif isinstance(target, ShoutReactionsFollower):
|
||||
entity_type = "shouts"
|
||||
|
||||
|
||||
def after_follower_delete_handler(mapper, connection, target):
|
||||
"""Обработчик удаления подписки."""
|
||||
if isinstance(target, AuthorFollower):
|
||||
# Пометить автора и подписчика для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(target.author_id, "authors")
|
||||
revalidation_manager.mark_for_revalidation(target.follower_id, "authors")
|
||||
elif isinstance(target, TopicFollower):
|
||||
# Пометить тему и подписчика для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(target.topic_id, "topics")
|
||||
revalidation_manager.mark_for_revalidation(target.follower_id, "authors")
|
||||
|
||||
|
||||
def after_reaction_update_handler(mapper, connection, reaction):
|
||||
"""Обработчик изменений реакций."""
|
||||
# Пометить shout для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(reaction.shout_id, "shouts")
|
||||
# Пометить автора реакции для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(reaction.created_by, "authors")
|
||||
|
||||
|
||||
def after_shout_author_insert_update_handler(mapper, connection, target):
|
||||
"""Обработчик добавления или обновления авторства публикации."""
|
||||
# Пометить shout и автора для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(target.shout_id, "shouts")
|
||||
revalidation_manager.mark_for_revalidation(target.author_id, "authors")
|
||||
|
||||
|
||||
def after_shout_author_delete_handler(mapper, connection, target):
|
||||
"""Обработчик удаления авторства публикации."""
|
||||
# Пометить shout и автора для ревалидации
|
||||
revalidation_manager.mark_for_revalidation(target.shout_id, "shouts")
|
||||
revalidation_manager.mark_for_revalidation(target.author_id, "authors")
|
||||
if entity_type:
|
||||
revalidation_manager.mark_for_revalidation(
|
||||
target.author_id if entity_type == "authors" else target.topic_id, entity_type
|
||||
)
|
||||
if not is_delete:
|
||||
revalidation_manager.mark_for_revalidation(target.follower_id, "authors")
|
||||
|
||||
|
||||
def events_register():
|
||||
"""Регистрация обработчиков событий для всех сущностей."""
|
||||
event.listen(ShoutAuthor, "after_insert", after_shout_author_insert_update_handler)
|
||||
event.listen(ShoutAuthor, "after_update", after_shout_author_insert_update_handler)
|
||||
event.listen(ShoutAuthor, "after_delete", after_shout_author_delete_handler)
|
||||
event.listen(ShoutAuthor, "after_insert", mark_for_revalidation)
|
||||
event.listen(ShoutAuthor, "after_update", mark_for_revalidation)
|
||||
event.listen(ShoutAuthor, "after_delete", mark_for_revalidation)
|
||||
|
||||
event.listen(AuthorFollower, "after_insert", after_follower_insert_update_handler)
|
||||
event.listen(AuthorFollower, "after_update", after_follower_insert_update_handler)
|
||||
event.listen(AuthorFollower, "after_delete", after_follower_delete_handler)
|
||||
event.listen(TopicFollower, "after_insert", after_follower_insert_update_handler)
|
||||
event.listen(TopicFollower, "after_update", after_follower_insert_update_handler)
|
||||
event.listen(TopicFollower, "after_delete", after_follower_delete_handler)
|
||||
event.listen(Reaction, "after_update", after_reaction_update_handler)
|
||||
event.listen(AuthorFollower, "after_insert", after_follower_handler)
|
||||
event.listen(AuthorFollower, "after_update", after_follower_handler)
|
||||
event.listen(AuthorFollower, "after_delete", lambda *args: after_follower_handler(*args, is_delete=True))
|
||||
|
||||
event.listen(Author, "after_update", after_update_handler)
|
||||
event.listen(Topic, "after_update", after_update_handler)
|
||||
event.listen(Shout, "after_update", after_update_handler)
|
||||
event.listen(
|
||||
Reaction,
|
||||
"after_update",
|
||||
lambda mapper, connection, target: revalidation_manager.mark_for_revalidation(target.shout, "shouts"),
|
||||
)
|
||||
event.listen(TopicFollower, "after_insert", after_follower_handler)
|
||||
event.listen(TopicFollower, "after_update", after_follower_handler)
|
||||
event.listen(TopicFollower, "after_delete", lambda *args: after_follower_handler(*args, is_delete=True))
|
||||
|
||||
event.listen(ShoutReactionsFollower, "after_insert", after_follower_handler)
|
||||
event.listen(ShoutReactionsFollower, "after_update", after_follower_handler)
|
||||
event.listen(ShoutReactionsFollower, "after_delete", lambda *args: after_follower_handler(*args, is_delete=True))
|
||||
|
||||
event.listen(Reaction, "after_update", mark_for_revalidation)
|
||||
event.listen(Author, "after_update", mark_for_revalidation)
|
||||
event.listen(Topic, "after_update", mark_for_revalidation)
|
||||
event.listen(Shout, "after_update", mark_for_revalidation)
|
||||
|
||||
logger.info("Event handlers registered successfully.")
|
||||
|
Reference in New Issue
Block a user