combo/combo/apps/dataviz/views.py

55 lines
2.2 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
from django.utils.translation import ugettext_lazy as _
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(request.user):
raise PermissionDenied()
try:
chart = cell.get_chart(
width=int(request.GET.get('width', 0)) or None,
height=int(request.GET.get('height', 0)) or int(cell.height)
)
except UnsupportedDataSet:
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': _('Unsupported dataset.')}
else:
svg = chart.render()
return HttpResponse(svg, content_type='image/svg+xml')