fargo: use the generic way to do signed requests (#10492)

This commit is contained in:
Frédéric Péters 2016-03-30 19:15:19 +02:00
parent 47f9f7c8ca
commit 0061a2a1c6
1 changed files with 9 additions and 24 deletions

View File

@ -14,8 +14,6 @@
# 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.conf import settings
from django.db import models
from django.utils.http import urlencode
@ -23,7 +21,7 @@ from django.utils.translation import ugettext_lazy as _
from combo.data.models import CellBase
from combo.data.library import register_cell_class
from combo.utils import NothingInCacheException, sign_url
from combo.utils import requests
@ -43,28 +41,15 @@ class RecentDocumentsCell(CellBase):
return hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('fargo')
def get_json(self, path, context):
fargo_site = settings.KNOWN_SERVICES['fargo'].values()[0]
fargo_url = fargo_site.get('url')
if not fargo_url.endswith('/'):
fargo_url += '/'
url = fargo_url + path
params = {}
params['orig'] = fargo_site.get('orig')
if context.get('user') and context['user'].is_authenticated():
if context.get('request') and hasattr(context['request'], 'session') \
and context['request'].session.get('mellon_session'):
mellon = context['request'].session['mellon_session']
params['NameID'] = mellon['name_id_content']
elif hasattr(context['user'], 'email') and context['user'].email:
params['email'] = context['user'].email
url += '?' + urlencode(params)
url = sign_url(url, fargo_site.get('secret'))
response_json = requests.get(url,
headers={'accept': 'application/json'}).json()
return response_json
response = requests.get(path,
remote_service=settings.KNOWN_SERVICES['fargo'].values()[0],
request=context.get('request'),
raise_if_not_cached=not(context.get('synchronous')),
headers={'accept': 'application/json'})
if response.status_code == 200:
return response.json()
return None
def render(self, context):
if not context.get('synchronous'):
raise NothingInCacheException()
context.update(self.get_json('api/documents/recently-added', context))
return super(RecentDocumentsCell, self).render(context)