Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix-links behavior due to a problem with external links substitution #78

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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
Loading