mirror of
https://github.com/Hopiu/django-uuslug.git
synced 2026-05-24 01:33:45 +00:00
add stopwords and auto trancate capabilities
This commit is contained in:
parent
e87dddfd7d
commit
e9851289dc
6 changed files with 91 additions and 44 deletions
|
|
@ -1,3 +1,10 @@
|
|||
## 1.1.5
|
||||
|
||||
Enhancement:
|
||||
|
||||
- Ability to remove `stopwords` from string
|
||||
- Ability to auto truncate string to match model field's max_length
|
||||
|
||||
## 1.0.5
|
||||
|
||||
Fix:
|
||||
|
|
|
|||
82
README.md
82
README.md
|
|
@ -26,7 +26,7 @@ How to install
|
|||
a. unzip the downloaded file
|
||||
b. cd into django-uuslug-* directory
|
||||
c. run python setup.py
|
||||
5. pip install -e git+https://github.com/un33k/django-uuslug#egg=django-uuslug
|
||||
5. pip install -e git+https://github.com/un33k/django-uuslug#egg=django-uuslug
|
||||
|
||||
How to use
|
||||
=================
|
||||
|
|
@ -39,6 +39,14 @@ Unicode Test
|
|||
r = slugify(txt)
|
||||
self.assertEqual(r, "this-is-a-test")
|
||||
|
||||
txt = "___This is a test ---"
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "this-is-a-test")
|
||||
|
||||
txt = "___This is a test___"
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "this-is-a-test")
|
||||
|
||||
txt = "This -- is a ## test ---"
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "this-is-a-test")
|
||||
|
|
@ -55,14 +63,14 @@ Unicode Test
|
|||
r = slugify(txt)
|
||||
self.assertEqual(r, "nin-hao-wo-shi-zhong-guo-ren")
|
||||
|
||||
txt = 'Компьютер'
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "kompiuter")
|
||||
|
||||
txt = 'jaja---lol-méméméoo--a'
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "jaja-lol-mememeoo-a")
|
||||
|
||||
txt = 'Компьютер'
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "kompiuter")
|
||||
|
||||
txt = 'jaja---lol-méméméoo--a'
|
||||
r = slugify(txt, max_length=9)
|
||||
self.assertEqual(r, "jaja-lol")
|
||||
|
|
@ -99,14 +107,6 @@ Unicode Test
|
|||
r = slugify(txt, max_length=20, word_boundary=True, separator="ZZZZZZ")
|
||||
self.assertEqual(r, "jajaZZZZZZlolZZZZZZmememeooZZZZZZa")
|
||||
|
||||
txt = "___This is a test ---"
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "this-is-a-test")
|
||||
|
||||
txt = "___This is a test___"
|
||||
r = slugify(txt)
|
||||
self.assertEqual(r, "this-is-a-test")
|
||||
|
||||
txt = 'one two three four five'
|
||||
r = slugify(txt, max_length=13, word_boundary=True, save_order=True)
|
||||
self.assertEqual(r, "one-two-three")
|
||||
|
|
@ -123,6 +123,27 @@ Unicode Test
|
|||
r = slugify(txt, max_length=12, word_boundary=True, save_order=True)
|
||||
self.assertEqual(r, "one-two")
|
||||
|
||||
txt = 'this has a stopword'
|
||||
r = slugify(txt, stopwords=['stopword'])
|
||||
self.assertEqual(r, 'this-has-a')
|
||||
|
||||
txt = 'the quick brown fox jumps over the lazy dog'
|
||||
r = slugify(txt, stopwords=['the'])
|
||||
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
|
||||
|
||||
txt = 'Foo A FOO B foo C'
|
||||
r = slugify(txt, stopwords=['foo'])
|
||||
self.assertEqual(r, 'a-b-c')
|
||||
|
||||
txt = 'Foo A FOO B foo C'
|
||||
r = slugify(txt, stopwords=['FOO'])
|
||||
self.assertEqual(r, 'a-b-c')
|
||||
|
||||
txt = 'the quick brown fox jumps over the lazy dog in a hurry'
|
||||
r = slugify(txt, stopwords=['the', 'in', 'a', 'hurry'])
|
||||
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
|
||||
|
||||
|
||||
|
||||
Uniqueness Test
|
||||
|
||||
|
|
@ -139,7 +160,6 @@ Uniqueness Test
|
|||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# self.slug = uuslug(self.name, instance=self, separator="_") # optional non-dash separator
|
||||
self.slug = uuslug(self.name, instance=self)
|
||||
super(CoolSlug, self).save(*args, **kwargs)
|
||||
|
||||
|
|
@ -152,19 +172,31 @@ Uniqueness Test
|
|||
name = "john"
|
||||
c = CoolSlug.objects.create(name=name)
|
||||
c.save()
|
||||
print c.slug # => "john"
|
||||
print(c.slug) # => "john"
|
||||
|
||||
c1 = CoolSlug.objects.create(name=name)
|
||||
c1.save()
|
||||
print c1.slug # => "john-1"
|
||||
print(c1.slug) # => "john-1"
|
||||
|
||||
c2 = CoolSlug.objects.create(name=name)
|
||||
c2.save()
|
||||
print c2.slug # => "john-2"
|
||||
print(c2.slug) # => "john-2"
|
||||
|
||||
|
||||
# If you need truncation of your slug, here is an example
|
||||
# If you need truncation of your slug to exact length, here is an example
|
||||
class SmartTruncatedSlug(models.Model):
|
||||
name = models.CharField(max_length=19)
|
||||
slug = models.CharField(max_length=10)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.slug = uuslug(self.name, instance=self, max_length=10)
|
||||
super(SmartTruncatedSlug, self).save(*args, **kwargs)
|
||||
|
||||
# If you need automatic truncation of your slug, here is an example
|
||||
class AutoTruncatedSlug(models.Model):
|
||||
name = models.CharField(max_length=19)
|
||||
slug = models.CharField(max_length=19)
|
||||
|
||||
|
|
@ -172,21 +204,9 @@ Uniqueness Test
|
|||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.slug = uuslug(self.name, instance=self, start_no=9, max_length=19, word_boundary=True)
|
||||
self.slug = uuslug(self.name, instance=self)
|
||||
super(SmartTruncatedSlug, self).save(*args, **kwargs)
|
||||
|
||||
# Let's test it
|
||||
name = 'jaja---lol-méméméoo--a'
|
||||
|
||||
obj = SmartTruncatedExactWordBoundrySlug.objects.create(name=name)
|
||||
self.assertEqual(obj.slug, "jaja-lol-mememeoo-a") # 19 is max_length
|
||||
|
||||
obj = SmartTruncatedExactWordBoundrySlug.objects.create(name=name)
|
||||
self.assertEqual(obj.slug, "jaja-lol-mememeoo-9") # 19 is max_length, start_no = 9
|
||||
|
||||
obj = SmartTruncatedExactWordBoundrySlug.objects.create(name=name)
|
||||
self.assertEqual(obj.slug, "jaja-lol-mememeo-10") # 19 is max_length, readjust for "-10"
|
||||
|
||||
|
||||
Running the tests
|
||||
=================
|
||||
|
|
|
|||
2
setup.py
2
setup.py
|
|
@ -14,7 +14,7 @@ url = 'https://github.com/un33k/django-uuslug'
|
|||
author = 'Val Neekman'
|
||||
author_email = 'info@neekware.com'
|
||||
license = 'BSD'
|
||||
install_requires = ['python-slugify>=0.1.0']
|
||||
install_requires = ['python-slugify>=1.1.2']
|
||||
classifiers = [
|
||||
'Development Status :: 5 - Production/Stable',
|
||||
'Environment :: Web Environment',
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ if 'testsettings' in os.environ['DJANGO_SETTINGS_MODULE']:
|
|||
self.slug = uuslug(self.name, instance=self, start_no=2, max_length=17, word_boundary=False, separator='_')
|
||||
super(TruncatedSlugDifferentSeparator, self).save(*args, **kwargs)
|
||||
|
||||
class SmallSlug(models.Model):
|
||||
class AutoTruncatedSlug(models.Model):
|
||||
"""
|
||||
The slug by generated by uuslug is bigger than the field max_length
|
||||
"""
|
||||
|
|
@ -94,5 +94,5 @@ if 'testsettings' in os.environ['DJANGO_SETTINGS_MODULE']:
|
|||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.slug = uuslug(self.name, instance=self, max_length=20)
|
||||
super(SmallSlug, self).save(*args, **kwargs)
|
||||
self.slug = uuslug(self.name, instance=self)
|
||||
super(AutoTruncatedSlug, self).save(*args, **kwargs)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from uuslug import slugify
|
|||
from uuslug.models import (CoolSlug, AnotherSlug, TruncatedSlug,
|
||||
SmartTruncatedSlug, SmartTruncatedExactWordBoundrySlug,
|
||||
CoolSlugDifferentSeparator, TruncatedSlugDifferentSeparator,
|
||||
SmallSlug)
|
||||
AutoTruncatedSlug)
|
||||
|
||||
|
||||
class SlugUnicodeTestCase(TestCase):
|
||||
|
|
@ -105,6 +105,26 @@ class SlugUnicodeTestCase(TestCase):
|
|||
r = slugify(txt, max_length=12, word_boundary=True, save_order=True)
|
||||
self.assertEqual(r, "one-two")
|
||||
|
||||
txt = 'this has a stopword'
|
||||
r = slugify(txt, stopwords=['stopword'])
|
||||
self.assertEqual(r, 'this-has-a')
|
||||
|
||||
txt = 'the quick brown fox jumps over the lazy dog'
|
||||
r = slugify(txt, stopwords=['the'])
|
||||
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
|
||||
|
||||
txt = 'Foo A FOO B foo C'
|
||||
r = slugify(txt, stopwords=['foo'])
|
||||
self.assertEqual(r, 'a-b-c')
|
||||
|
||||
txt = 'Foo A FOO B foo C'
|
||||
r = slugify(txt, stopwords=['FOO'])
|
||||
self.assertEqual(r, 'a-b-c')
|
||||
|
||||
txt = 'the quick brown fox jumps over the lazy dog in a hurry'
|
||||
r = slugify(txt, stopwords=['the', 'in', 'a', 'hurry'])
|
||||
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
|
||||
|
||||
|
||||
class SlugUniqueTestCase(TestCase):
|
||||
"""Tests for Slug - Unique"""
|
||||
|
|
@ -222,7 +242,7 @@ class SlugUniqueDifferentSeparatorTestCase(TestCase):
|
|||
|
||||
|
||||
class SlugMaxLengthTestCase(TestCase):
|
||||
"""Tests for Slug - Max length minor than field length"""
|
||||
"""Tests for Slug - Max length less than field length"""
|
||||
|
||||
def test_manager(self):
|
||||
name = "john" * 51
|
||||
|
|
@ -235,13 +255,13 @@ class SlugMaxLengthTestCase(TestCase):
|
|||
obj = CoolSlug.objects.create(name=name)
|
||||
self.assertEqual(obj.slug, name[:198] + "-1")
|
||||
|
||||
def test_max_length_bigger_than_field_slug(self):
|
||||
def test_max_length_greater_than_field_slug(self):
|
||||
name = 'jaja---lol-méméméoo--a-méméméoo'
|
||||
|
||||
obj = SmallSlug.objects.create(name=name)
|
||||
obj = AutoTruncatedSlug.objects.create(name=name)
|
||||
# 10 is field max_length, 20 is uuslug function max_length
|
||||
self.assertEqual(obj.slug, "jaja-lol-m")
|
||||
|
||||
# 10 is field max_length, 20 is uuslug function max_length
|
||||
obj = SmallSlug.objects.create(name=name)
|
||||
obj = AutoTruncatedSlug.objects.create(name=name)
|
||||
self.assertEqual(obj.slug, "jaja-lol-1")
|
||||
|
|
|
|||
|
|
@ -9,18 +9,18 @@ __all__ = ['slugify', 'uuslug']
|
|||
|
||||
|
||||
def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0,
|
||||
word_boundary=False, separator='-', save_order=False):
|
||||
word_boundary=False, separator='-', save_order=False, stopwords=()):
|
||||
"""
|
||||
Make a slug from a given text.
|
||||
"""
|
||||
|
||||
return smart_str(pyslugify(text, entities, decimal, hexadecimal, max_length,
|
||||
word_boundary, separator, save_order))
|
||||
word_boundary, separator, save_order, stopwords))
|
||||
|
||||
|
||||
def uuslug(s, instance, entities=True, decimal=True, hexadecimal=True,
|
||||
slug_field='slug', filter_dict=None, start_no=1, max_length=0,
|
||||
word_boundary=False, separator='-', save_order=False):
|
||||
word_boundary=False, separator='-', save_order=False, stopwords=()):
|
||||
|
||||
""" This method tries a little harder than django's django.template.defaultfilters.slugify. """
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ def uuslug(s, instance, entities=True, decimal=True, hexadecimal=True,
|
|||
|
||||
slug = slugify(s, entities=entities, decimal=decimal, hexadecimal=hexadecimal,
|
||||
max_length=max_length, word_boundary=word_boundary, separator=separator,
|
||||
save_order=save_order)
|
||||
save_order=save_order, stopwords=stopwords)
|
||||
|
||||
new_slug = slug
|
||||
counter = start_no
|
||||
|
|
|
|||
Loading…
Reference in a new issue