combo/combo/apps/dataviz/__init__.py

84 lines
3.1 KiB
Python

# combo - content management system
# Copyright (C) 2015 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/>.
import django.apps
from django.core import checks
from django.conf import settings
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from combo.utils import requests
class AppConfig(django.apps.AppConfig):
name = 'combo.apps.dataviz'
verbose_name = _('Data Visualisation')
def get_before_urls(self):
from . import urls
return urls.urlpatterns
def hourly(self):
self.update_available_statistics()
def update_available_statistics(self):
from .models import Statistic, ChartNgCell
if not settings.KNOWN_SERVICES:
return
start_update = timezone.now()
for provider in settings.STATISTICS_PROVIDERS:
if isinstance(provider, dict):
url = provider['url']
sites = {provider['id']: {'title': provider['name']}}
provider = provider['id']
else:
sites = settings.KNOWN_SERVICES.get(provider, {})
url = '/visualization/json/' if provider == 'bijoe' else '/api/statistics/'
for site_key, site_dict in sites.items():
site_title = site_dict.pop('title', '')
response = requests.get(
url, remote_service=site_dict, without_user=True, headers={'accept': 'application/json'}
)
if response.status_code != 200:
continue
result = response.json()
if isinstance(result, dict):
result = result['data'] # detect new api
for stat in result:
Statistic.objects.update_or_create(
slug=stat.get('slug') or stat['id'],
site_slug=site_key,
service_slug=provider,
defaults={
'label': stat['name'],
'url': stat.get('data-url') or stat['url'],
'site_title': site_title,
'filters': stat.get('filters', []),
'available': True,
}
)
Statistic.objects.filter(last_update__lt=start_update).update(available=False)
for cell in ChartNgCell.objects.all():
cell.check_validity()
default_app_config = 'combo.apps.dataviz.AppConfig'