summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLauréline Guérin <zebuline@entrouvert.com>2019-11-19 07:27:46 (GMT)
committerLauréline Guérin <zebuline@entrouvert.com>2019-11-19 14:27:21 (GMT)
commit8ed1923e44341c3613d20bdfecc970178ad57a16 (patch)
tree8b90c7c4b58406a98588e21f59537b0e173a5daf
parent879be68be43bf849660c2fbd3cd9b7f50e724192 (diff)
downloadcombo-wip/37733-clone-slug.zip
combo-wip/37733-clone-slug.tar.gz
combo-wip/37733-clone-slug.tar.bz2
page: fix slug on page duplication (#37733)wip/37733-clone-slug
-rw-r--r--combo/data/models.py10
-rw-r--r--tests/test_pages.py7
2 files changed, 10 insertions, 7 deletions
diff --git a/combo/data/models.py b/combo/data/models.py
index da81df4..8a2ad5a 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -174,17 +174,13 @@ class Page(models.Model):
max_order = Page.objects.all().aggregate(Max('order')).get('order__max') or 0
self.order = max_order + 1
if not self.slug:
- if Page.objects.count() == 0:
+ if not Page.objects.exists():
slug = 'index'
else:
base_slug = slugify(self.title)[:40]
slug = base_slug.strip('-')
i = 1
- while True:
- try:
- Page.objects.get(slug=slug, parent_id=self.parent_id)
- except ObjectDoesNotExist:
- break
+ while Page.objects.filter(slug=slug, parent_id=self.parent_id).exists():
i += 1
slug = '%s-%s' % (base_slug, i)
self.slug = slug
@@ -442,6 +438,8 @@ class Page(models.Model):
new_page.pk = None
# set title
new_page.title = _('Copy of %s') % self.title
+ # reset slug
+ new_page.slug = None
# reset snapshot
new_page.snapshot = None
# set order
diff --git a/tests/test_pages.py b/tests/test_pages.py
index f44ee17..5f45670 100644
--- a/tests/test_pages.py
+++ b/tests/test_pages.py
@@ -197,6 +197,7 @@ def test_duplicate_page():
group1 = Group.objects.create(name='foobar')
group2 = Group.objects.create(name='fooblah')
+ Page.objects.create() # page without snapshot, with slug 'index'
page = Page.objects.create(
title='foo',
slug='foo',
@@ -213,7 +214,7 @@ def test_duplicate_page():
new_page = page.duplicate()
assert new_page.pk != page.pk
assert new_page.title == 'Copy of foo'
- assert new_page.slug == page.slug
+ assert new_page.slug == 'copy-of-foo'
assert new_page.description == page.description
assert new_page.parent is None
assert new_page.snapshot is None
@@ -231,6 +232,10 @@ def test_duplicate_page():
assert new_cell2.placeholder == cell2.placeholder
assert list(new_cell2.groups.all()) == []
+ # duplicate again !
+ new_page = page.duplicate()
+ assert new_page.slug == 'copy-of-foo-2'
+
parent = Page.objects.create()
page.parent = parent
page.save()