67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
from django.conf import settings
|
|
|
|
from combo.utils import requests
|
|
|
|
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_site, path):
|
|
if wcs_site is None:
|
|
# no site specified (probably an import referencing a not yet deployed
|
|
# site)
|
|
return {'err': 1, 'err_desc': 'no-wcs-site'}
|
|
response = requests.get(path, remote_service=wcs_site, without_user=True,
|
|
headers={'accept': 'application/json'})
|
|
if response.status_code != 200:
|
|
# the exact error will have been logged during request.
|
|
return {}
|
|
return response.json()
|
|
|
|
def get_wcs_options(url, include_category_slug=False):
|
|
references = []
|
|
for wcs_key, wcs_site in get_wcs_services().items():
|
|
site_title = wcs_site.get('title')
|
|
response_json = get_wcs_json(wcs_site, url)
|
|
if type(response_json) is dict:
|
|
if response_json.get('err') == 1:
|
|
continue
|
|
response_json = response_json.get('data')
|
|
if not response_json:
|
|
continue
|
|
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)
|
|
if include_category_slug:
|
|
reference = '%s:%s:%s' % (wcs_key, element.get('category_slug') or '', slug)
|
|
else:
|
|
reference = '%s:%s' % (wcs_key, slug)
|
|
references.append((reference, label))
|
|
references.sort(key=lambda x: x[1])
|
|
return references
|