Projet

Général

Profil

0001-misc-limit-computed-field-data-source-selection-to-c.patch

Frédéric Péters, 02 août 2022 15:41

Télécharger (8,68 ko)

Voir les différences:

Subject: [PATCH] misc: limit computed field data source selection to cards
 (#64499)

 tests/admin_pages/test_datasource.py | 30 ++++++++++++++++++++
 tests/admin_pages/test_form.py       | 16 +++++++++--
 wcs/admin/data_sources.py            |  3 +-
 wcs/data_sources.py                  | 42 ++++++++++++++++++----------
 wcs/fields.py                        |  6 ++--
 5 files changed, 76 insertions(+), 21 deletions(-)
tests/admin_pages/test_datasource.py
280 280
    assert 'jsonp' in [x[0] for x in resp.form['data_source$type'].options]
281 281

  
282 282

  
283
def test_data_sources_type_options_python(pub):
284
    create_superuser(pub)
285

  
286
    data_source = NamedDataSource(name='foobar')
287
    data_source.store()
288

  
289
    if not pub.site_options.has_section('options'):
290
        pub.site_options.add_section('options')
291
    pub.site_options.set('options', 'disable-python-expressions', 'false')
292
    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
293
        pub.site_options.write(fd)
294

  
295
    app = login(get_app(pub))
296
    resp = app.get('/backoffice/settings/data-sources/%s/edit' % data_source.id)
297
    assert 'python' in [x[0] for x in resp.form['data_source$type'].options]
298

  
299
    pub.site_options.set('options', 'disable-python-expressions', 'true')
300
    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
301
        pub.site_options.write(fd)
302

  
303
    resp = app.get('/backoffice/settings/data-sources/%s/edit' % data_source.id)
304
    assert 'python' not in [x[0] for x in resp.form['data_source$type'].options]
305

  
306
    # make sure it's still displayed for sources using it.
307
    data_source.data_source = {'type': 'formula', 'value': '[]'}
308
    data_source.store()
309
    resp = app.get('/backoffice/settings/data-sources/%s/edit' % data_source.id)
310
    assert 'python' in [x[0] for x in resp.form['data_source$type'].options]
311

  
312

  
283 313
def test_data_sources_agenda_manual_qs_data_type_options(pub):
284 314
    create_superuser(pub)
285 315

  
tests/admin_pages/test_form.py
3241 3241
    resp = resp.forms[0].submit().follow()
3242 3242

  
3243 3243
    assert len(FormDef.get(1).fields) == 1
3244
    assert FormDef.get(1).fields[0].key == 'computed'
3245
    assert FormDef.get(1).fields[0].label == 'foobar'
3246
    assert FormDef.get(1).fields[0].varname == 'foobar'
3244
    field = FormDef.get(1).fields[0]
3245
    assert field.key == 'computed'
3246
    assert field.label == 'foobar'
3247
    assert field.varname == 'foobar'
3248

  
3249
    CardDef.wipe()
3250
    carddef = CardDef()
3251
    carddef.name = 'Baz'
3252
    carddef.digest_templates = {'default': 'plop'}
3253
    carddef.store()
3254
    resp = app.get('/backoffice/forms/%s/fields/%s/' % (formdef.id, field.id))
3255
    # only cards
3256
    assert resp.form['data_source$type'].options == [('None', True, 'None'), ('carddef:baz', False, 'Baz')]
3247 3257

  
3248 3258

  
3249 3259
def test_form_category_management_roles(pub, backoffice_user, backoffice_role):
wcs/admin/data_sources.py
88 88
                'data_source',
89 89
                value=self.datasource.data_source,
90 90
                title=_('Data Source'),
91
                allow_geojson=True,
92
                allow_named_sources=False,
91
                allowed_source_types={'json', 'jsonp', 'geojson', 'python'},
93 92
                required=True,
94 93
            )
95 94
        form.add(
wcs/data_sources.py
54 54

  
55 55

  
56 56
class DataSourceSelectionWidget(CompositeWidget):
57
    def __init__(
58
        self, name, value=None, allow_jsonp=True, allow_geojson=False, allow_named_sources=True, **kwargs
59
    ):
57
    def __init__(self, name, value=None, allowed_source_types=None, disallowed_source_types=None, **kwargs):
58
        if allowed_source_types is None:
59
            allowed_source_types = {'json', 'jsonp', 'geojson', 'named', 'cards', 'python'}
60
        if get_publisher().has_site_option('disable-python-expressions'):
61
            allowed_source_types.remove('python')
62
        if get_publisher().has_site_option('disable-jsonp-sources'):
63
            allowed_source_types.remove('jsonp')
64
        if disallowed_source_types:
65
            allowed_source_types = allowed_source_types.difference(disallowed_source_types)
66

  
60 67
        CompositeWidget.__init__(self, name, value, **kwargs)
61 68

  
62 69
        if not value:
......
64 71

  
65 72
        options = [(None, _('None'), None)]
66 73

  
67
        if allow_named_sources:
74
        if 'cards' in allowed_source_types:
68 75
            from wcs.carddef import CardDef
69 76

  
70 77
            cards_options = [
......
78 85
                options.append(OptGroup(_('Cards')))
79 86
                options.extend(cards_options)
80 87

  
88
        if 'named' in allowed_source_types:
81 89
            admin_accessible = NamedDataSource.is_admin_accessible()
82 90
            nds_options = []
83 91
            nds_agenda_options = []
......
136 144
                        options.append(OptGroup(name or _('Without category')))
137 145
                        options.extend(nds_by_category_names[name])
138 146

  
139
        if len(options) > 1:
147
        generic_options = []
148
        if 'json' in allowed_source_types:
149
            generic_options.append(('json', _('JSON URL'), 'json', {'data-maybe-datetimes': 'true'}))
150
        if 'jsonp' in allowed_source_types:
151
            generic_options.append(('jsonp', _('JSONP URL'), 'jsonp'))
152
        elif value.get('type') == 'jsonp':
153
            generic_options.append(('jsonp', _('JSONP URL (deprecated)'), 'jsonp'))
154
        if 'geojson' in allowed_source_types:
155
            generic_options.append(('geojson', _('GeoJSON URL'), 'geojson'))
156
        if 'python' in allowed_source_types:
157
            generic_options.append(('formula', _('Python Expression'), 'python'))
158
        elif value.get('type') == 'formula':
159
            generic_options.append(('formula', _('Python Expression (deprecated)'), 'python'))
160

  
161
        if len(options) > 1 and generic_options:
140 162
            options.append(OptGroup(_('Generic Data Sources')))
141
        options.append(('json', _('JSON URL'), 'json', {'data-maybe-datetimes': 'true'}))
142
        if allow_jsonp and (
143
            value.get('type') == 'jsonp' or not get_publisher().has_site_option('disable-jsonp-sources')
144
        ):
145
            options.append(('jsonp', _('JSONP URL'), 'jsonp'))
146
        if allow_geojson:
147
            options.append(('geojson', _('GeoJSON URL'), 'geojson'))
148
        if not get_publisher().has_site_option('disable-python-expressions'):
149
            options.append(('formula', _('Python Expression'), 'python'))
163
        options.extend(generic_options)
150 164

  
151 165
        self.add(
152 166
            SingleSelectWidget,
wcs/fields.py
1241 1241
            value=self.data_source,
1242 1242
            title=_('Data Source'),
1243 1243
            hint=_('This will allow autocompletion from an external source.'),
1244
            disallowed_source_types={'geojson'},
1244 1245
            advanced=True,
1245 1246
            required=False,
1246 1247
        )
......
2025 2026
            value=self.data_source,
2026 2027
            required=False,
2027 2028
            hint=_('This will get the available items from an external source.'),
2029
            disallowed_source_types={'geojson'},
2028 2030
            attrs={'data-dynamic-display-child-of': 'data_mode', 'data-dynamic-display-value': 'data-source'},
2029 2031
        )
2030 2032

  
......
3932 3934
            data_sources.DataSourceSelectionWidget,
3933 3935
            'data_source',
3934 3936
            value=self.data_source,
3935
            allow_jsonp=False,
3936
            title=_('Data Source'),
3937
            allowed_source_types={'cards'},
3938
            title=_('Data Source (cards only)'),
3937 3939
            hint=_('This will make linked card data available for expressions.'),
3938 3940
            required=False,
3939 3941
        )
3940
-