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

View File

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

View File

@ -126,6 +126,25 @@ def test_upload(app, connector):
assert json_resp['data'] == '' 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): def test_demandes_recues_reponses(app, connector):
with responses.RequestsMock() as rsps: with responses.RequestsMock() as rsps:
rsps.get('http://litteralis.invalid/demandes-recues/1234/reponses', status=200, json={'foo': 'bar'}) rsps.get('http://litteralis.invalid/demandes-recues/1234/reponses', status=200, json={'foo': 'bar'})