litteralis: add annexes endpoint (#69970)

This commit is contained in:
Emmanuel Cazenave 2022-10-11 18:12:05 +02:00
parent 083fab1f52
commit 6fd0cb1d9f
3 changed files with 56 additions and 17 deletions

View File

@ -83,6 +83,23 @@ class Litteralis(BaseResource, HTTPResource):
return resp.text
def _upload(self, url, post_data):
try:
file_byte_content = base64.b64decode(post_data['file']['content'])
except (TypeError, binascii.Error):
raise APIError("Can't decode file")
files = {
'file': (post_data['file']['filename'], file_byte_content, post_data['file']['content_type'])
}
return {
'data': self._call(
url,
method='post',
files=files,
)
}
@endpoint(
name='demandes-recues',
description=_('Create submission'),
@ -130,32 +147,35 @@ class Litteralis(BaseResource, HTTPResource):
@endpoint(
name='upload',
description=_('Upload file'),
description=_('Upload summary file'),
perm='can_access',
post={
'request_body': {
'schema': {
'application/json': schemas.UPLOAD,
'application/json': schemas.UPLOAD_ANNEXES,
}
}
},
)
def upload(self, request, post_data):
try:
file_byte_content = base64.b64decode(post_data['file']['content'])
except (TypeError, binascii.Error):
raise APIError("Can't decode file")
url = 'demandes-recues/%s/upload' % post_data['id_demande']
return self._upload(url, post_data)
files = {
'file': (post_data['file']['filename'], file_byte_content, post_data['file']['content_type'])
}
return {
'data': self._call(
'demandes-recues/%s/upload' % post_data['id_demande'],
method='post',
files=files,
)
}
@endpoint(
name='annexes',
description=_('Upload appendix file'),
perm='can_access',
post={
'request_body': {
'schema': {
'application/json': schemas.UPLOAD_ANNEXES,
}
}
},
)
def annexes(self, request, post_data):
url = 'demandes-recues/%s/annexes' % post_data['id_demande']
return self._upload(url, post_data)
@endpoint(
methods=['get'],

View File

@ -150,7 +150,7 @@ DEMANDES_RECUES = {
'unflatten': True,
}
UPLOAD = {
UPLOAD_ANNEXES = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'additionalProperties': False,

View File

@ -126,6 +126,25 @@ def test_upload(app, connector):
assert json_resp['data'] == ''
def test_annexes(app, connector):
params = {
'file': {
'filename': 'bla',
'content': base64.b64encode(b'who what').decode(),
'content_type': 'text/plain',
},
'id_demande': '1234',
}
with responses.RequestsMock() as rsps:
rsps.post('http://litteralis.invalid/demandes-recues/1234/annexes', status=200, body='')
resp = app.post_json('/litteralis/slug-litteralis/annexes', params=params)
assert len(rsps.calls) == 1
assert rsps.calls[0].request.headers['Content-Type'].startswith('multipart/form-data')
json_resp = resp.json
assert json_resp['err'] == 0
assert json_resp['data'] == ''
def test_demandes_recues_reponses(app, connector):
with responses.RequestsMock() as rsps:
rsps.get('http://litteralis.invalid/demandes-recues/1234/reponses', status=200, json={'foo': 'bar'})