diff --git a/CHANGES.rst b/CHANGES.rst index be9a7ebe..b9a0d7d5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,11 @@ Changelog 5.2.5 (unreleased) ------------------ +- Fix: the 'fix-link' view has a bug that corrupts links by replacing + the current external URL with a URL that is always relative to the + site, even when requesting replacement with a link from a different + website. + [lucabel]. - plone.app.redirector.FourOhFourView.search_for_similar patch to enable conditionally the search for similar [folix-01] diff --git a/src/redturtle/volto/browser/fix_links.py b/src/redturtle/volto/browser/fix_links.py index 0ced9f9d..0b01b2ee 100644 --- a/src/redturtle/volto/browser/fix_links.py +++ b/src/redturtle/volto/browser/fix_links.py @@ -6,6 +6,7 @@ from Products.Five import BrowserView from zope.component import queryMultiAdapter from zope.schema import getFieldsInOrder +from urllib.parse import urlparse, urlunparse import json import logging @@ -68,6 +69,7 @@ def __call__(self): # set the new value anyway because some values could not be transformed, # but they now have the right url anyway. setattr(item, name, res["new_value"]) + item.reindexObject() i += 1 logger.info("### END ###") logger.info("### {} items fixed ###".format(len(fixed_objects))) @@ -94,6 +96,10 @@ def check_pattern(self, value): return True def replace_pattern(self, value, is_link=False): + # obtain data to make link sub evaluation + current_host = urlparse(self.request.URL).netloc + destination_host = urlparse(self.portal_url).netloc + for url in self.request.form.get("to_replace", "").split(): match = re.search(r"(?<={}).*".format(url), value) if match: @@ -106,7 +112,11 @@ def replace_pattern(self, value, is_link=False): return value if obj: if is_link: - return "${portal_url}/resolveuid/" + obj.UID() + if current_host != destination_host: + new_url = urlparse(value)._replace(netloc=destination_host) + return urlunparse(new_url) + else: + return "${portal_url}/resolveuid/" + obj.UID() else: return obj.UID() return value