def clean(self):
cleaned_data = self.cleaned_data
slug = cleaned_data.get('slug', '')
page = self.instance
lang = cleaned_data.get('language', None)
# No language, can not go further, but validation failed already
if not lang:
return cleaned_data
parent = cleaned_data.get('parent', None)
site = self.cleaned_data.get('site', Site.objects.get_current())
if parent and parent.site != site:
raise ValidationError("Site doesn't match the parent's page site")
if site and not is_valid_page_slug(page, parent, lang, slug, site):
self._errors['slug'] = ErrorList([_('Another page with this slug already exists')])
del cleaned_data['slug']
if self.instance and page.title_set.count():
#Check for titles attached to the page makes sense only because
#AdminFormsTests.test_clean_overwrite_url validates the form with when no page instance available
#Looks like just a theoretical corner case
title = page.get_title_obj(lang, fallback=False)
if title and not isinstance(title, EmptyTitle) and slug:
oldslug = title.slug
title.slug = slug
title.save()
try:
is_valid_url(title.path, page)
except ValidationError as exc:
title.slug = oldslug
title.save()
if 'slug' in cleaned_data:
del cleaned_data['slug']
if hasattr(exc, 'messages'):
errors = exc.messages
else:
errors = [force_text(exc.message)]
self._errors['slug'] = ErrorList(errors)
return cleaned_data
评论列表
文章目录