Skip to content

Commit

Permalink
fix fix-links behavior due to a problem with external links substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-bellenghi committed Oct 9, 2023
1 parent 12b2f21 commit d9fbe81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ Changelog
5.2.5 (unreleased)
------------------

- Nothing changed yet.
- 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].


5.2.4 (2023-09-26)
Expand Down
12 changes: 11 additions & 1 deletion src/redturtle/volto/browser/fix_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit d9fbe81

Please sign in to comment.