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

1letter/fix#671 #675

Merged
merged 11 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions news/671.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix link_redirect_view, respect vhm vs none-vhm url schemes @1letter
5 changes: 1 addition & 4 deletions plone/app/contenttypes/browser/link_redirect_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ def absolute_target_url(self):
if "resolveuid" in url:
uid = url.split("/")[-1]
obj = uuidToObject(uid)
if obj:
url = "/".join(obj.getPhysicalPath()[2:])
if not url.startswith("/"):
url = "/" + url
url = obj.absolute_url()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd check for obj first (keep the if from line 114) ... it might be that the uuid is missing.

Copy link
Contributor

@yurj yurj Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also uid = url.split("/")[-1] above is fragile ("/resolveuid/" vs "resolveuid/" vs "/a/relativepath/resolveuid"). Generally, you should find the index of 'resolveuid' in the path, and get the next one if it exists. It should be something like this (uid = getuid(path)):

def get_uid(path):
    paths = path.split('/')
    uid = None
    if 'resolveuid' in paths:
        ri = paths.index('resolveuid')
        if ri + 1 != len(paths):
            uid = paths[ri + 1]
            if uid == '':
               uid = None
    return uid 
>>> get_uid('gfdgdf')
>>> get_uid('')
>>> get_uid('/resolveuid/')
>>> get_uid('/resolveuidgfdgd/')
>>> get_uid('/resolveuid/4342432')
'4342432'
>>> get_uid('/resolveuid/4342432/')
'4342432'
>>> get_uid('fdsfd/fdf/resolveuid/4342432/')
'4342432'
>>> get_uid('/fdsfd/fdf/resolveuid/4342432/')
'4342432'
>>> 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is resolve[uU]id still a thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petschki maybe, but i'm not sure

Copy link
Member

@petschki petschki Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def get_uid(path):
    paths = path.split('/')
    uid = None
    if 'resolveuid' in paths:
        ri = paths.index('resolveuid')
        if ri + 1 != len(paths):
            uid = paths[ri + 1]
            if '#' in uid:
                uid = uid.split('#')[0]
            if uid == '':
               uid = None
    return uid 
>>> get_uid('gfdgdf')
>>> get_uid('')
>>> get_uid('/resolveuid/')
>>> get_uid('/resolveuidgfdgd/')
>>> get_uid('/resolveuid/4342432')
'4342432'
>>> get_uid('/resolveuid/4342432/')
'4342432'
>>> get_uid('fdsfd/fdf/resolveuid/4342432/')
'4342432'
>>> get_uid('/fdsfd/fdf/resolveuid/4342432/')
'4342432'
>>> get_uid('/resolveuid/fb91bddde7eb46efbed20e9c10fb4929#autotoc-item-autotoc-1')
'fb91bddde7eb46efbed20e9c10fb4929'
>>> 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1letter solution to use urlparse is very good! Question: uid's are all lowercase? What if an UID has uppercase letters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least plone.outputfilters still takes it into account: https://github.com/plone/plone.outputfilters/blob/master/plone/outputfilters/filters/resolveuid_and_caption.py#L31

https://github.com/plone/plone.outputfilters/blob/278e2752c4790b5af2470bfc3c91a8aa26a9a80f/plone/outputfilters/filters/resolveuid_and_caption.py#L130

here is what plone.output filter does. It uses the re above to get all parts of the url. If there's an uid, find the object, translate it to the absolute_url, add the other parts.

If we want to do this the same way outputfilter does, we should use this code. This code also check IResolveUidsEnabler. Maybe this last part is not important (if a site disable uids and use resolveuid as a folder name...).

Generally, resoulveuid and catalog path resolution should be done in one place, I think in plone.api. I don't know about circular dipendencies...

Anyway, the actual code is ok, should works as expected and it is a real improvement.

if not url.startswith(("http://", "https://")):
url = self.request["SERVER_URL"] + url

Expand Down
8 changes: 7 additions & 1 deletion plone/app/contenttypes/tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,10 @@ def test_resolve_uid_to_absolute_target(self):
doc1 = self.portal["doc1"]
uid = IUUID(doc1)
self.link.remoteUrl = f"${{portal_url}}/resolveuid/{uid}"
self.assertEqual(view.absolute_target_url(), "http://nohost/doc1")

portal_state = getMultiAdapter(
(self.link, self.request), name="plone_portal_state"
)
portal_url = portal_state.portal_url()

self.assertEqual(view.absolute_target_url(), f"{portal_url}/doc1")