68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
# combo - content management system
|
|
# Copyright (C) 2015-2019 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.http import HttpResponse, Http404
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from requests.exceptions import HTTPError
|
|
|
|
from combo.utils import get_templated_url, requests
|
|
from .models import Gauge, ChartNgCell, UnsupportedDataSet
|
|
|
|
|
|
def ajax_gauge_count(request, *args, **kwargs):
|
|
gauge = Gauge.objects.get(id=kwargs['cell'])
|
|
response = requests.get(get_templated_url(gauge.data_source))
|
|
return HttpResponse(response.content, content_type='text/json')
|
|
|
|
|
|
def dataviz_graph(request, *args, **kwargs):
|
|
cell = ChartNgCell.objects.get(id=kwargs.get('cell'))
|
|
if not cell.page.is_visible(request.user):
|
|
raise PermissionDenied()
|
|
if not cell.is_visible(user=request.user):
|
|
raise PermissionDenied()
|
|
if not cell.cached_json:
|
|
raise Http404('misconfigured cell')
|
|
error_text = None
|
|
try:
|
|
chart = cell.get_chart(
|
|
width=int(request.GET['width']) if request.GET.get('width') else None,
|
|
height=int(request.GET['height']) if request.GET.get('height') else int(cell.height)
|
|
)
|
|
except UnsupportedDataSet as e:
|
|
error_text = _('Unsupported dataset.')
|
|
except HTTPError as e:
|
|
if e.response.status_code == 404:
|
|
error_text = _('Visualization not found.')
|
|
else:
|
|
error_text = _('Unknown HTTP error: %s' % e)
|
|
|
|
if error_text:
|
|
svg = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
|
|
viewBox="0 0 %(width)s 30" width="%(width)s" height="30">
|
|
<text
|
|
y="20"
|
|
x="10"
|
|
style="font-family: sans-serif; font-size: 16px; fill:#000000;">%(text)s</text>
|
|
</svg>""" % {'width': request.GET.get('width', 200),
|
|
'text': error_text}
|
|
else:
|
|
svg = chart.render()
|
|
return HttpResponse(svg, content_type='image/svg+xml')
|