From f9a833b140dff3810b7e581215233d3c838ae1f8 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 17 May 2022 11:56:57 +0200 Subject: [PATCH 2/2] dataviz: pass request in context during page variable evaluation (#65348) --- combo/apps/dataviz/models.py | 4 +++- .../dataviz/templates/combo/chartngcell.html | 18 ++++++++++++++++-- tests/test_dataviz.py | 7 +++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index df295b6e..c0d1cd21 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -460,7 +460,9 @@ class ChartNgCell(CellBase): if not hasattr(self, '_request'): raise MissingRequest - return RequestContext(self._request, self._request.extra_context) + ctx = RequestContext(self._request, self._request.extra_context) + ctx['request'] = self._request + return ctx def parse_response(self, response, chart): # normalize axis to have a fake axis when there are no dimensions and diff --git a/combo/apps/dataviz/templates/combo/chartngcell.html b/combo/apps/dataviz/templates/combo/chartngcell.html index 489ad98c..ae9bed14 100644 --- a/combo/apps/dataviz/templates/combo/chartngcell.html +++ b/combo/apps/dataviz/templates/combo/chartngcell.html @@ -12,6 +12,8 @@ $(function() { qs.push(chart_filters_form.serialize()); if(extra_context) qs.push('ctx=' + extra_context); + if (window.location.search) + qs.push(window.location.search.slice(1)); $.ajax({ url : "{% url 'combo-dataviz-graph' cell=cell.id %}?" + qs.join('&'), type: 'GET', @@ -35,7 +37,13 @@ $(function() { var chart_cell = $('#chart-{{cell.id}}').parent(); var new_width = Math.floor($(chart_cell).width()); var ratio = new_width / last_width; - var qs = '?width=' + new_width + var qs; + if (window.location.search) { + qs = window.location.search + '&'; + } else { + qs = '?'; + } + qs += 'width=' + new_width if(chart_filters_form) qs += '&' + chart_filters_form.serialize() if(extra_context) @@ -46,7 +54,13 @@ $(function() { } }).trigger('combo:resize-graphs'); $(window).on('combo:refresh-graphs', function() { - var qs = '?width=' + last_width + var qs; + if (window.location.search) { + qs = window.location.search + '&'; + } else { + qs = '?'; + } + qs += 'width=' + last_width if(chart_filters_form) qs += '&' + chart_filters_form.serialize() if(extra_context) diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 8b725b18..c7dca24e 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -1849,6 +1849,7 @@ def test_chartng_cell_new_api_filter_params(app, new_api_statistics, nocache, fr slug='index', extra_variables={ 'custom_date': '{{ "2021-02-03"|parse_date }}', + 'from-request': '{{ request.GET.test }}', 'not-a-date': 'not-a-date', 'syntax-error': '{% for %}', }, @@ -1962,6 +1963,12 @@ def test_chartng_cell_new_api_filter_params(app, new_api_statistics, nocache, fr request = new_api_mock.call['requests'][-1] assert 'start' not in request.url + cell.time_range_start_template = 'from-request' + cell.save() + app.get(location + '?test=2022-05-17') + request = new_api_mock.call['requests'][-1] + assert 'start=2022-05-17' in request.url + @with_httmock(new_api_mock) def test_chartng_cell_new_api_filter_params_month(new_api_statistics, nocache, freezer): -- 2.30.2