From beb6765cb8c5f37b9ec1164761e8eb89c6ade35a Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 2 Jun 2022 12:02:17 +0200 Subject: [PATCH] dataviz: fix access to context_processors on range template evaluation (#65908) --- combo/apps/dataviz/models.py | 2 +- tests/test_dataviz.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 7b36d6e2..c15816b0 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -441,7 +441,7 @@ class ChartNgCell(CellBase): context = self.request_context context.update({'now': datetime.now, 'today': datetime.now}) try: - return Template('{{ %s|date:"Y-m-d" }}' % value).render(Context(context)) + return Template('{{ %s|date:"Y-m-d" }}' % value).render(context) except (VariableDoesNotExist, TemplateSyntaxError): return None diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 495f59b9..917157ee 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -1,6 +1,6 @@ +import datetime import json import urllib.parse -from datetime import date from unittest import mock import pytest @@ -1391,8 +1391,8 @@ def test_chartng_cell_manager_new_api(app, admin_user, new_api_statistics): manager_submit_cell(resp.form) cell.refresh_from_db() assert cell.time_range == 'range' - assert cell.time_range_start == date(year=2020, month=10, day=1) - assert cell.time_range_end == date(year=2020, month=11, day=3) + assert cell.time_range_start == datetime.date(year=2020, month=10, day=1) + assert cell.time_range_end == datetime.date(year=2020, month=11, day=3) resp.form[field_prefix + 'time_range_start'] = '' resp.form[field_prefix + 'time_range_end'] = '' @@ -1855,7 +1855,8 @@ def test_dataviz_api_list_statistics(new_api_statistics, settings): @with_httmock(new_api_mock) @pytest.mark.parametrize('date', ['2020-03-02 12:01', '2020-03-05 12:01']) # Monday and Thursday -def test_chartng_cell_new_api_filter_params(app, new_api_statistics, nocache, freezer, date): +def test_chartng_cell_new_api_filter_params(app, new_api_statistics, nocache, freezer, date, settings): + settings.TEMPLATE_VARS['test_var'] = datetime.date(year=2020, month=10, day=1) page = Page.objects.create( title='One', slug='index', @@ -1864,6 +1865,7 @@ def test_chartng_cell_new_api_filter_params(app, new_api_statistics, nocache, fr 'from-request': '{{ request.GET.test }}', 'not-a-date': 'not-a-date', 'syntax-error': '{% for %}', + 'from-template-vars': '{{ test_var }}', }, ) cell = ChartNgCell(page=page, order=1, placeholder='content') @@ -1981,6 +1983,12 @@ def test_chartng_cell_new_api_filter_params(app, new_api_statistics, nocache, fr request = new_api_mock.call['requests'][-1] assert 'start=2022-05-17' in request.url + cell.time_range_start_template = 'from-template-vars' + cell.save() + app.get(location) + request = new_api_mock.call['requests'][-1] + assert 'start=2020-10-01' 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