dataviz: fix access to context_processors on range template evaluation (#65908)

This commit is contained in:
Valentin Deniaud 2022-06-02 12:02:17 +02:00
parent 827f68b629
commit c25139c7d8
2 changed files with 14 additions and 6 deletions

View File

@ -26,7 +26,7 @@ from dateutil.relativedelta import MO, relativedelta
from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.db import models, transaction
from django.template import Context, RequestContext, Template, TemplateSyntaxError, VariableDoesNotExist
from django.template import RequestContext, Template, TemplateSyntaxError, VariableDoesNotExist
from django.template.defaultfilters import date as format_date
from django.urls import reverse
from django.utils import timezone
@ -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

View File

@ -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):