Skip to content

Commit

Permalink
In Link.selected, use request.view_name instead of request.url (#514)
Browse files Browse the repository at this point in the history
* In Link.selected,  use request.view_name instead of request.url

* No need to calculate URLs to see if link is selected

* Simplify Link.selected code, use request.view_name instead of request.url

* Added CHANGELOG entry
  • Loading branch information
disko committed Nov 14, 2016
2 parents da2c065 + 69d5b72 commit 06c668b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 45 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Change History
a defined add_view. This avoids a hard crash with, for example, a content
type derived from Content that has no add_view defined.

- Change: simplify `kotti.util.LinkBase.selected()`: use request.view_name
instead of deriving the view name from request.url. Also, consider the View
editor bar entry as selected even when the url doesn't end with a slash '/'

1.3.0 - 2016-10-10
------------------

Expand Down
2 changes: 1 addition & 1 deletion kotti/templates/editor-bar.pt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<li tal:replace="api.render_view('workflow-dropdown')" />
</tal:condition>

<li class="${api.url() == request.url and 'active' or None}">
<li class="${api.url() == (request.url.endswith('/') and request.url or request.url + '/') and 'active' or None}">
<a href="${api.url()}" i18n:translate="">View</a>
</li>

Expand Down
37 changes: 4 additions & 33 deletions kotti/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,44 +140,15 @@ def test_link_selected(self):
from kotti.testing import DummyRequest

req = DummyRequest()
req.url = "http://example.com/@@manage"
req.view_name = "manage"

assert Link('manage').selected(Mock(__name__=None), req)

req.url = "http://example.com/@@manage_cats"
req.view_name = 'manage_cats'
assert not Link('manage').selected(Mock(__name__=None), req)

def test_link_selected_no_view_markers(self):
from kotti.util import Link
from kotti.testing import DummyRequest
from mock import Mock

req = DummyRequest()
root = Mock(__name__=None)
manage = Mock(__name__='manage',
__parent__=Mock(__name__=None))

req.url = "http://example.com/manage"
assert Link('manage').selected(root, req)

req.url = "http://example.com/manage/"
assert not Link('manage').selected(root, req)

req.url = "http://example.com/"
assert Link('').selected(root, req)

req.url = "http://example.com/manage/"
link = Link('')
assert link.selected(manage, req)

req.url = "http://example.com/manage"
assert not link.selected(manage, req)

req.url = "http://example.com/"
assert link.selected(root, req)

req.url = "http://example.com"
assert link.selected(root, req)
req.view_name = ''
assert Link('').selected(Mock(__name__=None), req)

def test_link_target(self):
from kotti.util import Link
Expand Down
18 changes: 7 additions & 11 deletions kotti/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,9 @@ def selected(self, context, request):
If the link name is '', it will be selected for all urls ending in '/'
"""
parsed = urlparse(unquote(request.url))

# insert view markers @@ in last component of the path
path = parsed.path.split('/')
if '@@' not in path[-1]:
path[-1] = '@@' + path[-1]
path = '/'.join(path)
url = urlunparse((parsed[0], parsed[1], path, '', '', ''))

return url == self.url(context, request)
if request.view_name is not None:
return request.view_name == self.name
return False

def permitted(self, context, request):
from kotti.security import view_permitted
Expand Down Expand Up @@ -197,7 +190,10 @@ def __init__(self, name, title=None, predicate=None, target=None):
self.target = target

def url(self, context, request):
return resource_url(context, request) + '@@' + self.name
if self.name:
return resource_url(context, request) + '@@' + self.name
else:
return resource_url(context, request)

def __eq__(self, other):
return isinstance(other, Link) and repr(self) == repr(other)
Expand Down

0 comments on commit 06c668b

Please sign in to comment.