def clean_slug(self): if self.attach: return slug = self.data.get('slug', '').strip().lower() language = self.data.get('language', settings.FALLBACK) parent_path = self.parent.get_path(language=language) if not slug: title = self.cleaned_data.get('title', '') slug = generate_slug(title, language=language, max_length=Node.MAX_PATHLEN, allowed=Node.ALLOWED_CHARS, default="node") existing = Node.get(path=parent_path + "/" + slug, language=language) base_slug = slug[:Node.MAX_PATHLEN-6] ## some space for counter count = 1 while (existing and existing != self.node) or \ (slug in self.reserved): slug = base_slug + str(count) existing = Node.get(path=self.parent.path + '/' + slug, language=language) count += 1 if slug in self.reserved: raise forms.ValidationError("This is a reserved name") if not Node.validpathre.match(slug): raise forms.ValidationError("Only numbers, letters, _-") existing = Node.get(path=parent_path + "/" + slug, language=language) if existing and existing != self.node: raise forms.ValidationError("Name in use") return slug
def test_maxlength(self): assert len(generate_slug('hello world', max_length=1)) == 1
def test_empty(self): assert generate_slug('', default='foo') == 'foo'
def test_stopwords(self): with mock.patch("wheelcms_axle.stopwords.stopwords") as swmock: swmock.get.return_value = ["foo", "bar"] assert generate_slug("foo blah bar") == "blah"
def test_allowed(self): assert generate_slug("abcdef", allowed="a") == "a"
def test_duplicate_plus_dash(self): assert generate_slug('foo--- -- - -- bar') == 'foo-bar'