family: link to wcs forms via keywords and person id (#8275)
This commit is contained in:
parent
51beecdf88
commit
a2a1b66783
|
@ -26,6 +26,8 @@ from combo.data.models import CellBase
|
|||
from combo.data.library import register_cell_class
|
||||
from combo.utils import NothingInCacheException, sign_url
|
||||
|
||||
from .utils import get_wcs_services, get_wcs_data
|
||||
|
||||
|
||||
@register_cell_class
|
||||
class FamilyInfosCell(CellBase):
|
||||
|
@ -41,14 +43,13 @@ class FamilyInfosCell(CellBase):
|
|||
|
||||
@classmethod
|
||||
def is_enabled(cls):
|
||||
return hasattr(settings, 'FAMILY_SERVICE') and settings.FAMILY_SERVICE
|
||||
return getattr(settings, 'FAMILY_SERVICE', None)
|
||||
|
||||
def get_cell_extra_context(self):
|
||||
context = {}
|
||||
if self.context.get('user') and self.context.get('request') and \
|
||||
hasattr(self.context['request'], 'session') and \
|
||||
self.context['request'].session.get('mellon_session'):
|
||||
name_id = self.context.get('request').session['mellon_session']['name_id_content']
|
||||
if (self.context.get('user') and self.context.get('request') and
|
||||
getattr(self.context['request'], 'session', {}).get('mellon_session')):
|
||||
name_id = self.context.get('request').session['mellon_session'].get('name_id_content')
|
||||
if name_id:
|
||||
params = {'NameID': name_id, 'orig': self.context['request'].get_host()}
|
||||
# get family data
|
||||
|
@ -58,6 +59,27 @@ class FamilyInfosCell(CellBase):
|
|||
ws_family_data = requests.get(ws_family_url).json()
|
||||
if ws_family_data.get('err') or not ws_family_data.get('data'):
|
||||
return {}
|
||||
# get formdefs and formdata from wcs
|
||||
def add_forms(obj, formdefs, forms, drafts, varname):
|
||||
keywords_set = set(obj.get('keywords', []))
|
||||
obj['formdefs'] = [f for f in formdefs
|
||||
if keywords_set.intersection(f.get('keywords', []))]
|
||||
if obj.get('id'):
|
||||
obj['forms'] = [f for f in forms
|
||||
if f['fields'].get(varname) == obj['id']]
|
||||
obj['drafts'] = [f for f in drafts
|
||||
if f['fields'].get(varname) == obj['id']]
|
||||
if get_wcs_services():
|
||||
formdefs = get_wcs_data('/api/formdefs/', params)
|
||||
params['full'] = 'on'
|
||||
forms = get_wcs_data('/api/user/forms', params)
|
||||
drafts = get_wcs_data('/api/user/drafts', params)
|
||||
family = ws_family_data['data']
|
||||
add_forms(family, formdefs, forms, drafts, 'family_id_raw')
|
||||
for adult in family.get('adults', []):
|
||||
add_forms(adult, formdefs, forms, drafts, 'adult_id_raw')
|
||||
for child in family.get('children', []):
|
||||
add_forms(child, formdefs, forms, drafts, 'child_id_raw')
|
||||
context['family'] = ws_family_data
|
||||
return context
|
||||
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
# combo - content management system
|
||||
# Copyright (C) 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
|
||||
import urllib
|
||||
|
||||
from combo.utils import sign_url
|
||||
from combo.apps.wcs.utils import get_wcs_services
|
||||
|
||||
|
||||
def get_wcs_data(endpoint, params=None):
|
||||
# consider only first wcs instance (FIXME)
|
||||
wcs_site = get_wcs_services().values()[0]
|
||||
wcs_site_url = wcs_site['url']
|
||||
if not wcs_site_url.endswith('/'):
|
||||
wcs_site_url += '/'
|
||||
url = wcs_site_url + endpoint
|
||||
if params:
|
||||
url += '?' + urllib.urlencode(params.items())
|
||||
url = sign_url(url, wcs_site.get('secret'))
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
Loading…
Reference in New Issue