Browse Source

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.
merge-requests/7/head
Deimos 6 years ago
parent
commit
13a3eff340
  1. 9
      tildes/tildes/templates/includes/topic_tags.jinja2
  2. 6
      tildes/tildes/templates/intercooler/topic_tags.jinja2
  3. 10
      tildes/tildes/templates/topic.jinja2
  4. 25
      tildes/tildes/views/api/web/topic.py

9
tildes/tildes/templates/includes/topic_tags.jinja2

@ -0,0 +1,9 @@
<ul class="topic-tags">
{% for tag in topic.tags %}
<li class="label label-topic-tag">
<a href="/~{{ topic.group.path }}?tag={{ tag.replace(' ', '_') }}">{{ tag }}</a>
</li>
{% else %}
<li class="label label-topic-tag">No tags</li>
{% endfor %}
</ul>

6
tildes/tildes/templates/intercooler/topic_tags.jinja2

@ -1,5 +1 @@
<ul class="topic-tags">
{% for tag in topic.tags %}
<li class="label label-topic-tag">{{ tag }}</li>
{% endfor %}
</ul>
{% include 'includes/topic_tags.jinja2' %}

10
tildes/tildes/templates/topic.jinja2

@ -204,15 +204,7 @@
<dl>
<dt>Tags</dt>
<dd>
<ul class="topic-tags">
{% for tag in topic.tags %}
<li class="label label-topic-tag">
<a href="/~{{ topic.group.path }}?tag={{ tag.replace(' ', '_') }}">{{ tag }}</a>
</li>
{% else %}
<li class="label label-topic-tag">No tags</li>
{% endfor %}
</ul>
{% include 'includes/topic_tags.jinja2' %}
</dd>
<dt>Comments</dt>

25
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
if tags:
# split the tag string on commas
split_tags = tags.split(',')
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,

Loading…
Cancel
Save