wcs: add combo/wcs/formdef integration (#6227)
This commit is contained in:
parent
ae8a573417
commit
4af530ed00
|
@ -0,0 +1,8 @@
|
|||
Combo/wcs integration
|
||||
=====================
|
||||
|
||||
INSTALLED_APPS += ('combo.apps.wcs',)
|
||||
|
||||
COMBO_WCS_SITES = {
|
||||
'default': {'title': 'wcs', 'url': 'http://wcs/'},
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
# 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 import forms
|
||||
from django.conf import settings
|
||||
|
||||
from .models import WcsFormCell
|
||||
|
||||
class WcsFormCellForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = WcsFormCell
|
||||
fields = ('formdef_reference',)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(WcsFormCellForm, self).__init__(*args, **kwargs)
|
||||
formdef_references = []
|
||||
for wcs_key in settings.COMBO_WCS_SITES:
|
||||
wcs_site = settings.COMBO_WCS_SITES.get(wcs_key)
|
||||
site_title = wcs_site.get('site_title')
|
||||
forms_response_json = WcsFormCell.get_wcs_json(wcs_site.get('url') + 'json')
|
||||
for form in forms_response_json:
|
||||
slug = form.get('slug')
|
||||
title = form.get('title')
|
||||
if len(settings.COMBO_WCS_SITES) == 1:
|
||||
label = title
|
||||
else:
|
||||
label = '%s : %s' % (site_title, title)
|
||||
reference = '%s:%s' % (wcs_key, slug)
|
||||
formdef_references.append((reference, label))
|
||||
formdef_references.sort(lambda x, y: cmp(x[1], y[1]))
|
||||
|
||||
self.fields['formdef_reference'].widget = forms.Select(choices=formdef_references)
|
|
@ -0,0 +1,67 @@
|
|||
# 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/>.
|
||||
|
||||
import requests
|
||||
|
||||
from django import template
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from combo.data.models import CellBase
|
||||
from combo.data.library import register_cell_class
|
||||
|
||||
@register_cell_class
|
||||
class WcsFormCell(CellBase):
|
||||
formdef_reference = models.CharField(_('Form'), max_length=100)
|
||||
|
||||
cached_title = models.CharField(_('Title'), max_length=50)
|
||||
cached_url = models.URLField(_('URL'))
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Form')
|
||||
|
||||
def get_default_form_class(self):
|
||||
from .forms import WcsFormCellForm
|
||||
return WcsFormCellForm
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.formdef_reference:
|
||||
wcs_key, form_slug = self.formdef_reference.split(':')
|
||||
wcs_site = settings.COMBO_WCS_SITES.get(wcs_key)
|
||||
forms_response_json = self.get_wcs_json(wcs_site.get('url') + 'json')
|
||||
for form in forms_response_json:
|
||||
slug = form.get('slug')
|
||||
if slug == form_slug:
|
||||
self.cached_title = form.get('title')
|
||||
self.cached_url = form.get('url')
|
||||
return super(WcsFormCell, self).save(*args, **kwargs)
|
||||
|
||||
def render(self, context):
|
||||
formdef_template = template.loader.get_template('combo/wcs/form.html')
|
||||
context['slug'] = self.formdef_reference.split(':')[-1]
|
||||
context['title'] = self.cached_title
|
||||
context['url'] = self.cached_url
|
||||
return formdef_template.render(context)
|
||||
|
||||
@classmethod
|
||||
def get_wcs_json(cls, url):
|
||||
response_json = cache.get(url)
|
||||
if response_json is None:
|
||||
response_json = requests.get(url).json()
|
||||
cache.set(url, response_json)
|
||||
return response_json
|
|
@ -0,0 +1 @@
|
|||
<div class="wcs-form-{{slug}}"><a href="{{ url }}">{{ title }}</a></div>
|
Loading…
Reference in New Issue