solis: get _links content for a list of dicts (#39440)
This commit is contained in:
parent
819afb282c
commit
5db20e618f
|
@ -491,6 +491,24 @@ class Solis(BaseResource):
|
|||
}
|
||||
link['content'] = value
|
||||
|
||||
def rsa_get_links(self, information, links):
|
||||
if isinstance(information, list):
|
||||
for info in information:
|
||||
self.rsa_get_links(info, links)
|
||||
return
|
||||
if isinstance(information, dict) and '_links' in information:
|
||||
# return linked objects in non-underscored key, usable in Django template
|
||||
information['rsa_links'] = copy.deepcopy(information['_links'])
|
||||
if not links:
|
||||
# get all links
|
||||
for link in information['rsa_links'].keys():
|
||||
self.rsa_fill_with_link_content(information['rsa_links'][link])
|
||||
else:
|
||||
# get selected link types
|
||||
for link in links:
|
||||
if link in information['rsa_links']:
|
||||
self.rsa_fill_with_link_content(information['rsa_links'][link])
|
||||
|
||||
def rsa_get_information(self, information, user_id=None, code=None, dob=None, token=None,
|
||||
index='search', links=None):
|
||||
# simulate "individu" referential: get user details from civi/individu/user_id
|
||||
|
@ -513,18 +531,12 @@ class Solis(BaseResource):
|
|||
|
||||
information = self.request(endpoint)
|
||||
|
||||
if isinstance(information, dict) and '_links' in information:
|
||||
# return linked objects in non-underscored key, usable in Django template
|
||||
information['rsa_links'] = copy.deepcopy(information['_links'])
|
||||
|
||||
if links is not None:
|
||||
if not links.strip(): # links is empty: get all
|
||||
links = information['rsa_links'].keys()
|
||||
else:
|
||||
links = [x.strip() for x in links.split(',') if x.strip()]
|
||||
links = [x for x in links if x in information['rsa_links']]
|
||||
for link in links:
|
||||
self.rsa_fill_with_link_content(information['rsa_links'][link])
|
||||
if links is not None:
|
||||
if not links.strip(): # links is empty: get all
|
||||
links = None
|
||||
else:
|
||||
links = [x.strip() for x in links.split(',') if x.strip()]
|
||||
self.rsa_get_links(information, links)
|
||||
|
||||
return information
|
||||
|
||||
|
|
|
@ -66,9 +66,13 @@ RSA_3LINKS = '''{
|
|||
}
|
||||
}'''
|
||||
RSA_ACTIONS = '''[
|
||||
{"idStructure":42, "prescriptionPlacement":"Placement"},
|
||||
{"idStructure":42, "prescriptionPlacement":"Placement",
|
||||
"_links": {"structure":
|
||||
{"href": "https://solis.example.net/solisapi/referentiels/trans/organisme/6481/"}}},
|
||||
{"idStructure":42, "prescriptionPlacement":"Prescription"},
|
||||
{"idStructure":43, "prescriptionPlacement":"Prescription"},
|
||||
{"idStructure":43, "prescriptionPlacement":"Prescription",
|
||||
"_links": {"structure":
|
||||
{"href": "https://solis.example.net/solisapi/referentiels/trans/organisme/6482/"}}},
|
||||
{"idStructure":44, "prescriptionPlacement":"Placement"}
|
||||
]'''
|
||||
|
||||
|
@ -905,6 +909,21 @@ def test_solis_rsa_link_infos_unlink(app, solis):
|
|||
resp = app.get(filtered, status=200)
|
||||
assert len(resp.json['data']) == 4 # empty filters: all actions
|
||||
|
||||
requests_get.reset_mock()
|
||||
requests_get.side_effect = [
|
||||
utils.FakedResponse(status_code=200, content=RSA_ACTIONS), # base info
|
||||
utils.FakedResponse(status_code=200, content='{"nom":"Structure1"}'), # link 1
|
||||
utils.FakedResponse(status_code=200, content='{"nom":"Structure2"}'), # link 2
|
||||
]
|
||||
filtered = endpoint + '&links'
|
||||
resp = app.get(filtered, status=200)
|
||||
assert len(resp.json['data']) == 4
|
||||
assert requests_get.call_count == 3 # actions + two _links
|
||||
assert resp.json['data'][0]['rsa_links']['structure']['content'] == {'nom': 'Structure1'}
|
||||
assert resp.json['data'][2]['rsa_links']['structure']['content'] == {'nom': 'Structure2'}
|
||||
assert 'rsa_links' not in resp.json['data'][1]
|
||||
assert 'rsa_links' not in resp.json['data'][3]
|
||||
|
||||
# get referential for a linked user with links
|
||||
with mock.patch('passerelle.utils.Request.get') as requests_get:
|
||||
with mock.patch('passerelle.utils.Request.post') as requests_post:
|
||||
|
|
Loading…
Reference in New Issue