Skip to content

Commit

Permalink
Évite les tags répétés plusieurs fois (#3601)
Browse files Browse the repository at this point in the history
* augmente la taille des tags et évite qu'ils soient recréés quand ils sont trop grands.

* ajoute un test unitaire

* Fix l'unicite des tags

* Corrige l'unicite des tags (again)

* change le processus de création des tags lors du sujet de beta

* fix indexerror

* pep8

* pep8

* fix indexerror

* remove useless test

* fix test

* pep8
  • Loading branch information
artragis authored and gustavi committed May 12, 2016
1 parent c150c5f commit 2b0663f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 50 deletions.
14 changes: 5 additions & 9 deletions zds/tutorialv2/management/commands/migrate_to_zep25.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.core.management.base import BaseCommand
from django.db import transaction
from django.shortcuts import get_object_or_404
from django.utils.encoding import smart_text
from django.utils.text import slugify

from zds import settings
Expand Down Expand Up @@ -33,16 +32,13 @@ def categories_to_tags(self):
for content in contents:
categories = content.subcategory.all()
for cat in categories:
tag_title = smart_text(cat.slug.replace('-', ' ').strip().lower())
current_tag = Tag.objects.filter(title=tag_title).first()
if current_tag is None:
current_tag = Tag(title=tag_title)
current_tag.save()
self.stdout.write('[ZEP-25] : Tag "{}" added'.format(current_tag))
n += 1
# do not add "autre" tag (useless)
if current_tag != 'autre':
if cat.strip() != 'autre':
current_tag, created = Tag.objects.get_or_create(title=cat.lower())
content.tags.add(current_tag)
if created:
self.stdout.write('[ZEP-25] : Tag "{}" added'.format(current_tag))
n += 1
content.save()
self.stdout.write('[ZEP-25] : {} new tag(s)'.format(n))

Expand Down
4 changes: 1 addition & 3 deletions zds/tutorialv2/models/models_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from django.core.urlresolvers import reverse
from django.db import models
from django.http import Http404
from django.utils.encoding import smart_text
from django.utils.http import urlencode
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import pre_delete, post_delete
Expand Down Expand Up @@ -527,9 +526,8 @@ def add_tags(self, tag_collection):
:type tag_collection: list
"""
for tag in tag_collection:
tag_title = smart_text(tag.strip().lower())
try:
current_tag = Tag.objects.get_from_title(tag_title)
current_tag, created = Tag.objects.get_or_create(title=tag.lower())
self.tags.add(current_tag)
except ValueError as e:
logging.getLogger("zds.tutorialv2").warn(e)
Expand Down
9 changes: 5 additions & 4 deletions zds/tutorialv2/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.encoding import smart_text

from zds.forum.factories import ForumFactory, CategoryFactory
from zds.forum.models import Topic, Post, TopicRead
Expand All @@ -27,7 +26,7 @@
from zds.tutorialv2.models.models_database import PublishableContent, Validation, PublishedContent, ContentReaction, \
ContentRead
from zds.tutorialv2.publication_utils import publish_content, Publicator, PublicatorRegistery
from zds.utils.models import HelpWriting, Alert
from zds.utils.models import HelpWriting, Alert, Tag
from zds.utils.factories import HelpWritingFactory
from zds.utils.templatetags.interventions import interventions_topics

Expand Down Expand Up @@ -582,7 +581,9 @@ def test_beta_workflow(self):
username=self.user_author.username,
password='hostel77'),
True)

sometag = Tag(title="randomizeit")
sometag.save()
self.tuto.tags.add(sometag)
# create second author and add to tuto
second_author = ProfileFactory().user
self.tuto.authors.add(second_author)
Expand All @@ -608,7 +609,7 @@ def test_beta_workflow(self):
self.assertIsNotNone(TopicAnswerSubscription.objects.get_existing(self.user_author, beta_topic, is_active=True))
self.assertEqual(Post.objects.filter(topic=beta_topic).count(), 1)
self.assertEqual(beta_topic.tags.count(), 1)
self.assertEqual(beta_topic.tags.first().title, smart_text(self.subcategory.title).lower()[:20])
self.assertEqual(beta_topic.tags.first().title, sometag.title)

# test if second author follow the topic
self.assertIsNotNone(TopicAnswerSubscription.objects.get_existing(second_author, beta_topic, is_active=True))
Expand Down
25 changes: 7 additions & 18 deletions zds/tutorialv2/views/views_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from django.shortcuts import redirect, get_object_or_404
from django.template.loader import render_to_string
from django.utils.decorators import method_decorator
from django.utils.encoding import smart_text
from django.utils.translation import string_concat
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView, DeleteView, RedirectView
Expand All @@ -48,7 +47,7 @@
default_slug_pool, BadArchiveError, InvalidSlugError
from zds.utils.forums import send_post, lock_topic, create_topic, unlock_topic
from zds.utils.models import Licence
from zds.utils.models import Tag, HelpWriting
from zds.utils.models import HelpWriting
from zds.utils.mps import send_mp
from zds.utils.paginator import ZdSPagingListView

Expand Down Expand Up @@ -1278,26 +1277,16 @@ class ManageBetaContent(LoggedWithReadWriteHability, SingleContentFormViewMixin)
action = None

def _get_all_tags(self):
all_tags = []
categories = self.object.subcategory.all()
names = [smart_text(category.title).lower() for category in categories]
existing_tags = Tag.objects.filter(title__in=names).all()
existing_tags_names = [tag.title for tag in existing_tags]
unexisting_tags = list(set(names) - set(existing_tags_names))
for tag in unexisting_tags:
new_tag = Tag()
new_tag.title = tag[:20]
new_tag.save()
all_tags.append(new_tag)
all_tags += existing_tags
return all_tags
return list(self.object.tags.all())

def _create_beta_topic(self, msg, beta_version, _type, tags):
topic_title = beta_version.title
tags = "[beta][{}]".format(_type)
_tags = "[beta][{}]".format(_type)
i = 0
while len(topic_title) + len(tags) + len(tags[i]) + 2 < Topic._meta.get_field("title").max_length:
tags += '[{}]'.format(tags[i])
max_len = Topic._meta.get_field("title").max_length

while i < len(tags) and len(topic_title) + len(_tags) + len(tags[i].title) + 2 < max_len:
_tags += '[{}]'.format(tags[i])
i += 1
forum = get_object_or_404(Forum, pk=settings.ZDS_APP['forum']['beta_forum_id'])
topic = create_topic(request=self.request,
Expand Down
3 changes: 1 addition & 2 deletions zds/utils/management/commands/load_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ def load_tags(cli, size, fake):
tps1 = time.time()
for i in range(0, nb_tags):
title = fake.word()
tag = Tag(title=title, slug=slugify(title))
tag.save()
tag, created = Tag.objects.get_or_create(title=title.lower())
sys.stdout.write(" Tag {}/{} \r".format(i + 1, nb_tags))
sys.stdout.flush()
tps2 = time.time()
Expand Down
24 changes: 24 additions & 0 deletions zds/utils/migrations/0006_auto_20160509_1633.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('utils', '0005_commentvote'),
]

operations = [
migrations.AlterField(
model_name='tag',
name='slug',
field=models.SlugField(max_length=30),
),
migrations.AlterField(
model_name='tag',
name='title',
field=models.CharField(max_length=30, verbose_name=b'Titre'),
),
]
24 changes: 24 additions & 0 deletions zds/utils/migrations/0007_auto_20160511_2153.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('utils', '0006_auto_20160509_1633'),
]

operations = [
migrations.AlterField(
model_name='tag',
name='slug',
field=models.SlugField(max_length=30, verbose_name=b'Slug'),
),
migrations.AlterField(
model_name='tag',
name='title',
field=models.CharField(unique=True, max_length=30, verbose_name=b'Titre', db_index=True),
),
]
19 changes: 5 additions & 14 deletions zds/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,27 +287,16 @@ 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."""

class Meta:
verbose_name = 'Tag'
verbose_name_plural = 'Tags'
title = models.CharField(max_length=20, verbose_name='Titre')
slug = models.SlugField(max_length=20)
objects = TagManager()

title = models.CharField('Titre', max_length=30, unique=True, db_index=True)
slug = models.SlugField('Slug', max_length=30)

def __unicode__(self):
"""Textual Link Form."""
Expand All @@ -317,6 +306,8 @@ def get_absolute_url(self):
return reverse('topic-tag-find', kwargs={'tag_pk': self.pk, 'tag_slug': self.slug})

def save(self, *args, **kwargs):
if not self.title.strip() or not slugify(self.title.strip().replace('-', '')):
raise ValueError('Tag "{}" is not correct'.format(self.title))
self.title = smart_text(self.title).lower()
self.slug = slugify(self.title)
super(Tag, self).save(*args, **kwargs)
Expand Down

0 comments on commit 2b0663f

Please sign in to comment.