# combo - content management system # Copyright (C) 2014-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 . import requests from django.conf import settings from django.core.cache import cache def is_wcs_enabled(cls): return hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('wcs') def get_wcs_services(): if not is_wcs_enabled(None): return {} return settings.KNOWN_SERVICES.get('wcs') def get_wcs_json(wcs_url, path): if not wcs_url.endswith('/'): wcs_url += '/' url = wcs_url + path response_json = cache.get(url) if response_json is None: response_json = requests.get(url, headers={'accept': 'application/json'}).json() cache.set(url, response_json) return response_json def get_wcs_options(url): references = [] for wcs_key, wcs_site in get_wcs_services().iteritems(): site_title = wcs_site.get('title') response_json = get_wcs_json(wcs_site.get('url'), url) if type(response_json) is dict: response_json = response_json.get('data') for element in response_json: slug = element.get('slug') title = element.get('title') if len(get_wcs_services()) == 1: label = title else: label = '%s : %s' % (site_title, title) reference = '%s:%s' % (wcs_key, slug) references.append((reference, label)) references.sort(lambda x, y: cmp(x[1], y[1])) return references