-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
597 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/brasil/gov/portal/browser/content/link_redirect_view.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from plone.app.contenttypes.browser.link_redirect_view import LinkRedirectView as OriginalView | ||
from plone.app.contenttypes.browser.link_redirect_view import NON_RESOLVABLE_URL_SCHEMES | ||
from plone.app.contenttypes.utils import replace_link_variables_by_paths | ||
from zope.component import getMultiAdapter | ||
|
||
|
||
class LinkRedirectView(OriginalView): | ||
""" | ||
Substituicao do path pela url do site ao utilizar variaveis ${portal_url} | ||
ou ${navigation_root_url} no campo remoteUrl do Link. | ||
Acertada tambem a url de links relativos (../, ./), pois gerava a url | ||
utilizando como base a url do proprio objeto Link. | ||
Isso fazia com que a url, mesmo redirecionando corretamente, ficasse | ||
diferente da real, e não seria atingida pelo purge. | ||
Demanda PR para correção da issue 463: | ||
https://github.com/plonegovbr/brasil.gov.portal/issues/463 | ||
""" | ||
|
||
def absolute_target_url(self): | ||
"""Compute the absolute target URL.""" | ||
url = self.context.remoteUrl | ||
|
||
if self._url_uses_scheme(NON_RESOLVABLE_URL_SCHEMES): | ||
# For non http/https url schemes, there is no path to resolve. | ||
return url | ||
|
||
remote_url_utils = getMultiAdapter( | ||
(self.context, self.request), | ||
name=u'remote_url_utils', | ||
) | ||
path = '/'.join(self.context.getPhysicalPath()) | ||
|
||
if url.startswith('.'): | ||
# ./ ../ ../../ | ||
url = remote_url_utils.remote_url_transform( | ||
path, | ||
url, | ||
) | ||
else: | ||
url = replace_link_variables_by_paths(self.context, url) | ||
url = remote_url_utils.remote_url_transform( | ||
path, | ||
url, | ||
) | ||
if not url.startswith(('http://', 'https://')): | ||
portal_state = self.context.restrictedTraverse( | ||
'@@plone_portal_state', | ||
) | ||
url = '{0}{1}'.format(portal_state.portal_url(), url) | ||
return url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:browser="http://namespaces.zope.org/browser"> | ||
|
||
<include package="z3c.unconfigure" file="meta.zcml" /> | ||
|
||
<!-- override link_redirect_view --> | ||
<!-- Com Interface herdando do layer original ou mesmo no overrides | ||
continuava o erro: ConfigurationConflictError | ||
Foi necessario utilizar unconfigure --> | ||
<include package="plone.app.contenttypes.browser" file="configure.zcml" /> | ||
<unconfigure> | ||
<browser:page | ||
name="link_redirect_view" | ||
for="plone.app.contenttypes.interfaces.ILink" | ||
class=".link_redirect_view.LinkRedirectView" | ||
layer="plone.app.contenttypes.interfaces.IPloneAppContenttypesLayer" | ||
permission="zope2.View" | ||
menu="plone_displayviews" | ||
title="View Link" | ||
/> | ||
</unconfigure> | ||
|
||
<!-- customize link_redirect_view --> | ||
<configure package="plone.app.contenttypes.browser"> | ||
<browser:page | ||
name="link_redirect_view" | ||
for="plone.app.contenttypes.interfaces.ILink" | ||
class="brasil.gov.portal.browser.content.link_redirect_view.LinkRedirectView" | ||
layer="brasil.gov.portal.interfaces.IBrasilGov" | ||
permission="zope2.View" | ||
menu="plone_displayviews" | ||
title="View Link" | ||
/> | ||
</configure> | ||
|
||
</configure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from plone.app.contenttypes.browser.link_redirect_view import NON_RESOLVABLE_URL_SCHEMES | ||
from Products.Five import BrowserView | ||
from zope.component import getMultiAdapter | ||
from zope.interface import implements | ||
from zope.interface import Interface | ||
|
||
|
||
class IRemoteUrlUtils(Interface): | ||
|
||
def _url_uses_scheme(self): | ||
"""""" | ||
|
||
def remote_url_transform(self): | ||
"""Transforma o path em url do site.""" | ||
|
||
|
||
class RemoteUrlUtils(BrowserView): | ||
""" | ||
Substituicao do path pela url do site. | ||
Utilizado para o tratamento do valor obtido do metadado getRemoteUrl | ||
via portal_catalog e pela visao padrao do tipo Link (link_redirect_view). | ||
Demanda PR para correção da issue 463: | ||
https://github.com/plonegovbr/brasil.gov.portal/issues/463 | ||
""" | ||
implements(IRemoteUrlUtils) | ||
|
||
def __init__(self, context, request): | ||
self.context = context | ||
self.request = request | ||
self.portal_state = getMultiAdapter( | ||
(self.context, self.request), | ||
name=u'plone_portal_state', | ||
) | ||
self.portal_path = self.portal_state.navigation_root_path() | ||
self.portal_url = self.portal_state.portal_url() | ||
|
||
def _url_uses_scheme(self, url=None): | ||
for scheme in NON_RESOLVABLE_URL_SCHEMES: | ||
if url.startswith(scheme): | ||
return True | ||
return False | ||
|
||
def remote_url_transform(self, path=None, url=None): | ||
"""Transforma o path em url do site.""" | ||
if url: | ||
# http:// https:// | ||
if url.startswith('http://') or url.startswith('https://'): | ||
return url | ||
# file: ftp: mailto: webdav: ... | ||
if self._url_uses_scheme(url): | ||
return url | ||
# ./ ../ | ||
if path: | ||
if url.startswith('.'): | ||
path_items = path.split('/') | ||
url_items = url.split('/') | ||
# ./ | ||
if url_items[0] == '.': | ||
url = url.replace( | ||
'./', | ||
'/'.join(path_items[:-1]) + '/', | ||
) | ||
# ../ ../../../ | ||
elif url_items[0] == '..': | ||
count = url.count('../') | ||
position = count + 1 | ||
url = url.replace( | ||
'../' * count, | ||
'/'.join(path_items[:-position]) + '/', | ||
) | ||
# /path/site | ||
url = url.replace(self.portal_path, self.portal_url) | ||
return url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.