From 72d513feaa588292940e0d989659b5e2234ff370 Mon Sep 17 00:00:00 2001 From: Thiago Curvelo Date: Mon, 24 Jul 2017 00:05:18 -0300 Subject: [PATCH] Add an utility function to get object paths (fixes #119) --- sc/social/like/browser/canonicalurl.py | 8 +++-- sc/social/like/subscribers.py | 5 ++- .../like/tests/test_canonicalurl_view.py | 33 ++++++++++++++++++- sc/social/like/utils.py | 8 +++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/sc/social/like/browser/canonicalurl.py b/sc/social/like/browser/canonicalurl.py index 6603f9b5..356eb43d 100644 --- a/sc/social/like/browser/canonicalurl.py +++ b/sc/social/like/browser/canonicalurl.py @@ -7,6 +7,7 @@ from sc.social.like.interfaces import ISocialLikeSettings from sc.social.like.logger import logger from sc.social.like.utils import get_valid_objects +from sc.social.like.utils import get_path_to_virtual_path from sc.social.like.utils import validate_canonical_domain from z3c.form import button from z3c.form import field @@ -86,6 +87,9 @@ def handle_update(self, action): def handle_cancel(self, action): self.request.response.redirect(self.context.absolute_url()) + def get_item_path(self, item): + return get_path_to_virtual_path(self.request, item) + def update_canonical_url(self, data): """Update the canonical URL of all objects in the catalog providing the ISocialMedia behavior. @@ -106,9 +110,7 @@ def update_canonical_url(self, data): logger.info(u'{0} objects will have their canonical URL updated'.format(total)) for obj in get_valid_objects(results): - # FIXME: we're currently ignoring the Plone site id - # https://github.com/collective/sc.social.like/issues/119 - path = '/'.join(obj.getPhysicalPath()[2:]) + path = self.get_item_path(obj) if obj.effective_date < DateTime(published_before): # use the canonical domain defined in this form obj.canonical_url = '{0}/{1}'.format(old_canonical_domain, path) diff --git a/sc/social/like/subscribers.py b/sc/social/like/subscribers.py index d81ad7d9..936cc1be 100644 --- a/sc/social/like/subscribers.py +++ b/sc/social/like/subscribers.py @@ -19,6 +19,7 @@ from sc.social.like.config import PROJECTNAME from sc.social.like.interfaces import ISocialLikeSettings from sc.social.like.logger import logger +from sc.social.like.utils import get_path_to_virtual_path from zope.component import getUtility from zope.schema.interfaces import WrongType @@ -97,9 +98,7 @@ def assign_canonical_url(obj, event): # we can't assign a canonical URL without a canonical domain if canonical_domain: - # FIXME: we're currently ignoring the Plone site id - # https://github.com/collective/sc.social.like/issues/119 - path = '/'.join(obj.getPhysicalPath()[2:]) + path = get_path_to_virtual_path(event.request, obj) obj.canonical_url = '{0}/{1}'.format(canonical_domain, path) logger.info('canonical_url set for {0}'.format(obj.canonical_url)) else: diff --git a/sc/social/like/tests/test_canonicalurl_view.py b/sc/social/like/tests/test_canonicalurl_view.py index 0414e936..32453010 100644 --- a/sc/social/like/tests/test_canonicalurl_view.py +++ b/sc/social/like/tests/test_canonicalurl_view.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- """Test for the canonical URL updater form.""" from DateTime import DateTime +from Products.SiteAccess.VirtualHostMonster import manage_addVirtualHostMonster from plone import api from sc.social.like.interfaces import ISocialLikeSettings from sc.social.like.testing import HAS_DEXTERITY from sc.social.like.testing import INTEGRATION_TESTING from sc.social.like.tests.utils import enable_social_media_behavior +from sc.social.like.utils import get_path_to_virtual_path import unittest @@ -47,7 +49,6 @@ def setup_content(self): self.portal['baz'].effective_date = DateTime() self.portal['baz'].reindexObject() - @unittest.expectedFailure # FIXME: https://github.com/collective/sc.social.like/issues/119 def test_update_canonical_url(self): # canonical URL is None as we did not set up a canonical domain self.assertIsNone(self.portal['foo'].canonical_url) @@ -72,3 +73,33 @@ def test_update_canonical_url(self): # canonical URL unchaged self.assertEqual( self.portal['baz'].canonical_url, 'https://example.org/plone/baz') + + +class UtilPathToVirtualPathTestCase(unittest.TestCase): + + layer = INTEGRATION_TESTING + + def setUp(self): + app = self.layer['app'] + portal = self.layer['portal'] + self.request = self.layer['request'] + + if 'virtual_hosting' not in app.objectIds(): + manage_addVirtualHostMonster(app, 'virtual_hosting') + + with api.env.adopt_roles(['Manager']): + self.obj = api.content.create(portal, type='News Item', id='foo') + api.content.transition(self.obj, 'publish') + + def test_get_path_to_virtual_path(self): + self.request.traverse('/plone/') + self.assertEqual( + get_path_to_virtual_path(self.request, self.obj), + 'plone/foo') + + def test_get_path_to_virtual_path_using_virtualhostmonster(self): + self.request.traverse( + '/VirtualHostBase/http/bar.com/plone/VirtualHostRoot/') + self.assertEqual( + get_path_to_virtual_path(self.request, self.obj), + 'foo') diff --git a/sc/social/like/utils.py b/sc/social/like/utils.py index ca4e3d09..3db31862 100644 --- a/sc/social/like/utils.py +++ b/sc/social/like/utils.py @@ -128,3 +128,11 @@ def get_valid_objects(brains): logger.warn(msg.format(b.getPath())) continue yield obj + + +def get_path_to_virtual_path(request, item): + return '/'.join( + request.physicalPathToVirtualPath( + item.getPhysicalPath() + ) + )