family: rewrite get_cell_extra_context

This commit is contained in:
Thomas NOËL 2015-11-10 14:38:23 +01:00
parent 7df003805e
commit 51beecdf88
1 changed files with 33 additions and 15 deletions

View File

@ -1,3 +1,19 @@
# 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
@ -25,23 +41,25 @@ class FamilyInfosCell(CellBase):
@classmethod
def is_enabled(cls):
return hasattr(settings, 'FAMILY_SERVICE')
return hasattr(settings, 'FAMILY_SERVICE') and settings.FAMILY_SERVICE
def get_cell_extra_context(self):
data = {}
context = self.context
if context.get('user'):
if context.get('request') and hasattr(context['request'], 'session') and \
context['request'].session.get('mellon_session'):
mellon = context.get('request').session['mellon_session']
params = urllib.urlencode({'NameID': mellon['name_id_content'],
'orig': context['request'].get_host()
})
url = sign_url(settings.FAMILY_SERVICE.get('url') + '/family/?' + params,
settings.FAMILY_SERVICE.get('signature_key'))
r = requests.get(url)
data.update({'family': r.json()})
return data
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 name_id:
params = {'NameID': name_id, 'orig': self.context['request'].get_host()}
# get family data
ws_family_url = sign_url(settings.FAMILY_SERVICE.get('url') +
'/family/?' + urllib.urlencode(params),
settings.FAMILY_SERVICE.get('signature_key'))
ws_family_data = requests.get(ws_family_url).json()
if ws_family_data.get('err') or not ws_family_data.get('data'):
return {}
context['family'] = ws_family_data
return context
def render(self, context):
self.context = context