• 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

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
"""
15
from AccessControl import ClassSecurityInfo
1✔
16
from AccessControl.class_init import InitializeClass
1✔
17
from AccessControl.Permission import getPermissionIdentifier
1✔
18
from AccessControl.Permissions import view_management_screens
1✔
19
from AccessControl.tainted import TaintedString
1✔
20
from Acquisition import aq_base
1✔
21
from Acquisition import aq_parent
1✔
22
from App.special_dtml import DTMLFile
1✔
23
from DateTime.DateTime import DateTime
1✔
24
from DocumentTemplate._DocumentTemplate import InstanceDict
1✔
25
from DocumentTemplate._DocumentTemplate import TemplateDict
1✔
26
from DocumentTemplate.DT_Util import Eval
1✔
27
from DocumentTemplate.security import RestrictedDTML
1✔
28
from ExtensionClass import Base
1✔
29
from OFS.interfaces import IFindSupport
1✔
30
from zope.interface import implementer
1✔
31
from ZPublisher.HTTPRequest import default_encoding
1✔
32

33

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

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

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

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

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

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

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

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

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

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

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

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

102
        base = aq_base(obj)
1✔
103

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

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

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

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

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

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

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

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

181
        return result
1✔
182

183

184
InitializeClass(FindSupport)
1✔
185

186

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

190

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

200

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

206

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

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

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

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

237

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