wcs: remove usage of cmsplugin_blurp (#9067)
This commit is contained in:
parent
6ef2900451
commit
0de4306d51
|
@ -14,23 +14,29 @@
|
|||
# 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 hashlib
|
||||
import json
|
||||
import logging
|
||||
import requests
|
||||
import urllib
|
||||
|
||||
from django import template
|
||||
from django.core.cache import cache
|
||||
from django.db import models
|
||||
from django.forms import models as model_forms
|
||||
from django.forms import Select
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
import cmsplugin_blurp.utils
|
||||
from jsonfield import JSONField
|
||||
|
||||
from combo.data.models import CellBase
|
||||
from combo.data.library import register_cell_class
|
||||
from combo.utils import NothingInCacheException
|
||||
from combo.utils import NothingInCacheException, sign_url
|
||||
|
||||
from .utils import get_wcs_json, is_wcs_enabled, get_wcs_services
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@register_cell_class
|
||||
class WcsFormCell(CellBase):
|
||||
formdef_reference = models.CharField(_('Form'), max_length=150)
|
||||
|
@ -131,33 +137,23 @@ class WcsCategoryCell(WcsCommonCategoryCell):
|
|||
|
||||
class WcsBlurpMixin(object):
|
||||
is_enabled = classmethod(is_wcs_enabled)
|
||||
user_dependant = True
|
||||
cache_duration = 5
|
||||
|
||||
def get_blurp_renderer(self, context):
|
||||
def get_data(self, context):
|
||||
if self.wcs_site:
|
||||
try:
|
||||
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, {
|
||||
'class': 'cmsplugin_blurp.renderers.template.TemplateRenderer',
|
||||
'template': ''
|
||||
})
|
||||
return {}
|
||||
else:
|
||||
wcs_sites = get_wcs_services()
|
||||
|
||||
sources = []
|
||||
for slug, wcs_site in wcs_sites.items():
|
||||
wcs_sites = wcs_sites.copy()
|
||||
for wcs_slug, wcs_site in wcs_sites.items():
|
||||
url = wcs_site.get('url')
|
||||
if not url.endswith('/'):
|
||||
url += '/'
|
||||
source = {
|
||||
'slug': slug,
|
||||
'parser_type': 'json',
|
||||
'verify_certificate': False,
|
||||
'allow_redirects': False,
|
||||
'default': {'title': wcs_site['title'], 'base_url': url}
|
||||
}
|
||||
|
||||
url += self.api_url
|
||||
if not '?' in url:
|
||||
|
@ -167,8 +163,6 @@ class WcsBlurpMixin(object):
|
|||
url += 'format=json'
|
||||
if wcs_site.get('orig') and wcs_site.get('secret'):
|
||||
url += '&orig=%s' % wcs_site['orig']
|
||||
source['auth_mech'] = 'hmac-sha1'
|
||||
source['signature_key'] = str(wcs_site['secret'])
|
||||
if context.get('user'):
|
||||
if context.get('request') and hasattr(context['request'], 'session') \
|
||||
and context['request'].session.get('mellon_session'):
|
||||
|
@ -178,22 +172,28 @@ class WcsBlurpMixin(object):
|
|||
elif hasattr(context['user'], 'email') and context['user'].email:
|
||||
url += '&email=' + urllib.quote(context['user'].email)
|
||||
|
||||
source['url'] = url
|
||||
sources.append(source)
|
||||
cache_key = hashlib.md5(url).hexdigest()
|
||||
cache_content = cache.get(cache_key)
|
||||
if not cache_content:
|
||||
if not context.get('synchronous'):
|
||||
raise NothingInCacheException()
|
||||
source_response = requests.get(sign_url(
|
||||
url, wcs_site['secret']),
|
||||
verify=False)
|
||||
if source_response.status_code == 200:
|
||||
cache_content = json.loads(source_response.content)
|
||||
cache.set(cache_key, cache_content, self.cache_duration)
|
||||
else:
|
||||
logger.error('failed to load %s', url)
|
||||
cache_content = None
|
||||
wcs_site['data'] = cache_content
|
||||
|
||||
renderer = cmsplugin_blurp.utils.create_renderer(self.variable_name, {
|
||||
'name': self._meta.verbose_name,
|
||||
'class': 'cmsplugin_blurp.renderers.data_source.DictRendererWithDefault',
|
||||
'sources': sources,
|
||||
'template_name': self.template_name,
|
||||
'refresh': 10,
|
||||
'ajax': False,
|
||||
})
|
||||
return wcs_sites
|
||||
|
||||
if not context.get('synchronous') and not renderer.has_cached_content(context):
|
||||
raise NothingInCacheException()
|
||||
|
||||
return renderer
|
||||
def get_cell_extra_context(self):
|
||||
extra_context = super(WcsBlurpMixin, self).get_cell_extra_context()
|
||||
extra_context[self.variable_name] = self.get_data()
|
||||
return extra_context
|
||||
|
||||
|
||||
class WcsDataBaseCell(CellBase, WcsBlurpMixin):
|
||||
|
@ -213,10 +213,8 @@ class WcsDataBaseCell(CellBase, WcsBlurpMixin):
|
|||
widgets={'wcs_site': Select(choices=combo_wcs_sites)})
|
||||
|
||||
def render(self, context):
|
||||
renderer = self.get_blurp_renderer(context)
|
||||
template = renderer.render_template()
|
||||
context = renderer.render(context)
|
||||
return template.render(context)
|
||||
context[self.variable_name] = self.get_data(context)
|
||||
return super(WcsDataBaseCell, self).render(context)
|
||||
|
||||
|
||||
@register_cell_class
|
||||
|
@ -254,6 +252,7 @@ class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin):
|
|||
|
||||
variable_name = 'forms'
|
||||
template_name = 'combo/wcs/forms_of_category.html'
|
||||
cache_duration = 600
|
||||
|
||||
def get_default_form_class(self):
|
||||
from .forms import WcsFormsOfCategoryCellForm
|
||||
|
@ -273,9 +272,7 @@ class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin):
|
|||
def render(self, context):
|
||||
if not self.category_reference:
|
||||
return ''
|
||||
renderer = self.get_blurp_renderer(context)
|
||||
template = renderer.render_template()
|
||||
context = renderer.render(context)
|
||||
context[self.variable_name] = self.get_data(context)
|
||||
context['slug'] = self.category_reference.split(':')[-1]
|
||||
context['title'] = self.cached_title
|
||||
context['description'] = self.cached_description
|
||||
|
@ -294,7 +291,7 @@ class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin):
|
|||
context['more_forms'] = True
|
||||
context['forms'] = context['forms'][:self.limit]
|
||||
|
||||
return template.render(context)
|
||||
return super(WcsFormsOfCategoryCell, self).render(context)
|
||||
|
||||
|
||||
@register_cell_class
|
||||
|
@ -302,16 +299,11 @@ class CategoriesCell(WcsDataBaseCell):
|
|||
api_url = 'api/categories/?full=on'
|
||||
variable_name = 'form_categories'
|
||||
template_name = 'combo/wcs/form_categories.html'
|
||||
cache_duration = 600
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Form Categories')
|
||||
|
||||
def render(self, context):
|
||||
renderer = self.get_blurp_renderer(context)
|
||||
template = renderer.render_template()
|
||||
context = renderer.render(context)
|
||||
return template.render(context)
|
||||
|
||||
|
||||
@register_cell_class
|
||||
class TrackingCodeInputCell(CellBase):
|
||||
|
|
Loading…
Reference in New Issue