dataviz: allow variables in gauge URLs (#17361)

This commit is contained in:
Frédéric Péters 2017-07-05 15:26:03 +02:00
parent 25317caee3
commit 271c46fd13
3 changed files with 44 additions and 4 deletions

View File

@ -24,7 +24,7 @@ from django.conf import settings
from combo.data.models import CellBase
from combo.data.library import register_cell_class
from combo.utils import NothingInCacheException
from combo.utils import NothingInCacheException, get_templated_url
from . import utils
@ -49,12 +49,12 @@ class Gauge(CellBase):
def get_cell_extra_context(self, context):
if self.jsonp_data_source:
data_source_url = self.data_source
data_source_url = get_templated_url(self.data_source)
else:
data_source_url = reverse('combo-ajax-gauge-count', kwargs={'cell': self.id})
return {'cell': self,
'title': self.title,
'url': self.url,
'url': get_templated_url(self.url) if self.url else None,
'max_value': self.max_value,
'data_source_url': data_source_url,
'jsonp': self.jsonp_data_source,

View File

@ -18,10 +18,11 @@ import requests
from django.http import HttpResponse
from combo.utils import get_templated_url
from .models import Gauge
def ajax_gauge_count(request, *args, **kwargs):
gauge = Gauge.objects.get(id=kwargs['cell'])
response = requests.get(gauge.data_source)
response = requests.get(get_templated_url(gauge.data_source))
return HttpResponse(response.content, content_type='text/json')

39
tests/test_dataviz.py Normal file
View File

@ -0,0 +1,39 @@
import mock
import pytest
from django.test import override_settings
from combo.data.models import Page
from combo.apps.dataviz.models import Gauge
pytestmark = pytest.mark.django_db
@pytest.fixture
def cell():
page = Page(title='One', slug='index')
page.save()
cell = Gauge(page=page, order=0, placeholder='content')
cell.data_source = '[test_url]/XXX'
cell.save()
return cell
def test_jsonp_gauge(app, cell):
with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}):
resp = app.get('/')
assert 'data-gauge-count-jsonp-url="http://www.example.net/XXX"' in resp.body
def test_json_gauge(app, cell):
cell.jsonp_data_source = False
cell.save()
with override_settings(TEMPLATE_VARS={'test_url': 'http://www.example.net'}):
with mock.patch('combo.apps.dataviz.views.requests.get') as requests_get:
requests_get.return_value = mock.Mock(content='xxx', status_code=200)
resp = app.get('/')
assert 'data-gauge-count-url="/ajax/gauge-count/%s/"' % cell.id in resp.body
resp = app.get('/ajax/gauge-count/%s/' % cell.id)
assert resp.content == 'xxx'
assert requests_get.call_args[0][0] == 'http://www.example.net/XXX'