Repository: zestedesavoir/zds-site

Issue:
[beta v18] C'est possible de créer un tag vide
En bossant sur le design de la page des tags, j'ai malencontreusement laissé une virgule à la fin de la liste des tags, ce qui a eu pour effet de créer un tag vide.

ping @gustavi 



Discussion:
@gustavi, vu ton commit, tu t'en occupe ?

Oui, juste un souci de rebase. Ça vient soon.


Resolving patch:
diff --git a/zds/tutorialv2/models/models_database.py b/zds/tutorialv2/models/models_database.py
--- a/zds/tutorialv2/models/models_database.py
+++ b/zds/tutorialv2/models/models_database.py
@@ -528,11 +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 current_tag is None:
-                current_tag = Tag(title=tag_title)
-                current_tag.save()
+            if len(tag_title) > 0:
+                if current_tag is None:
+                    current_tag = Tag(title=tag_title)
+                    current_tag.save()
 
-            self.tags.add(current_tag)
+                self.tags.add(current_tag)
         self.save()
 
 


Test patch:
diff --git a/zds/tutorialv2/tests/tests_models.py b/zds/tutorialv2/tests/tests_models.py
--- a/zds/tutorialv2/tests/tests_models.py
+++ b/zds/tutorialv2/tests/tests_models.py
@@ -15,6 +15,7 @@
 from zds.gallery.factories import UserGalleryFactory
 from zds.tutorialv2.models.models_database import PublishableContent
 from zds.tutorialv2.publication_utils import publish_content
+from zds.utils.models import Tag
 
 overrided_zds_app = settings.ZDS_APP
 overrided_zds_app['content']['repo_private_path'] = os.path.join(BASE_DIR, 'contents-private-test')
@@ -466,6 +467,41 @@ def test_publication_and_attributes_consistency(self):
         self.assertEqual(old_date, article.public_version.publication_date)
         self.assertNotEqual(old_date, article.public_version.update_date)
 
+    def test_add_tags(self):
+        tuto = self.tuto
+        tags_len = len(Tag.objects.all())
+        tuto_tags_len = len(tuto.tags.all())
+
+        # add 3 tags
+        tags = ['a', 'b', 'c']
+        tuto.add_tags(tags)
+        tags_len += 3
+        tuto_tags_len += 3
+        self.assertEqual(tags_len, len(Tag.objects.all()))
+        self.assertEqual(tuto_tags_len, len(tuto.tags.all()))
+
+        # add the same tags (nothing append)
+        tags = ['a', 'b', 'c']
+        tuto.add_tags(tags)
+        self.assertEqual(tags_len, len(Tag.objects.all()))
+        self.assertEqual(tuto_tags_len, len(tuto.tags.all()))
+
+        # add 2 more
+        tags = ['d', 'e']
+        tuto.add_tags(tags)
+        tags_len += 2
+        tuto_tags_len += 2
+        self.assertEqual(tags_len, len(Tag.objects.all()))
+        self.assertEqual(tuto_tags_len, len(tuto.tags.all()))
+
+        # add 3 with invalid content (only 2 valid)
+        tags = ['f', 'g', ' ']
+        tuto.add_tags(tags)
+        tags_len += 2
+        tuto_tags_len += 2
+        self.assertEqual(tags_len, len(Tag.objects.all()))
+        self.assertEqual(tuto_tags_len, len(tuto.tags.all()))
+
     def tearDown(self):
         if os.path.isdir(settings.ZDS_APP['content']['repo_private_path']):
             shutil.rmtree(settings.ZDS_APP['content']['repo_private_path'])