-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customize INameChooser adapter to check also alias ids and disallow t… (
#101) * Customize INameChooser adapter to check also alias ids and disallow to create contents that could override aliases * black and isort * zpretty
- Loading branch information
Showing
9 changed files
with
331 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from Acquisition import aq_inner | ||
from plone import api | ||
from plone.app.content.namechooser import ( | ||
NormalizingNameChooser as BaseNormalizingNameChooser, | ||
) | ||
from plone.app.redirector.interfaces import IRedirectionStorage | ||
from redturtle.volto import _ | ||
from redturtle.volto.interfaces import IRedTurtleVoltoSettings | ||
from zExceptions import BadRequest | ||
from zope.component import getUtility | ||
|
||
|
||
class NormalizingNameChooser(BaseNormalizingNameChooser): | ||
def chooseName(self, name, obj): | ||
""" | ||
Additional check: the id should not be in redirection tool. | ||
""" | ||
id = super().chooseName(name=name, obj=obj) | ||
try: | ||
if not api.portal.get_registry_record( | ||
"check_aliases_in_namechooser", | ||
interface=IRedTurtleVoltoSettings, | ||
default=False, | ||
): | ||
return id | ||
except KeyError: | ||
return id | ||
parent = aq_inner(self.context) | ||
storage = getUtility(IRedirectionStorage) | ||
path = "/".join(parent.getPhysicalPath()) + "/" + id | ||
if storage.get(path): | ||
portal_path = "/".join(api.portal.get().getPhysicalPath()) | ||
fixed_path = path.replace(portal_path, "") | ||
msg = _( | ||
"name_chooser_alias_error", | ||
default='The id "${id}" is invalid because there is already an alias for that path. ' | ||
'Search "${fixed_path}" in aliases management to manage it.', | ||
mapping={"id": id, "fixed_path": fixed_path}, | ||
) | ||
raise BadRequest(api.portal.translate(msg)) | ||
return id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.