From 13a3eff340583648dadf62cdbfd0220a3b8f48d2 Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 19 Jul 2018 18:50:32 -0600 Subject: [PATCH] Prevent adding a TopicLog entry if tags unchanged Previously, an entry would be added to the topic log even if someone didn't actually change the topic's tags at all. This commit fixes that, as well as moving the the template fragment that renders the list into a separate file in includes/ that can be used both for the initial render as well as by intercooler when it updates that section of the page. --- .../templates/includes/topic_tags.jinja2 | 9 +++++++ .../templates/intercooler/topic_tags.jinja2 | 6 +---- tildes/tildes/templates/topic.jinja2 | 10 +------ tildes/tildes/views/api/web/topic.py | 27 ++++++++----------- 4 files changed, 22 insertions(+), 30 deletions(-) create mode 100644 tildes/tildes/templates/includes/topic_tags.jinja2 diff --git a/tildes/tildes/templates/includes/topic_tags.jinja2 b/tildes/tildes/templates/includes/topic_tags.jinja2 new file mode 100644 index 0000000..55b3472 --- /dev/null +++ b/tildes/tildes/templates/includes/topic_tags.jinja2 @@ -0,0 +1,9 @@ + diff --git a/tildes/tildes/templates/intercooler/topic_tags.jinja2 b/tildes/tildes/templates/intercooler/topic_tags.jinja2 index 292d8be..58ba1df 100644 --- a/tildes/tildes/templates/intercooler/topic_tags.jinja2 +++ b/tildes/tildes/templates/intercooler/topic_tags.jinja2 @@ -1,5 +1 @@ - +{% include 'includes/topic_tags.jinja2' %} diff --git a/tildes/tildes/templates/topic.jinja2 b/tildes/tildes/templates/topic.jinja2 index 15b7a20..6859134 100644 --- a/tildes/tildes/templates/topic.jinja2 +++ b/tildes/tildes/templates/topic.jinja2 @@ -204,15 +204,7 @@
Tags
-
    - {% for tag in topic.tags %} -
  • - {{ tag }} -
  • - {% else %} -
  • No tags
  • - {% endfor %} -
+ {% include 'includes/topic_tags.jinja2' %}
Comments
diff --git a/tildes/tildes/views/api/web/topic.py b/tildes/tildes/views/api/web/topic.py index b97d565..6e33b66 100644 --- a/tildes/tildes/views/api/web/topic.py +++ b/tildes/tildes/views/api/web/topic.py @@ -14,7 +14,7 @@ from tildes.models.log import LogTopic from tildes.models.topic import Topic, TopicVote from tildes.schemas.group import GroupSchema from tildes.schemas.topic import TopicSchema -from tildes.views import IC_EMPTY, IC_NOOP +from tildes.views import IC_NOOP from tildes.views.decorators import ic_view_config @@ -160,28 +160,23 @@ def tag_topic(request: Request, tags: str) -> dict: """Apply tags to a topic with Intercooler.""" topic = request.context - if not tags: - request.db_session.add( - LogTopic( - LogEventType.TOPIC_TAG, - request, - topic, - info={'old': topic.tags, 'new': []}, - ), - ) - topic.tags = [] - return IC_EMPTY - - # split the tag string on commas - split_tags = tags.split(',') + if tags: + # split the tag string on commas + new_tags = tags.split(',') + else: + new_tags = [] old_tags = topic.tags try: - topic.tags = split_tags + topic.tags = new_tags except ValidationError: raise ValidationError({'tags': ['Invalid tags']}) + # if tags weren't changed, don't add a log entry or update page + if set(topic.tags) == set(old_tags): + return IC_NOOP + request.db_session.add( LogTopic( LogEventType.TOPIC_TAG,