Skip to content

Commit

Permalink
Fix #3542 : Empêche de créer un tag en double
Browse files Browse the repository at this point in the history
  • Loading branch information
artragis authored and gustavi committed Apr 26, 2016
1 parent 87cc41f commit 2f05fb3
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
{% trans "Tags les plus utilisés" %}
</li>
{% for tag in categories.tags %}
<li><a href="{% url 'tutorial:list' %}?tag={{ tag.slug }}">{{ tag }}</a></li>
<li><a href="{% url 'tutorial:list' %}?tag={{ tag.title|urlencode }}">{{ tag }}</a></li>
{% endfor %}
<li><a href="{% url 'content:tags' %}">Tous les tags</a></li>
</ul>
Expand Down Expand Up @@ -272,7 +272,7 @@
{% trans "Tags les plus utilisés" %}
</li>
{% for tag in categories.tags %}
<li><a href="{% url 'article:list' %}?tag={{ tag.slug }}">{{ tag }}</a></li>
<li><a href="{% url 'article:list' %}?tag={{ tag.title|urlencode }}">{{ tag }}</a></li>
{% endfor %}
<li><a href="{% url 'content:tags' %}">Tous les tags</a></li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion templates/tutorialv2/includes/content_item.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ <h3 class="content-title" itemprop="itemListElement">
<ul class="content-tags" itemprop="keywords">
{% for tag in content.tags.all|slice:":3" %}
<li>
<a href="{% url 'content:list' %}?tag={{ tag.slug }}">
<a href="{% url 'content:list' %}?tag={{ tag.title|urlencode }}">
{{ tag.title }}
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion templates/tutorialv2/includes/tags_authors.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% if publishablecontent.tags.all|length > 0 %}
<ul class="taglist" itemprop="keywords">
{% for tag in publishablecontent.tags.all %}
<li><a href="{% url 'content:list' %}?tag={{ tag.slug }}">{{ tag.title }}</a></li>
<li><a href="{% url 'content:list' %}?tag={{ tag.title|urlencode }}">{{ tag.title }}</a></li>
{% endfor %}
</ul>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion templates/tutorialv2/index_online.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<h1 class="ico-after ico-{% if current_content_type == "TUTORIAL" %}tutorials{% elif current_content_type == "ARTICLE" %}articles{% else %}articles{% endif %}" itemprop="name">
{% block headline %}
{% if tag %}
{{ verbose_type_name_plural|title }} : {{ tag }}
{{ verbose_type_name_plural|title }} : {{ tag.title }}
{% elif category %}
{{ verbose_type_name_plural|title }} : {{ category }}
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion templates/tutorialv2/view/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h2 class="ico-after ico-tags">
<div class="content-tags-list">
{% for tag in tags %}
<div class="content-tag">
<a href="{% url 'content:list' %}?tag={{ tag.slug }}">
<a href="{% url 'content:list' %}?tag={{ tag.title|urlencode }}">
<div class="tag-title">{{ tag }}</div>
<div class="tag-count">{{ tag.num_content }} {% trans "contenu" %}{{ tag.num_content|pluralize }}</div>
</a>
Expand Down
12 changes: 6 additions & 6 deletions zds/tutorialv2/models/models_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from zds.tutorialv2.models import TYPE_CHOICES, STATUS_CHOICES
from zds.tutorialv2.models.models_versioned import NotAPublicVersion
from zds.tutorialv2.managers import PublishedContentManager, PublishableContentManager
import logging

ALLOWED_TYPES = ['pdf', 'md', 'html', 'epub', 'zip']

Expand Down Expand Up @@ -527,13 +528,12 @@ def add_tags(self, tag_collection):
"""
for tag in tag_collection:
tag_title = smart_text(tag.strip().lower())
current_tag = Tag.objects.filter(title=tag_title).first()
if len(tag_title) > 0:
if current_tag is None:
current_tag = Tag(title=tag_title)
current_tag.save()

try:
current_tag = Tag.objects.get_from_title(tag_title)
self.tags.add(current_tag)
except ValueError as e:
logging.getLogger("zds.tutorialv2").warn(e)

self.save()


Expand Down
3 changes: 2 additions & 1 deletion zds/tutorialv2/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ def test_add_tags(self):
tuto.add_tags(tags)
tags_len += 2
tuto_tags_len += 2
self.assertEqual(tags_len, len(Tag.objects.all()))
self.assertEqual(tags_len, Tag.objects.count(),
'all tags are "{}"'.format('","'.join([str(t) for t in Tag.objects.all()])))
self.assertEqual(tuto_tags_len, len(tuto.tags.all()))

def tearDown(self):
Expand Down
7 changes: 5 additions & 2 deletions zds/tutorialv2/views/views_published.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,11 @@ def get_queryset(self):
self.category = get_object_or_404(SubCategory, slug=self.request.GET.get('category'))
queryset = queryset.filter(content__subcategory__in=[self.category])
if 'tag' in self.request.GET:
self.tag = get_object_or_404(Tag, slug=self.request.GET.get('tag'))
queryset = queryset.filter(content__tags__in=[self.tag])
self.tag = Tag.objects.filter(title=self.request.GET.get('tag').lower().strip())
if not self.tag:
raise Http404("tag not found " + self.request.GET.get('tag'))
queryset = queryset.filter(content__tags__in=list(self.tag)) # different tags can have same
# slug such as C/C#/C++, as a first version we get all of them
queryset = queryset.extra(select={"count_note": sub_query})
return queryset.order_by('-publication_date')

Expand Down
12 changes: 12 additions & 0 deletions zds/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ class Meta:
positive = models.BooleanField("Est un vote positif", default=True)


class TagManager(models.Manager):
def get_from_title(self, title):
if not title.strip() or not slugify(title.strip().replace("-", "")):
raise ValueError('tag "{}" is not correct'.format(title))
current_tag = self.filter(title=title).first()
if current_tag is None:
current_tag = Tag(title=title)
current_tag.save()
return current_tag


class Tag(models.Model):

"""Set of tags."""
Expand All @@ -296,6 +307,7 @@ class Meta:
verbose_name_plural = 'Tags'
title = models.CharField(max_length=20, verbose_name='Titre')
slug = models.SlugField(max_length=20)
objects = TagManager()

def __unicode__(self):
"""Textual Link Form."""
Expand Down

0 comments on commit 2f05fb3

Please sign in to comment.