Skip to content

Commit

Permalink
Merge pull request #3515 from gustavi/fix-3503
Browse files Browse the repository at this point in the history
Fix #3503 : plus possible de creer un tag vide
  • Loading branch information
sandhose committed Apr 9, 2016
2 parents 668f7aa + 9ab0765 commit be9492f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
9 changes: 5 additions & 4 deletions zds/tutorialv2/models/models_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
36 changes: 36 additions & 0 deletions zds/tutorialv2/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'])
Expand Down

0 comments on commit be9492f

Please sign in to comment.