def is_valid_url(url, instance, create_links=True, site=None):
""" Checks for conflicting urls
"""
page_root = unquote(reverse("pages-root"))
if url and url != page_root:
# Url sanity check via regexp
if not any_path_re.match(url):
raise ValidationError(_('Invalid URL, use /my/url format.'))
# We only check page FK to site object to allow is_valid_url check on
# incomplete Page instances
if not site and instance.site_id:
site = instance.site
# Retrieve complete queryset of pages with corresponding URL
# This uses the same resolving function as ``get_page_from_path``
if url.startswith(page_root):
url = url[len(page_root):]
page_qs = get_page_queryset_from_path(url.strip('/'), site=site, draft=instance.publisher_is_draft)
url_clashes = []
# If queryset has pages checks for conflicting urls
for page in page_qs:
# Every page in the queryset except the current one is a conflicting page
# We have to exclude both copies of the page
if page and page.publisher_public_id != instance.pk and page.pk != instance.pk:
if create_links:
# Format return message with page url
url_clashes.append('<a href="%(page_url)s%(pk)s" target="_blank">%(page_title)s</a>' % {
'page_url': admin_reverse('cms_page_changelist'), 'pk': page.pk,
'page_title': force_text(page),
})
else:
# Just return the page name
url_clashes.append("'%s'" % page)
if url_clashes:
# If clashing pages exist raise the exception
raise ValidationError(mark_safe(
ungettext_lazy('Page %(pages)s has the same url \'%(url)s\' as current page "%(instance)s".',
'Pages %(pages)s have the same url \'%(url)s\' as current page "%(instance)s".',
len(url_clashes)) %
{'pages': ', '.join(url_clashes), 'url': url, 'instance': instance}))
return True
评论列表
文章目录