astech: add endpoint to get view data (#82416) #366
|
@ -440,3 +440,64 @@ class ASTech(BaseResource, HTTPResource):
|
|||
position['id'] = position['position']
|
||||
position['text'] = position['positionLib']
|
||||
return {'data': positions}
|
||||
|
||||
@endpoint(
|
||||
name='list-views',
|
||||
display_order=1,
|
||||
description=_('List available views'),
|
||||
display_category=_('Referential'),
|
||||
)
|
||||
def list_views(self, request):
|
||||
results = self.call('apicli/data/views')
|
||||
astech_views = results.get('views', [])
|
||||
for view in astech_views:
|
||||
view['id'] = view['apivId']
|
||||
view['text'] = view['apivNom']
|
||||
return {'data': astech_views}
|
||||
|
||||
@endpoint(
|
||||
name='get-view-columns',
|
||||
display_order=2,
|
||||
description=_('Get view columns'),
|
||||
display_category=_('Referential'),
|
||||
parameters={
|
||||
'code': {
|
||||
'description': _('View code'),
|
||||
'example_value': 'ASTECH_BIENS',
|
||||
},
|
||||
},
|
||||
)
|
||||
def get_view_columns(self, request, code):
|
||||
endpoint = 'apicli/data/%s/columns' % code
|
||||
results = self.call(endpoint)
|
||||
columns = results.get('columns', [])
|
||||
for column in columns:
|
||||
column['id'] = column['code']
|
||||
column['text'] = column['des']
|
||||
return {'data': columns}
|
||||
|
||||
@endpoint(
|
||||
name='get-view-data',
|
||||
display_order=3,
|
||||
description=_('Get view data'),
|
||||
display_category=_('Referential'),
|
||||
datasource=True,
|
||||
parameters={
|
||||
'code': {
|
||||
'description': _('View code'),
|
||||
'example_value': 'ASTECH_BIENS',
|
||||
},
|
||||
'id_column': {'description': _('Name of column contaning the id'), 'example_value': 'BIEN_ID'},
|
||||
smihai marked this conversation as resolved
Outdated
|
||||
'text_column': {
|
||||
smihai marked this conversation as resolved
Outdated
tnoel
commented
J'aurai nommé ça "text_column" mais ça roule ainsi. (j'imagine aussi qu'un classique "text_template" sera plus tard demandé, mais on peut attendre le «cas d'usage» et un ticket correspondant) J'aurai nommé ça "text_column" mais ça roule ainsi.
(j'imagine aussi qu'un classique "text_template" sera plus tard demandé, mais on peut attendre le «cas d'usage» et un ticket correspondant)
smihai
commented
Yep, c'est fait. > J'aurai nommé ça "text_column" mais ça roule ainsi.
>
Yep, c'est fait.
|
||||
'description': _('Name of column contaning the label'),
|
||||
smihai marked this conversation as resolved
Outdated
tnoel
commented
À mon avis ça ne marche que pour le code ASTECH_BIENS, à voir avec les autres (ASTECH_MESURES, etc). À mon avis ça ne marche que pour le code ASTECH_BIENS, à voir avec les autres (ASTECH_MESURES, etc).
smihai
commented
En effet. En effet.
Et je demande pour ça 2 paramètres pour spécifier les colonnes contenant l'id et le texte.
|
||||
'example_value': 'DESIGNATION',
|
||||
},
|
||||
},
|
||||
)
|
||||
def get_view_data(self, request, code, id_column, text_column):
|
||||
endpoint = 'apicli/data/%s/results' % code
|
||||
results = self.call(endpoint, json={'data': {'filters': []}})
|
||||
for result in results:
|
||||
result['id'] = result[id_column]
|
||||
result['text'] = result[text_column]
|
||||
return {'data': results}
|
||||
|
|
|
@ -78,6 +78,21 @@ POSITIONS_RESPONSE = """
|
|||
[{"position":"A","positionLib":"En attente","color":"0, 0, 0"},{"position":"E","positionLib":"Envoi","color":"190, 190, 0"},{"position":"C","positionLib":"En cours","color":"255, 0, 0"},{"position":"D","positionLib":"Envoi signataire","color":"255, 255, 113"},{"position":"T","positionLib":"Termin\u00e9","color":"0, 0, 0"},{"position":"I","positionLib":"\u00c9dition devis","color":"0, 255, 255"},{"position":"R","positionLib":"Refus","color":"255, 0, 0"},{"position":"V","positionLib":"V\u00e9rification","color":"0, 255, 0"},{"position":"F","positionLib":"Devis effectu\u00e9","color":"153, 204, 255"},{"position":"P","positionLib":"Livraison partielle","color":"255, 102, 0"},{"position":"L","positionLib":"Livraison","color":"128, 0, 0"}]
|
||||
"""
|
||||
|
||||
VIEWS_RESPONSE = """
|
||||
{"views":[{"apivId":"7","apivCode":"ASTECH_FORMDYN","apivNom":"Formulaires dynamiques - champs de saisie"},
|
||||
{"apivId":"1","apivCode":"ASTECH_BIENS","apivNom":"Liste des biens"}]}
|
||||
"""
|
||||
|
||||
COLUMNS_RESPONSE = """
|
||||
{"columns":[{"code":"BIEN_ID","des":"Identifiant du bien AS-TECH","type":"NUM","length":"18"},
|
||||
{"code":"ANCETREID","des":"Identifiant de l\u0027anc\u00eatre du bien","type":"NUM","length":""}]}
|
||||
"""
|
||||
|
||||
RESULTS_RESPONSE = """[{"BIEN_ID": "2219", "CODE_BIEN": "AC-849-YE", "DESIGNATION": "RENAULT KANGOO"},
|
||||
{"BIEN_ID": "2220", "CODE_BIEN": "AC-933-EA", "DESIGNATION": "RENAULT MASTER"},
|
||||
{"BIEN_ID": "2221", "CODE_BIEN": "AC-955-SE", "DESIGNATION": "RENAULT KANGOO"}]
|
||||
"""
|
||||
|
||||
|
||||
@mock.patch('passerelle.utils.Request.request')
|
||||
def test_connections(mocked_request, app, setup):
|
||||
|
@ -435,3 +450,74 @@ def test_positions(mocked_auth, mocked_request, app, setup):
|
|||
'id': 'A',
|
||||
'text': 'En attente',
|
||||
}
|
||||
|
||||
|
||||
@mock.patch('passerelle.utils.Request.request')
|
||||
@mock.patch('passerelle.apps.astech.models.ASTech.get_authorization')
|
||||
def test_list_views(mocked_auth, mocked_request, app, setup):
|
||||
mocked_auth.return_value = {'access_token': '4242', 'connection_id': 'TEST'}
|
||||
|
||||
endpoint = reverse(
|
||||
'generic-endpoint',
|
||||
kwargs={'connector': 'astech', 'slug': setup.slug, 'endpoint': 'list-views'},
|
||||
)
|
||||
mocked_request.return_value = tests.utils.FakedResponse(content=VIEWS_RESPONSE, status_code=200)
|
||||
response = app.get(endpoint)
|
||||
assert mocked_request.call_args[0][0] == 'get'
|
||||
assert mocked_request.call_args[0][1].endswith('apicli/data/views')
|
||||
assert response.json['data']
|
||||
for r in response.json['data']:
|
||||
assert 'id' in r
|
||||
assert 'text' in r
|
||||
|
||||
|
||||
@mock.patch('passerelle.utils.Request.request')
|
||||
@mock.patch('passerelle.apps.astech.models.ASTech.get_authorization')
|
||||
def test_view_columns(mocked_auth, mocked_request, app, setup):
|
||||
mocked_auth.return_value = {'access_token': '4242', 'connection_id': 'TEST'}
|
||||
|
||||
endpoint = reverse(
|
||||
'generic-endpoint',
|
||||
kwargs={'connector': 'astech', 'slug': setup.slug, 'endpoint': 'get-view-columns'},
|
||||
)
|
||||
mocked_request.return_value = tests.utils.FakedResponse(content=COLUMNS_RESPONSE, status_code=200)
|
||||
response = app.get(endpoint, params={'code': 'ASTECH_BIENS'})
|
||||
assert mocked_request.call_args[0][0] == 'get'
|
||||
assert mocked_request.call_args[0][1].endswith('apicli/data/ASTECH_BIENS/columns')
|
||||
assert response.json['data']
|
||||
for r in response.json['data']:
|
||||
assert 'id' in r
|
||||
assert 'text' in r
|
||||
|
||||
|
||||
@mock.patch('passerelle.utils.Request.request')
|
||||
@mock.patch('passerelle.apps.astech.models.ASTech.get_authorization')
|
||||
def test_view_data(mocked_auth, mocked_request, app, setup):
|
||||
mocked_auth.return_value = {'access_token': '4242', 'connection_id': 'TEST'}
|
||||
|
||||
endpoint = reverse(
|
||||
'generic-endpoint',
|
||||
kwargs={'connector': 'astech', 'slug': setup.slug, 'endpoint': 'get-view-data'},
|
||||
)
|
||||
mocked_request.return_value = tests.utils.FakedResponse(content=RESULTS_RESPONSE, status_code=200)
|
||||
response = app.get(
|
||||
endpoint, params={'code': 'ASTECH_BIENS', 'id_column': 'BIEN_ID', 'text_column': 'DESIGNATION'}
|
||||
)
|
||||
assert mocked_request.call_args[0][0] == 'post'
|
||||
assert mocked_request.call_args[0][1].endswith('apicli/data/ASTECH_BIENS/results')
|
||||
assert response.json['data']
|
||||
for r in response.json['data']:
|
||||
assert 'id' in r
|
||||
assert 'text' in r
|
||||
|
||||
response = app.get(
|
||||
endpoint,
|
||||
params={'code': 'ASTECH_BIENS', 'id_column': 'BIEN_ID', 'text_column': 'DESIGNATION', 'id': 2221},
|
||||
)
|
||||
assert len(response.json['data']) == 1
|
||||
|
||||
response = app.get(
|
||||
endpoint,
|
||||
params={'code': 'ASTECH_BIENS', 'id_column': 'BIEN_ID', 'text_column': 'DESIGNATION', 'q': 'KANGOO'},
|
||||
)
|
||||
assert len(response.json['data']) == 2
|
||||
|
|
Loading…
Reference in New Issue
Tu n'auras pas besoin de gérer "q" et "id" ici, et de construire un filtre si l'un ou l'autre est présent ?
Avec le paramètre
datasource=True
ça fonctionne tout seul.