Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #527 #529

Merged
merged 1 commit into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cadasta/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def save(self, *args, **kwargs):
self.name, max_length=max_length, allow_unicode=True
)

orig = self.slug
orig_slug = self.slug

if not self.id or self.__original_slug != self.slug:
for x in itertools.count(1):
if not type(self).objects.filter(slug=self.slug).exists():
break
slug_length = max_length - int(math.log10(x)) - 2
orig = self.slug[:slug_length]
self.slug = '{}-{}'.format(orig, x)
trial_slug = orig_slug[:slug_length]
self.slug = '{}-{}'.format(trial_slug, x)

self.__original_slug = self.slug

Expand Down
18 changes: 11 additions & 7 deletions cadasta/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def test_create_with_duplicate_slug(self):
assert instance1.slug != instance2.slug
assert instance2.slug == 'test-name-1'

def test_duplicate_slug_100_times(self):
for i in range(0, 101):
instance = MySlugModel()
instance.name = ("Test Name")
instance.save()

assert MySlugModel.objects.count() == 101
assert instance.slug == 'test-name-100'

def test_keep_slug(self):
instance = MySlugModel()
instance.name = 'Test Name'
Expand Down Expand Up @@ -136,17 +145,12 @@ def test_duplicate_slug_long_name(self):
print(instance1.slug)
print(instance2.slug)

def test_duplicate_slug_long_name_ten_times(self):
for i in range(0, 100):
def test_duplicate_slug_long_name_100_times(self):
for i in range(0, 101):
instance = MySlugModel()
instance.name = ('Very Long Name For The Purposes of Testing '
'That Slug Truncation Functions Correctly')
instance.save()

instance = MySlugModel()
instance.name = ('Very Long Name For The Purposes of '
'Testing That Slug Truncation Functions Correctly')
instance.save()

assert MySlugModel.objects.count() == 101
assert instance.slug[-4:] == '-100'