• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.74
/src/OFS/absoluteurl.py
1
##############################################################################
2
#
3
# Copyright (c) 2004, 2005 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14

15
from urllib.parse import quote
1✔
16

17
from Acquisition import aq_parent
1✔
18
from OFS.interfaces import ITraversable
1✔
19
from zope.component import getMultiAdapter
1✔
20
from zope.interface import implementer
1✔
21
from zope.publisher.browser import BrowserView
1✔
22
from zope.traversing.browser.absoluteurl import _insufficientContext
1✔
23
from zope.traversing.browser.absoluteurl import _safe
1✔
24
from zope.traversing.browser.interfaces import IAbsoluteURL
1✔
25

26

27
@implementer(IAbsoluteURL)
1✔
28
class AbsoluteURL(BrowserView):
1✔
29
    """An absolute_url adapter for generic objects in Zope 2 that
30
    aren't OFS.Traversable (e.g. views, resources, etc.).
31

32
    This is very close to the generic implementation from
33
    zope.traversing.browser, but the Zope 2 request doesn't support
34
    all the methods that it uses yet.
35
    """
36

37
    def __str__(self):
1✔
38
        context = self.context
1✔
39
        request = self.request
1✔
40

41
        container = aq_parent(context)
1✔
42
        if container is None:
1!
43
            raise TypeError(_insufficientContext)
×
44

45
        url = str(getMultiAdapter((container, request), name='absolute_url'))
1✔
46
        name = self._getContextName(context)
1✔
47
        if name is None:
1!
48
            raise TypeError(_insufficientContext)
×
49

50
        if name:
1!
51
            url += '/' + quote(name.encode('utf-8'), _safe)
1✔
52

53
        return url
1✔
54

55
    __call__ = __str__
1✔
56

57
    def _getContextName(self, context):
1✔
58
        if getattr(context, 'getId', None) is not None:
1!
59
            return context.getId()
1✔
60
        return getattr(context, '__name__', None)
×
61

62
    def breadcrumbs(self):
1✔
63
        context = self.context
×
64
        request = self.request
×
65

66
        # We do this here do maintain the rule that we must be wrapped
67
        container = aq_parent(context)
×
68
        if container is None:
×
69
            raise TypeError(_insufficientContext)
×
70

71
        base = tuple(getMultiAdapter((container, request),
×
72
                                     name='absolute_url').breadcrumbs())
73

74
        name = self._getContextName(context)
×
75
        if name is None:
×
76
            raise TypeError(_insufficientContext)
×
77

78
        if name:
×
79
            url = "{}/{}".format(
×
80
                base[-1]['url'],
81
                quote(name.encode('utf-8'), _safe))
82
            base += ({'name': name,
×
83
                      'url': url}, )
84
        return base
×
85

86

87
@implementer(IAbsoluteURL)
1✔
88
class OFSTraversableAbsoluteURL(BrowserView):
1✔
89
    """An absolute_url adapter for OFS.Traversable subclasses
90
    """
91

92
    def __str__(self):
1✔
93
        return self.context.absolute_url()
1✔
94

95
    __call__ = __str__
1✔
96

97
    def breadcrumbs(self):
1✔
98
        context = self.context
1✔
99
        container = aq_parent(context)
1✔
100
        request = self.request
1✔
101

102
        name = context.getId()
1✔
103

104
        if container is None or \
1✔
105
           self._isVirtualHostRoot() or \
106
           not ITraversable.providedBy(container):
107
            return ({'name': name, 'url': context.absolute_url()},)
1✔
108

109
        view = getMultiAdapter((container, request), IAbsoluteURL)
1✔
110
        base = tuple(view.breadcrumbs())
1✔
111
        base += ({'name': name,
1✔
112
                  'url': "{}/{}".format(base[-1]['url'], name)},)
113

114
        return base
1✔
115

116
    def _isVirtualHostRoot(self):
1✔
117
        virtualrootpath = self.request.get('VirtualRootPhysicalPath', None)
1✔
118
        if virtualrootpath is None:
1✔
119
            return False
1✔
120
        context = self.context
1✔
121
        return context.restrictedTraverse(virtualrootpath) == context
1✔
122

123

124
class RootAbsoluteURL(OFSTraversableAbsoluteURL):
1✔
125
    """An absolute_url adapter for the root object (OFS.Application)
126
    """
127
    def breadcrumbs(self):
1✔
128
        context = self.context
1✔
129

130
        return ({
1✔
131
            'name': context.getId(),
132
            'url': context.absolute_url()
133
        },)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc