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.
This commit is contained in:
Deimos
2018-07-19 18:50:32 -06:00
parent 9347389676
commit 13a3eff340
4 changed files with 22 additions and 30 deletions

View File

@@ -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>

View File

@@ -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' %}

View File

@@ -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>

View File

@@ -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,