litteralis: cleanup payload from emtpy values (#70115)
This commit is contained in:
parent
5eae2a1a29
commit
d57398c7f8
|
@ -103,21 +103,22 @@ class Litteralis(BaseResource, HTTPResource):
|
|||
if not post_data['demandeur'].get(field):
|
||||
raise APIError('Missing <%s> in demandeur' % field)
|
||||
|
||||
def change_date_fields(data):
|
||||
if data is None:
|
||||
return None
|
||||
def clean_payload(data):
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
|
||||
res = {}
|
||||
for k, v in data.items():
|
||||
if k.startswith('date'):
|
||||
res[k] = make_aware(parse_datetime(v)).isoformat()
|
||||
else:
|
||||
res[k] = change_date_fields(v)
|
||||
if v:
|
||||
if k.startswith('date'):
|
||||
res[k] = make_aware(parse_datetime(v)).isoformat()
|
||||
else:
|
||||
cleaned_payload = clean_payload(v)
|
||||
if cleaned_payload:
|
||||
res[k] = cleaned_payload
|
||||
return res
|
||||
|
||||
data = change_date_fields(post_data)
|
||||
data = clean_payload(post_data)
|
||||
coord = data.get('geom', {}).get('coordinates', [])
|
||||
if coord:
|
||||
new_coord = []
|
||||
|
|
|
@ -65,6 +65,48 @@ def test_demandes_recues(app, connector):
|
|||
assert json_req['geom']['coordinates'] == [48.866667, 2.333333]
|
||||
|
||||
|
||||
def test_demandes_recues_clean_empty_fields(app, connector):
|
||||
params = {
|
||||
'fournisseur': 'the-fournisseur',
|
||||
'idDemande': '12-34',
|
||||
'dateEnvoi': '2022-10-03T12:43:18',
|
||||
'typeModele': 'DA',
|
||||
'demandeur': {
|
||||
'raisonSociale': 'the-world-company',
|
||||
'mail': 'contact@the-world-company.com',
|
||||
'adresse': {
|
||||
'rue': 'rue de la république',
|
||||
'insee': '69034',
|
||||
},
|
||||
},
|
||||
'destinataire': {
|
||||
'idCollectivite': '1',
|
||||
'nomCollectivite': 'Malakoff',
|
||||
},
|
||||
'geom': {'type': 'Point', 'coordinates': ['48.866667', '2.333333']},
|
||||
'additionalInformation': {
|
||||
'typeDemande': 'Stationnement pour travaux',
|
||||
'dateDebut': '2019-12-04T14:33:13',
|
||||
'dateFin': '2019-12-09T14:33:13',
|
||||
'dateXX': '',
|
||||
'foo': {'bar': ''},
|
||||
'baz': {'who': 'what', 'which': ''},
|
||||
},
|
||||
}
|
||||
|
||||
with responses.RequestsMock() as rsps:
|
||||
rsps.post('http://litteralis.invalid/demandes-recues', status=200, json={'identifier': '1234'})
|
||||
resp = app.post_json('/litteralis/slug-litteralis/demandes-recues', params=params)
|
||||
json_resp = resp.json
|
||||
assert json_resp['err'] == 0
|
||||
assert len(rsps.calls) == 1
|
||||
req = rsps.calls[0].request
|
||||
json_req = json.loads(req.body)
|
||||
assert 'dateXX' not in json_req['additionalInformation']
|
||||
assert 'foo' not in json_req['additionalInformation']
|
||||
assert json_req['additionalInformation']['baz'] == {'who': 'what'}
|
||||
|
||||
|
||||
def test_upload(app, connector):
|
||||
params = {
|
||||
'file': {
|
||||
|
|
Loading…
Reference in New Issue