Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
language only exist once on a site.
"""
exclude = [] if exclude is None else exclude
super().clean_fields(exclude)
if self.parent and (
self.parent.application
or self.parent.ancestors().exclude(application="").exists()
):
error = _("Apps may not have any descendants.")
raise validation_error(
_("Invalid parent: %s") % (error,), field="parent", exclude=exclude
)
if self.application and self.children.exists():
raise validation_error(
_("Apps may not have any descendants in the tree."),
field="application",
exclude=exclude,
)
app_config = self.application_config()
if app_config and app_config.get("required_fields"):
missing = [
field
for field in app_config["required_fields"]
if not getattr(self, field)
]
if missing:
error = _("This field is required for the application %s.") % (
self.get_application_display(),
)
if self.language_code == settings.LANGUAGES[0][0] and self.translation_of:
raise validation_error(
_(
"Objects in the primary language cannot be"
" the translation of another object."
),
field="translation_of",
exclude=exclude,
)
if (
self.translation_of
and self.translation_of.language_code != settings.LANGUAGES[0][0]
):
raise validation_error(
_(
"Objects may only be the translation of"
" objects in the primary language."
),
field="translation_of",
exclude=exclude,
)
def clean_fields(self, exclude=None):
"""
Check for path uniqueness problems.
"""
super().clean_fields(exclude)
if self.static_path:
if not self.path:
raise validation_error(
_("Static paths cannot be empty. Did you mean '/'?"),
field="path",
exclude=exclude,
)
else:
self.path = "%s%s/" % (self.parent.path if self.parent else "/", self.slug)
super().clean()
# Skip if we don't exist yet.
if not self.pk:
return
clash_candidates = dict(self._path_clash_candidates().values_list("path", "id"))
for pk, node in self._branch_for_update().items():
if (
else:
self.path = "%s%s/" % (self.parent.path if self.parent else "/", self.slug)
super().clean()
# Skip if we don't exist yet.
if not self.pk:
return
clash_candidates = dict(self._path_clash_candidates().values_list("path", "id"))
for pk, node in self._branch_for_update().items():
if (
node.path in clash_candidates
and not clash_candidates[node.path] == node.pk
):
raise validation_error(
_("The page %(page)s's new path %(path)s would not be unique.")
% {"page": node, "path": node.path},
field="path",
exclude=exclude,
)
def clean_fields(self, exclude=None):
"""
Checks that application nodes do not have any descendants, and that
required fields for the selected application (if any) are filled out,
and that app instances with the same instance namespace and same
language only exist once on a site.
"""
exclude = [] if exclude is None else exclude
super().clean_fields(exclude)
if self.parent and (
self.parent.application
or self.parent.ancestors().exclude(application="").exists()
):
error = _("Apps may not have any descendants.")
raise validation_error(
_("Invalid parent: %s") % (error,), field="parent", exclude=exclude
)
if self.application and self.children.exists():
raise validation_error(
_("Apps may not have any descendants in the tree."),
field="application",
exclude=exclude,
)
app_config = self.application_config()
if app_config and app_config.get("required_fields"):
missing = [
field
for field in app_config["required_fields"]
if not getattr(self, field)
exclude=exclude,
)
if self.redirect_to_page_id:
if self.redirect_to_page_id == self.pk:
raise validation_error(
_("Cannot redirect to self."),
field="redirect_to_page",
exclude=exclude,
)
if (
self.redirect_to_page.redirect_to_page_id
or self.redirect_to_page.redirect_to_url
):
raise validation_error(
_(
'Do not chain redirects. The selected page "%(title)s"'
' redirects to "%(path)s".'
)
% {
"title": self.redirect_to_page,
"path": (
self.redirect_to_page.redirect_to_page.get_absolute_url()
if self.redirect_to_page.redirect_to_page
else self.redirect_to_page.redirect_to_url
),
},
field="redirect_to_page",
exclude=exclude,
)