Skip to content

Commit 13f989b

Browse files
Test slugs after generation that they are really valid.
1 parent 7cd34dc commit 13f989b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

readthedocs/builds/version_slug.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
VERSION_SLUG_REGEX = '(?:[a-z0-9][-._a-z0-9]+?)'
3434

3535

36-
version_slug_regex = re.compile(VERSION_SLUG_REGEX)
37-
38-
3936
class VersionSlugField(models.CharField):
4037
"""
4138
Implementation inspired by ``django_extensions.db.fields.AutoSlugField``.
@@ -45,6 +42,7 @@ class VersionSlugField(models.CharField):
4542
allowed_chars = string.lowercase + string.digits + allowed_punctuation
4643
placeholder = '-'
4744
fallback_slug = 'unkown'
45+
test_pattern = re.compile('^{pattern}$'.format(pattern=VERSION_SLUG_REGEX))
4846

4947
def __init__(self, *args, **kwargs):
5048
kwargs.setdefault('db_index', True)
@@ -154,6 +152,8 @@ def create_slug(self, model_instance):
154152
slug = slug + end
155153
kwargs[self.attname] = slug
156154
next += 1
155+
assert self.test_pattern.match(slug), (
156+
'Invalid generated slug: {slug}'.format(slug=slug))
157157
return slug
158158

159159
def pre_save(self, model_instance, add):

0 commit comments

Comments
 (0)