Projet

Général

Profil

0001-misc-expose-request-query-string-in-expression-varia.patch

Frédéric Péters, 09 janvier 2019 08:10

Télécharger (6,56 ko)

Voir les différences:

Subject: [PATCH] misc: expose request (/query string) in expression variables
 (#29565)

 .../qommon/forms/widgets/select--test.html    |  3 +-
 tests/test_form_pages.py                      | 41 ++++++++++++++++++-
 wcs/context_processors.py                     |  1 +
 wcs/qommon/http_request.py                    |  3 +-
 wcs/settings.py                               |  1 -
 wcs/variables.py                              | 27 ++++++++++++
 6 files changed, 72 insertions(+), 4 deletions(-)
tests/templates/qommon/forms/widgets/select--test.html
1 1
{% extends "qommon/forms/widget.html" %}
2 2
{% block widget-control %}
3 3
<!-- TEST TEMPLATE -->
4
<!-- backoffice: {{ request.quixote_request.is_in_backoffice|pprint }} -->
4
<!-- backoffice: {{ request.is_in_backoffice|pprint }} -->
5
<!-- backoffice compat: {{ request.quixote_request.is_in_backoffice|pprint }} -->
5 6
<!-- substitution variable: {{ example_url }} -->
6 7
<select id="form_{{widget.name}}" name="{{widget.name}}"
7 8
    {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}"{% endfor %}>
tests/test_form_pages.py
2189 2189
    assert resp.form['f0'].value == ''
2190 2190
    assert 'widget-prefilled' not in resp.body
2191 2191

  
2192
def test_form_page_query_string_prefill(pub):
2192
def test_form_page_session_var_prefill(pub):
2193 2193
    user = create_user(pub)
2194 2194
    formdef = create_formdef()
2195 2195
    formdef.data_class().wipe()
......
2246 2246

  
2247 2247
    os.unlink(os.path.join(pub.app_dir, 'site-options.cfg'))
2248 2248

  
2249
def test_form_page_query_string_list_prefill(pub):
2250
    user = create_user(pub)
2251
    formdef = create_formdef()
2252
    formdef.data_class().wipe()
2253
    formdef.fields = [fields.ItemField(id='1', label='item',
2254
        varname='item', required=False, data_source={'type': 'foobar'},
2255
        prefill={'type': 'string', 'value': '{{request.query_string.preselect}}'},
2256
        )]
2257
    formdef.store()
2258

  
2259
    NamedDataSource.wipe()
2260
    data_source = NamedDataSource(name='foobar')
2261
    data_source.data_source = {'type': 'formula',
2262
            'value': repr([{'id': '1', 'text': 'un'},
2263
                           {'id': '2', 'text': 'deux'},
2264
                           {'id': '3', 'text': 'trois'},
2265
                           {'id': '4', 'text': 'quatre'},
2266
                           ])}
2267
    data_source.store()
2268

  
2269
    resp = get_app(pub).get('/test/')
2270
    assert resp.form['f1'].value == '1'
2271

  
2272
    resp = get_app(pub).get('/test/?preselect=2')
2273
    assert resp.form['f1'].value == '2'
2274
    resp = resp.form.submit('submit')
2275
    resp = resp.form.submit('submit').follow()
2276
    assert 'deux' in resp.body
2277

  
2278
    formdef.fields = [fields.ItemField(id='1', label='item',
2279
        varname='item', required=False, data_source={'type': 'foobar'},
2280
        prefill={'type': 'string', 'value': '{{request.GET.preselect}}'},
2281
        )]
2282
    formdef.store()
2283

  
2284
    resp = get_app(pub).get('/test/?preselect=3')
2285
    assert resp.form['f1'].value == '3'
2286

  
2249 2287
def test_form_page_profile_prefill_list(pub):
2250 2288
    user = create_user(pub)
2251 2289
    formdef = create_formdef()
......
4966 5004
    assert 'TEST TEMPLATE' in resp.body
4967 5005
    # make sure request is available in context
4968 5006
    assert '<!-- backoffice: False -->' in resp.body
5007
    assert '<!-- backoffice compat: False -->' in resp.body
4969 5008

  
4970 5009
    # test for substitution variables being available
4971 5010
    if not pub.site_options.has_section('variables'):
wcs/context_processors.py
25 25
    template_base = 'wcs/base.html'
26 26
    if request.path.startswith('/backoffice/'):
27 27
        template_base = 'wcs/blank.html'
28
    from .variables import LazyRequest
28 29
    return {'publisher': get_publisher,
29 30
            'response': get_response,
30 31
            'user': lambda: get_request() and get_request().user,
wcs/qommon/http_request.py
85 85

  
86 86
    def get_substitution_variables(self):
87 87
        # environment variables APPNAME_* are exported to env_*
88
        from wcs.variables import LazyRequest
88 89
        prefix = get_publisher().APP_NAME.lower() + '_'
89
        variables = {}
90
        variables = {'request': LazyRequest(self)}
90 91
        for k, v in self.environ.items():
91 92
            if k.lower().startswith(prefix):
92 93
                variables['env_' + k.lower()[len(prefix):]] = v
wcs/settings.py
94 94
                "django.template.context_processors.debug",
95 95
                "django.template.context_processors.i18n",
96 96
                "django.template.context_processors.media",
97
                "django.template.context_processors.request",
98 97
                "django.template.context_processors.static",
99 98
                "django.template.context_processors.tz",
100 99
                "django.contrib.messages.context_processors.messages",
wcs/variables.py
486 486
            return super(LazyUser, self).__getattr__(attr)
487 487
        except AttributeError:
488 488
            return getattr(self.user, attr)
489

  
490

  
491
class LazyRequest(object):
492
    def __init__(self, request):
493
        self.request = request
494

  
495
    @property
496
    def quixote_request(self):  # compatibility
497
        return self.request
498

  
499
    @property
500
    def GET(self):
501
        return self.request.django_request.GET
502

  
503
    query_string = GET
504

  
505
    @property
506
    def is_in_backoffice(self):
507
        return self.request.is_in_backoffice()
508

  
509
    @property
510
    def method(self):
511
        return self.request.method
512

  
513
    @property
514
    def user(self):
515
        return self.request.user
489
-