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

zopefoundation / Products.ZSQLMethods / 12348259030

06 Jun 2024 07:13AM UTC coverage: 73.402% (-0.8%) from 74.196%
12348259030

push

github

web-flow
Drop support for Python 3.7. (#49)

337 of 630 branches covered (53.49%)

Branch coverage included in aggregate %.

2144 of 2750 relevant lines covered (77.96%)

0.78 hits per line

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

57.42
/src/Shared/DC/ZRDB/Search.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
"""Search Interface Wizard"""
14

15
from html import escape
1✔
16

17
from AccessControl import getSecurityManager
1✔
18
from App.special_dtml import DTMLFile
1✔
19
from OFS.DTMLMethod import addDTMLMethod
1✔
20
from Products.PageTemplates.ZopePageTemplate import manage_addPageTemplate
1✔
21
from zExceptions import BadRequest
1✔
22
from zExceptions import Unauthorized
1✔
23

24
from .Aqueduct import Args
1✔
25
from .Aqueduct import custom_default_report
1✔
26
from .Aqueduct import custom_default_zpt_report
1✔
27
from .Aqueduct import nicify
1✔
28

29

30
addForm = DTMLFile('dtml/searchAdd', globals())
1✔
31

32

33
def manage_addZSearch(self, report_id, report_title, report_style,
1✔
34
                      input_id, input_title, object_type, queries=[],
35
                      REQUEST=None):
36
    """add a report"""
37

38
    if not queries:
1!
39
        raise BadRequest('No <em>searchable objects</em> were selected')
×
40

41
    if not report_id:
1!
42
        raise BadRequest('No <em>report id</em> were specified')
×
43

44
    if input_title and not input_id:
1!
45
        raise BadRequest('No <em>input id</em> were specified')
×
46

47
    qs = list(map(lambda q, self=self: _getquery(self, q), queries))
1✔
48
    arguments = {}
1✔
49
    keys = []
1✔
50

51
    checkPermission = getSecurityManager().checkPermission
1✔
52

53
    for q in qs:
1✔
54
        url = q.absolute_url()
1✔
55
        if input_id:
1!
56
            for name, arg in q._searchable_arguments().items():
1✔
57
                if len(qs) > 1:
1!
58
                    key = f'{id}/{name}'
1✔
59
                else:
60
                    key = name
×
61
                arguments[key] = arg
1✔
62
                keys.append(key)
1✔
63
        if q._searchable_result_columns() is None:
1!
64
            raise BadRequest(
×
65
                """The input searchable object, <em>{}</em>,
66
                has not been tested.  Until it has been tested,
67
                it\'s output schema is unknown, and a report
68
                cannot be generated.  Before creating a report
69
                from this query, you must try out the query.  To
70
                try out the query, <a href="{}">click here</a>.
71
                """.format(escape(q.title_and_id()), escape(url, 1)))
72

73
    if object_type == 'dtml_methods':
1✔
74

75
        if not checkPermission('Add DTML Methods', self):
1!
76
            raise Unauthorized('You are not authorized to add DTML Methods.')
×
77

78
        if input_id:
1!
79
            arguments = Args(arguments, keys)
1✔
80
            addDTMLMethod(self, input_id, input_title,
1✔
81
                          default_input_form(arguments, report_id))
82

83
        reports = [custom_default_report(x.id, x, no_table=report_style)
1✔
84
                   for x in qs]
85
        addDTMLMethod(
1✔
86
            self, report_id, report_title,
87
            ('<html><head><title><dtml-var title_or_id></title>'
88
             '</head><body bgcolor="#FFFFFF">\n%s\n'
89
             '</body></html>' %
90
             '\n<hr>\n'.join(reports)))
91

92
        if REQUEST:
1!
93
            return self.manage_main(self, REQUEST)
×
94

95
    elif object_type == 'page_templates':
1!
96

97
        if not checkPermission('Add Page Templates', self):
1!
98
            raise Unauthorized('You are not authorized to add Page Templates.')
×
99

100
        if input_id:
1!
101
            arguments = Args(arguments, keys)
1✔
102
            manage_addPageTemplate(
1✔
103
                self, input_id, input_title,
104
                default_input_zpt_form(arguments, report_id))
105

106
        reports = [custom_default_zpt_report(x.id, x, no_table=report_style)
1✔
107
                   for x in qs]
108
        manage_addPageTemplate(
1✔
109
            self, report_id, report_title,
110
            ('<html><body>\n%s\n'
111
             '</body></html>' %
112
             '\n<hr>\n'.join(reports)))
113

114
        if REQUEST:
1!
115
            return self.manage_main(self, REQUEST)
×
116

117

118
def ZQueryIds(self):
1✔
119
    # Note that report server configurations will expend on this
120
    t = []
×
121
    ids = {}
×
122
    old = ids.__contains__
×
123
    o = self
×
124
    n = 0
×
125
    while 1:
×
126

127
        # Look for queries
128
        try:
×
129
            map = o.objectMap()
×
130
        except AttributeError:
×
131
            map = ()
×
132

133
        for i in map:
×
134
            try:
×
135
                id = i['id']
×
136
                if not old(id) and \
×
137
                   hasattr(getattr(o, id), '_searchable_arguments'):
138
                    t.append(i['id'])
×
139
                    ids[id] = 1
×
140
            except Exception:
×
141
                pass
×
142

143
        # Now extend search to parent
144
        try:
×
145
            o = o.aq_parent
×
146
        except Exception:
×
147
            return sorted(t)
×
148

149
        if n > 100:
×
150
            return sorted(t)  # Seat belt
×
151

152
        n = n + 1
×
153

154

155
def _getquery(self, id):
1✔
156

157
    o = self
1✔
158
    i = 0
1✔
159
    while 1:
1✔
160
        __traceback_info__ = o
1✔
161
        q = getattr(o, id)
1✔
162
        try:
1✔
163
            if hasattr(q, '_searchable_arguments'):
1!
164
                try:
1✔
165
                    q = q.__of__(self.aq_parent)
1✔
166
                except Exception:
1✔
167
                    pass
1✔
168
                return q
1✔
169
        except Exception:
×
170
            pass
×
171
        if i > 100:
×
172
            raise AttributeError(id)
×
173
        i = i + 1
×
174
        o = o.aq_parent
×
175

176

177
def default_input_form(arguments, action='query',
1✔
178
                       tabs=''):
179
    if arguments:
1!
180
        items = arguments.items()
1✔
181
        return (
1✔
182
            '{}\n{}{}'.format(
183
                '<html><head><title><dtml-var title_or_id></title>'
184
                '</head><body bgcolor="#FFFFFF">\n%s\n'
185
                '<form action="%s" method="get">\n'
186
                '<h2><dtml-var document_title></h2>\n'
187
                'Enter query parameters:<br>'
188
                '<table>\n'
189
                % (tabs, action),
190
                '\n'.join(
191
                    map(
192
                        lambda a:
193
                        ('<tr><th>%s</th>\n'
194
                         '    <td><input name="%s"\n'
195
                         '               size="30" value="%s">'
196
                         '</td></tr>'
197
                         % (nicify(a[0]),
198
                            'type' in a[1]
199
                            and ('{}:{}'.format(a[0], a[1]['type'])) or a[0],
200
                            'default' in a[1] and a[1]['default'] or '',
201
                            )), items)),
202
                '\n<tr><td colspan=2 align=center>\n'
203
                '<input type="SUBMIT" name="SUBMIT" value="Submit Query">\n'
204
                '</td></tr>\n</table>\n</form>\n'
205
                '</body></html>\n'))
206
    else:
207
        return (
×
208
            '<html><head><title><dtml-var title_or_id></title>'
209
            '</head><body bgcolor="#FFFFFF">\n%s\n'
210
            '<form action="%s" method="get">\n'
211
            '<h2><dtml-var document_title></h2>\n'
212
            'This query requires no input.<p>\n'
213
            '<input type="SUBMIT" name="SUBMIT" value="Submit Query">\n'
214
            '</form>\n'
215
            '</body></html>\n' % (tabs, action))
216

217

218
def default_input_zpt_form(arguments, action='query', tabs=''):
1✔
219
    if arguments:
1!
220
        items = arguments.items()
1✔
221
        return (
1✔
222
            '{}\n{}{}'.format(
223
                '<html><body>\n%s\n'
224
                '<form action="%s" method="get">\n'
225
                '<h2 tal:content="template/title_or_id">Title</h2>\n'
226
                'Enter query parameters:<br>'
227
                '<table>\n'
228
                % (tabs, action),
229
                '\n'.join(
230
                    map(
231
                        lambda a:
232
                        ('<tr><th>%s</th>\n'
233
                         '    <td><input name="%s"\n'
234
                         '               size="30" value="%s">'
235
                         '</td></tr>'
236
                         % (nicify(a[0]),
237
                            'type' in a[1]
238
                            and ('{}:{}'.format(a[0], a[1]['type'])) or a[0],
239
                            'default' in a[1] and a[1]['default'] or '',
240
                            )), items)),
241
                '\n<tr><td colspan=2 align=center>\n'
242
                '<input type="SUBMIT" name="SUBMIT" value="Submit Query">\n'
243
                '</td></tr>\n</table>\n</form>\n'
244
                '</body></html>\n'))
245
    else:
246
        return (
×
247
            '<html><body>\n%s\n'
248
            '<form action="%s" method="get">\n'
249
            '<h2 tal:content="template/title_or_id">Title</h2>\n'
250
            '<p>This query requires no input.</p>\n'
251
            '<input type="SUBMIT" name="SUBMIT" value="Submit Query">\n'
252
            '</form>\n'
253
            '</body></html>\n' % (tabs, action))
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