utils: explicit user in requests (#10530)

This commit is contained in:
Thomas NOËL 2016-04-01 18:50:41 +02:00
parent 8917bdc597
commit 462aada3f8
5 changed files with 28 additions and 21 deletions

View File

@ -43,7 +43,7 @@ class RecentDocumentsCell(CellBase):
def get_json(self, path, context):
response = requests.get(path,
remote_service=settings.KNOWN_SERVICES['fargo'].values()[0],
request=context.get('request'),
user=self.get_concerned_user(context),
raise_if_not_cached=not(context.get('synchronous')),
headers={'accept': 'application/json'})
if response.status_code == 200:

View File

@ -150,7 +150,7 @@ class WcsBlurpMixin(object):
response = requests.get(
self.api_url,
remote_service=wcs_site,
request=context.get('request'),
user=self.get_concerned_user(context),
cache_duration=self.cache_duration,
raise_if_not_cached=not(context.get('synchronous')))
if response.status_code == 200:

View File

@ -27,7 +27,7 @@ def get_wcs_services():
return settings.KNOWN_SERVICES.get('wcs')
def get_wcs_json(wcs_site, path):
return requests.get(path, remote_service=wcs_site, user=None,
return requests.get(path, remote_service=wcs_site, without_user=True,
headers={'accept': 'application/json'}).json()
def get_wcs_options(url, include_category_slug=False):

View File

@ -396,6 +396,9 @@ class CellBase(models.Model):
'''Return whether the cell content varies from user to user.'''
return self.user_dependant
def get_concerned_user(self, context):
return getattr(context.get('request'), 'user', None)
def get_cell_extra_context(self, context):
return {'cell': self}

View File

@ -35,32 +35,36 @@ class NothingInCacheException(Exception):
class Requests(RequestsSession):
AUTO_USER = object()
AUTO_USER_EMAIL = object()
AUTO_USER_NAMEID = object()
def request(self, method, url, **kwargs):
remote_service = kwargs.pop('remote_service', None)
cache_duration = kwargs.pop('cache_duration', 15)
user = kwargs.pop('user', self.AUTO_USER)
user = kwargs.pop('user', None)
without_user = kwargs.pop('without_user', False)
federation_key = kwargs.pop('federation_key', 'auto') # 'auto', 'email', 'nameid'
raise_if_not_cached = kwargs.pop('raise_if_not_cached', False)
current_request = kwargs.pop('request', None)
if remote_service:
query_params = {'orig': remote_service.get('orig')}
if user in (self.AUTO_USER, self.AUTO_USER_NAMEID, self.AUTO_USER_EMAIL):
if current_request.user and current_request.user.is_authenticated():
if current_request.session.get('mellon_session') and user is not self.AUTO_USER_EMAIL:
mellon = current_request.session['mellon_session']
query_params['NameID'] = mellon['name_id_content']
elif user is not self.AUTO_USER_NAMEID:
query_params['email'] = current_request.user.email
if not user:
if without_user:
query_params = {}
else:
query_params['NameID'] = ''
query_params['email'] = ''
elif user:
# user must then be a dictionary
query_params.update(user)
query_params = {'NameID': '', 'email': ''}
elif isinstance(user, dict):
query_params = user.copy()
else:
query_params = {}
if federation_key == 'nameid':
query_params['NameID'] = user.saml_identifiers.first().name_id
elif federation_key == 'email':
query_params['email'] = user.email
else: # 'auto'
if hasattr(user, 'saml_identifiers') and user.saml_identifiers.exists():
query_params['NameID'] = user.saml_identifiers.first().name_id
else:
query_params['email'] = user.email
query_params['orig'] = remote_service.get('orig')
remote_service_base_url = remote_service.get('url')
scheme, netloc, old_path, params, old_query, fragment = urlparse.urlparse(