summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2015-05-18 14:18:56 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2015-05-20 09:15:17 (GMT)
commitec06dabf2f15f3c983fce72b5c79a8ebf7152724 (patch)
tree6f33fd01506af9efe6e85d22d25757506182fd09
parentc667a9018af112b3857b834b94bd8a80a6b815d3 (diff)
downloadcombo-ec06dabf2f15f3c983fce72b5c79a8ebf7152724.zip
combo-ec06dabf2f15f3c983fce72b5c79a8ebf7152724.tar.gz
combo-ec06dabf2f15f3c983fce72b5c79a8ebf7152724.tar.bz2
wcs: refactor access to KNOWN_SERVICES to really be uniform (#7273)
-rw-r--r--combo/apps/wcs/models.py25
-rw-r--r--combo/apps/wcs/utils.py12
2 files changed, 21 insertions, 16 deletions
diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 30249dc..d8a4ea1 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -29,10 +29,7 @@ from combo.data.models import CellBase
from combo.data.library import register_cell_class
from combo.utils import NothingInCacheException
-from .utils import get_wcs_json
-
-def is_wcs_enabled(cls):
- return hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('wcs')
+from .utils import get_wcs_json, is_wcs_enabled, get_wcs_services
@register_cell_class
class WcsFormCell(CellBase):
@@ -53,7 +50,7 @@ class WcsFormCell(CellBase):
def save(self, *args, **kwargs):
if self.formdef_reference:
wcs_key, form_slug = self.formdef_reference.split(':')
- wcs_site = settings.KNOWN_SERVICES.get('wcs', {}).get(wcs_key)
+ wcs_site = get_wcs_services().get(wcs_key)
forms_response_json = get_wcs_json(wcs_site.get('url') + 'json')
for form in forms_response_json:
slug = form.get('slug')
@@ -89,7 +86,7 @@ class WcsCommonCategoryCell(CellBase):
def save(self, *args, **kwargs):
if self.category_reference:
wcs_key, category_slug = self.category_reference.split(':')
- wcs_site = settings.KNOWN_SERVICES.get('wcs', {}).get(wcs_key)
+ wcs_site = get_wcs_services().get(wcs_key)
categories_response_json = get_wcs_json(wcs_site.get('url') + 'categories')
for category in categories_response_json.get('data'):
slug = category.get('slug')
@@ -135,7 +132,7 @@ class WcsBlurpMixin(object):
def get_blurp_renderer(self, context):
if self.wcs_site:
try:
- wcs_sites = {self.wcs_site: settings.KNOWN_SERVICES['wcs'][self.wcs_site]}
+ wcs_sites = {self.wcs_site: get_wcs_services()[self.wcs_site]}
except KeyError:
# in case of the site disappeared from settings
return cmsplugin_blurp.utils.create_renderer(self.variable_name, {
@@ -143,7 +140,7 @@ class WcsBlurpMixin(object):
'template': ''
})
else:
- wcs_sites = settings.KNOWN_SERVICES['wcs']
+ wcs_sites = get_wcs_services()
sources = []
for slug, wcs_site in wcs_sites.items():
@@ -195,10 +192,10 @@ class WcsDataBaseCell(CellBase, WcsBlurpMixin):
abstract = True
def get_default_form_class(self):
- if len(settings.KNOWN_SERVICES.get('wcs')) == 1:
+ if len(get_wcs_services()) == 1:
return None
combo_wcs_sites = [('', _('All'))]
- combo_wcs_sites.extend([(x, y.get('title')) for x, y in settings.KNOWN_SERVICES.get('wcs', {}).items()])
+ combo_wcs_sites.extend([(x, y.get('title')) for x, y in get_wcs_services().items()])
return model_forms.modelform_factory(self.__class__,
fields=['wcs_site'],
widgets={'wcs_site': Select(choices=combo_wcs_sites)})
@@ -305,9 +302,9 @@ class TrackingCodeInputCell(CellBase):
verbose_name = _('Tracking Code Input')
def get_default_form_class(self):
- if len(settings.KNOWN_SERVICES.get('wcs')) == 1:
+ if len(get_wcs_services()) == 1:
return None
- combo_wcs_sites = [(x, y.get('title')) for x, y in settings.KNOWN_SERVICES.get('wcs', {}).items()]
+ combo_wcs_sites = [(x, y.get('title')) for x, y in get_wcs_services().items()]
return model_forms.modelform_factory(self.__class__,
fields=['wcs_site'],
widgets={'wcs_site': Select(choices=combo_wcs_sites)})
@@ -315,6 +312,6 @@ class TrackingCodeInputCell(CellBase):
def render(self, context):
tmpl = template.loader.get_template(self.template_name)
if not self.wcs_site:
- self.wcs_site = settings.KNOWN_SERVICES['wcs'].keys()[0]
- context['url'] = settings.KNOWN_SERVICES['wcs'].get(self.wcs_site).get('url')
+ self.wcs_site = get_wcs_services().keys()[0]
+ context['url'] = get_wcs_services().get(self.wcs_site).get('url')
return tmpl.render(context)
diff --git a/combo/apps/wcs/utils.py b/combo/apps/wcs/utils.py
index 47a6acf..ee885a4 100644
--- a/combo/apps/wcs/utils.py
+++ b/combo/apps/wcs/utils.py
@@ -19,6 +19,14 @@ 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(url):
response_json = cache.get(url)
if response_json is None:
@@ -28,7 +36,7 @@ def get_wcs_json(url):
def get_wcs_options(url):
references = []
- for wcs_key, wcs_site in settings.KNOWN_SERVICES.get('wcs', {}).iteritems():
+ 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:
@@ -36,7 +44,7 @@ def get_wcs_options(url):
for element in response_json:
slug = element.get('slug')
title = element.get('title')
- if len(settings.KNOWN_SERVICES['wcs']) == 1:
+ if len(get_wcs_services()) == 1:
label = title
else:
label = '%s : %s' % (site_title, title)