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

zopefoundation / Zope / 6263629025

21 Sep 2023 03:12PM UTC coverage: 82.146% (-0.01%) from 82.159%
6263629025

Pull #1164

github

web-flow
[pre-commit.ci lite] apply automatic fixes
Pull Request #1164: Move all linters to pre-commit.

4353 of 6963 branches covered (0.0%)

Branch coverage included in aggregate %.

487 of 487 new or added lines in 186 files covered. (100.0%)

27394 of 31684 relevant lines covered (86.46%)

0.86 hits per line

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

58.5
/src/OFS/FindSupport.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE.
11
#
12
##############################################################################
13
"""Find support."""
1✔
14
from AccessControl import ClassSecurityInfo
1✔
15
from AccessControl.class_init import InitializeClass
1✔
16
from AccessControl.Permission import getPermissionIdentifier
1✔
17
from AccessControl.Permissions import view_management_screens
1✔
18
from AccessControl.tainted import TaintedString
1✔
19
from Acquisition import aq_base
1✔
20
from Acquisition import aq_parent
1✔
21
from App.special_dtml import DTMLFile
1✔
22
from DateTime.DateTime import DateTime
1✔
23
from DocumentTemplate._DocumentTemplate import InstanceDict
1✔
24
from DocumentTemplate._DocumentTemplate import TemplateDict
1✔
25
from DocumentTemplate.DT_Util import Eval
1✔
26
from DocumentTemplate.security import RestrictedDTML
1✔
27
from ExtensionClass import Base
1✔
28
from OFS.interfaces import IFindSupport
1✔
29
from zope.interface import implementer
1✔
30
from ZPublisher.HTTPRequest import default_encoding
1✔
31

32

33
@implementer(IFindSupport)
1✔
34
class FindSupport(Base):
1✔
35
    """Find support for Zope Folders."""
36

37
    manage_options = ()
1✔
38
    security = ClassSecurityInfo()
1✔
39

40
    security.declareProtected(view_management_screens, 'manage_findForm')  # NOQA: D001,E501
1✔
41
    manage_findForm = DTMLFile(
1✔
42
        'dtml/findForm',
43
        globals(),
44
        management_view='Find',
45
    )
46

47
    manage_options = (
1✔
48
        {
49
            'label': 'Find',
50
            'action': 'manage_findForm',
51
        },
52
    )
53

54
    @security.protected(view_management_screens)
1✔
55
    def ZopeFind(self, obj, obj_ids=None, obj_metatypes=None,
1✔
56
                 obj_searchterm=None, obj_expr=None,
57
                 obj_mtime=None, obj_mspec=None,
58
                 obj_permission=None, obj_roles=None,
59
                 search_sub=0,
60
                 REQUEST=None, result=None, pre=''):
61
        """Zope Find interface."""
62
        return self.ZopeFindAndApply(
1✔
63
            obj, obj_ids=obj_ids,
64
            obj_metatypes=obj_metatypes, obj_searchterm=obj_searchterm,
65
            obj_expr=obj_expr, obj_mtime=obj_mtime, obj_mspec=obj_mspec,
66
            obj_permission=obj_permission, obj_roles=obj_roles,
67
            search_sub=search_sub, REQUEST=REQUEST, result=result,
68
            pre=pre, apply_func=None, apply_path=''
69
        )
70

71
    @security.protected(view_management_screens)
1✔
72
    def ZopeFindAndApply(self, obj, obj_ids=None, obj_metatypes=None,
1✔
73
                         obj_searchterm=None, obj_expr=None,
74
                         obj_mtime=None, obj_mspec=None,
75
                         obj_permission=None, obj_roles=None,
76
                         search_sub=0,
77
                         REQUEST=None, result=None, pre='',
78
                         apply_func=None, apply_path=''):
79
        """Zope Find interface and apply."""
80

81
        if result is None:
1!
82
            result = []
1✔
83

84
            if obj_metatypes and 'all' in obj_metatypes:
1!
85
                obj_metatypes = None
×
86

87
            if obj_mtime and isinstance(obj_mtime, str):
1!
88
                obj_mtime = DateTime(obj_mtime).timeTime()
×
89

90
            if obj_permission:
1!
91
                obj_permission = getPermissionIdentifier(obj_permission)
×
92

93
            if obj_roles and isinstance(obj_roles, str):
1!
94
                obj_roles = [obj_roles]
×
95

96
            if obj_expr:
1!
97
                # Setup expr machinations
98
                md = td()
×
99
                obj_expr = (Eval(obj_expr), md, md._push, md._pop)
×
100

101
        base = aq_base(obj)
1✔
102

103
        if not hasattr(base, 'objectItems'):
1!
104
            return result
×
105
        try:
1✔
106
            items = obj.objectItems()
1✔
107
        except Exception:
×
108
            return result
×
109

110
        try:
1✔
111
            add_result = result.append
1✔
112
        except Exception:
×
113
            raise AttributeError(repr(result))
×
114

115
        for id, ob in items:
1✔
116
            if pre:
1!
117
                p = f"{pre}/{id}"
×
118
            else:
119
                p = id
1✔
120

121
            dflag = 0
1✔
122
            if hasattr(ob, '_p_changed') and (ob._p_changed is None):
1!
123
                dflag = 1
×
124

125
            bs = aq_base(ob)
1✔
126
            if obj_searchterm:
1✔
127
                if isinstance(obj_searchterm, TaintedString):
1✔
128
                    obj_searchterm = str(obj_searchterm)
1✔
129
                    if not isinstance(obj_searchterm, str):
1!
130
                        obj_searchterm = obj_searchterm.decode(
×
131
                            default_encoding)
132
                if hasattr(ob, 'PrincipiaSearchSource'):
1!
133
                    pss = ob.PrincipiaSearchSource()
1✔
134
                    if not isinstance(pss, str):
1✔
135
                        try:
1✔
136
                            pss = pss.decode(default_encoding)
1✔
137
                        except UnicodeDecodeError:
1✔
138
                            pss = ''
1✔
139
                if hasattr(ob, 'SearchableText'):
1✔
140
                    st = ob.SearchableText()
1✔
141
                    if not isinstance(st, str):
1!
142
                        try:
1✔
143
                            st = st.decode(default_encoding)
1✔
144
                        except UnicodeDecodeError:
1✔
145
                            st = ''
1✔
146
            else:
147
                pss = st = ''
1✔
148

149
            if ((not obj_ids or absattr(bs.getId()) in obj_ids)
1✔
150
                and (not obj_metatypes
151
                     or (hasattr(bs, 'meta_type')
152
                         and bs.meta_type in obj_metatypes))
153
                and (not obj_searchterm
154
                     or (hasattr(ob, 'PrincipiaSearchSource')
155
                         and obj_searchterm in pss)
156
                     or (hasattr(ob, 'SearchableText')
157
                         and obj_searchterm in st))
158
                and (not obj_expr or expr_match(ob, obj_expr))
159
                and (not obj_mtime or mtime_match(ob, obj_mtime, obj_mspec))
160
                and ((not obj_permission or not obj_roles)
161
                     or role_match(ob, obj_permission, obj_roles))):
162

163
                if apply_func:
1✔
164
                    apply_func(ob, (apply_path + '/' + p))
1✔
165
                else:
166
                    add_result((p, ob))
1✔
167
                    dflag = 0
1✔
168

169
            if search_sub and hasattr(bs, 'objectItems'):
1!
170
                self.ZopeFindAndApply(ob, obj_ids, obj_metatypes,
×
171
                                      obj_searchterm, obj_expr,
172
                                      obj_mtime, obj_mspec,
173
                                      obj_permission, obj_roles,
174
                                      search_sub,
175
                                      REQUEST, result, p,
176
                                      apply_func, apply_path)
177
            if dflag:
1!
178
                ob._p_deactivate()
×
179

180
        return result
1✔
181

182

183
InitializeClass(FindSupport)
1✔
184

185

186
class td(RestrictedDTML, TemplateDict):
1✔
187
    pass
1✔
188

189

190
def expr_match(ob, ed, c=InstanceDict, r=0):
1✔
191
    e, md, push, pop = ed
×
192
    push(c(ob, md))
×
193
    try:
×
194
        r = e.eval(md)
×
195
    finally:
196
        pop()
×
197
        return r
×
198

199

200
def mtime_match(ob, t, q, fn=hasattr):
1✔
201
    if not fn(ob, '_p_mtime'):
×
202
        return 0
×
203
    return q == '<' and (ob._p_mtime < t) or (ob._p_mtime > t)
×
204

205

206
def role_match(ob, permission, roles, lt=type([]), tt=type(())):
1✔
207
    pr = []
×
208
    fn = pr.append
×
209

210
    while True:
211
        if hasattr(ob, permission):
×
212
            p = getattr(ob, permission)
×
213
            if isinstance(p, lt):
×
214
                list(map(fn, p))
×
215
                if hasattr(ob, '__parent__'):
×
216
                    ob = aq_parent(ob)
×
217
                    continue
×
218
                break
×
219
            if isinstance(p, tt):
×
220
                list(map(fn, p))
×
221
                break
×
222
            if p is None:
×
223
                list(map(fn, ('Manager', 'Anonymous')))
×
224
                break
×
225

226
        if hasattr(ob, '__parent__'):
×
227
            ob = aq_parent(ob)
×
228
            continue
×
229
        break
×
230

231
    for role in roles:
×
232
        if not (role in pr):
×
233
            return 0
×
234
    return 1
×
235

236

237
def absattr(attr):
1✔
238
    if callable(attr):
1!
239
        return attr()
×
240
    return attr
1✔
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