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

zopefoundation / grokcore.view / 16248935885

18 Jun 2025 06:52AM UTC coverage: 93.234%. Remained the same
16248935885

push

github

icemac
Back to development: 5.1

151 of 180 branches covered (83.89%)

Branch coverage included in aggregate %.

1475 of 1564 relevant lines covered (94.31%)

0.94 hits per line

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

80.77
/src/grokcore/view/util.py
1
##############################################################################
2
#
3
# Copyright (c) 2006-2007 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
"""Grok utility functions.
15
"""
16
from urllib.parse import quote
1✔
17
from urllib.parse import urlencode
1✔
18
from urllib.parse import urlparse
1✔
19
from urllib.parse import urlunparse
1✔
20

21
from grokcore.security.util import check_permission
1✔
22
from zope.component import getMultiAdapter
1✔
23
from zope.contentprovider.interfaces import IContentProvider
1✔
24
from zope.security.checker import NamesChecker
1✔
25
from zope.security.checker import defineChecker
1✔
26
from zope.traversing.browser.absoluteurl import _safe as SAFE_URL_CHARACTERS
1✔
27
from zope.traversing.browser.interfaces import IAbsoluteURL
1✔
28

29
from grokcore.view import directive
1✔
30

31

32
ASIS = object()
1✔
33

34

35
def url(request, obj, name=None, skin=ASIS, data=None):
1✔
36
    url = getMultiAdapter((obj, request), IAbsoluteURL)()
1✔
37
    if name is not None:
1✔
38
        url += '/' + quote(name.encode('utf-8'), SAFE_URL_CHARACTERS)
1✔
39

40
    if skin is not ASIS:
1✔
41
        # Remove whatever ``++skin++[name]`` is active.
42
        parts = list(urlparse(url))
1✔
43
        path = parts[2]
1✔
44
        if path.startswith('/++skin++'):
1!
45
            # Find next / in the path.
46
            idx = path.find('/', 1)
1✔
47
            path = path[idx:]
1✔
48
        if skin is not None:
1✔
49
            # If a skin is set, add ``++skin++`` as the leading path segment.
50
            if isinstance(skin, str):
1✔
51
                path = f'/++skin++{skin}{path}'
1✔
52
            else:
53
                path = '/++skin++{}{}'.format(
1✔
54
                    directive.skin.bind().get(skin), path)
55

56
        parts[2] = path
1✔
57
        url = urlunparse(parts)
1✔
58

59
    if not data:
1✔
60
        return url
1✔
61

62
    if not isinstance(data, dict):
1✔
63
        raise TypeError('url() data argument must be a dict.')
1✔
64

65
    for k, v in data.items():
1✔
66
        if isinstance(v, str):
1✔
67
            data[k] = v.encode('utf-8')
1✔
68
        if isinstance(v, (list, set, tuple)):
1✔
69
            data[k] = [
1✔
70
                isinstance(item, str) and item.encode('utf-8')
71
                or item for item in v]
72

73
    return url + '?' + urlencode(data, doseq=True)
1✔
74

75

76
def render_provider(context, request, view, name):
1✔
77
    provider = getMultiAdapter(
1✔
78
        (context, request, view), interface=IContentProvider, name=name)
79
    provider.update()
1✔
80
    return provider.render()
1✔
81

82

83
def make_checker(factory, view_factory, permission, method_names=None):
1✔
84
    if method_names is None:
×
85
        method_names = ['__call__']
×
86
    if permission is not None:
×
87
        check_permission(factory, permission)
×
88
    if permission is None or permission == 'zope.Public':
×
89
        checker = NamesChecker(method_names)
×
90
    else:
91
        checker = NamesChecker(method_names, permission)
×
92
    defineChecker(view_factory, checker)
×
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