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

zopefoundation / Zope / 10365406565

13 Aug 2024 07:23AM UTC coverage: 82.876% (+0.6%) from 82.268%
10365406565

push

github

web-flow
Fix MANIFEST.in (#1222)

5525 of 8123 branches covered (68.02%)

Branch coverage included in aggregate %.

28030 of 32365 relevant lines covered (86.61%)

0.87 hits per line

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

61.29
/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
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 import zpublish
1✔
32
from ZPublisher.HTTPRequest import default_encoding
1✔
33

34

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

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

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

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

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

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

85
        if result is None:
1!
86
            result = []
1✔
87

88
            if obj_metatypes and 'all' in obj_metatypes:
1!
89
                obj_metatypes = None
×
90

91
            if obj_mtime and isinstance(obj_mtime, str):
1!
92
                obj_mtime = DateTime(obj_mtime).timeTime()
×
93

94
            if obj_permission:
1!
95
                obj_permission = getPermissionIdentifier(obj_permission)
×
96

97
            if obj_roles and isinstance(obj_roles, str):
1!
98
                obj_roles = [obj_roles]
×
99

100
            if obj_expr:
1!
101
                # Setup expr machinations
102
                md = td()
×
103
                obj_expr = (Eval(obj_expr), md, md._push, md._pop)
×
104

105
        base = aq_base(obj)
1✔
106

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

114
        try:
1✔
115
            add_result = result.append
1✔
116
        except Exception:
×
117
            raise AttributeError(repr(result))
×
118

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

125
            dflag = 0
1✔
126
            if hasattr(ob, '_p_changed') and (ob._p_changed is None):
1!
127
                dflag = 1
×
128

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

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

167
                if apply_func:
1✔
168
                    apply_func(ob, (apply_path + '/' + p))
1✔
169
                else:
170
                    add_result((p, ob))
1✔
171
                    dflag = 0
1✔
172

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

184
        return result
1✔
185

186

187
InitializeClass(FindSupport)
1✔
188

189

190
class td(RestrictedDTML, TemplateDict):
1✔
191
    pass
1✔
192

193

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

203

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

209

210
def role_match(ob, permission, roles, lt=type([]), tt=type(())):
1✔
211
    pr = []
×
212
    fn = pr.append
×
213

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

230
        if hasattr(ob, '__parent__'):
×
231
            ob = aq_parent(ob)
×
232
            continue
×
233
        break
×
234

235
    for role in roles:
×
236
        if not (role in pr):
×
237
            return 0
×
238
    return 1
×
239

240

241
def absattr(attr):
1✔
242
    if callable(attr):
1!
243
        return attr()
×
244
    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