[lib/responses] ajout format XLSX, car ODS bug avec les dates

https://github.com/pyexcel/pyexcel-ods/issues/31
This commit is contained in:
Maxime Alves LIRMM@home 2022-05-17 10:30:12 +02:00
parent 0d9dc2a018
commit 99d4aaeb8d
4 changed files with 22 additions and 3 deletions

View File

@ -16,7 +16,7 @@ import yaml
from starlette.exceptions import HTTPException
from halfapi.lib import acl
from halfapi.lib.responses import ORJSONResponse, ODSResponse
from halfapi.lib.responses import ORJSONResponse, ODSResponse, XLSXResponse
# from halfapi.lib.router import read_router
from halfapi.lib.constants import VERBS
@ -93,6 +93,14 @@ def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine:
return ODSResponse(res)
if ret_type == 'xlsx':
res = fct(**fct_args)
assert isinstance(res, list)
for elt in res:
assert isinstance(elt, dict)
return XLSXResponse(res)
raise NotImplementedError
except NotImplementedError as exc:

View File

@ -118,6 +118,8 @@ class HJSONResponse(ORJSONResponse):
return super().render(list(content))
class ODSResponse(Response):
file_type = 'ods'
def __init__(self, d_rows: typing.List[typing.Dict]):
try:
import pyexcel as pe
@ -140,10 +142,10 @@ class ODSResponse(Response):
self.sheet = pe.Sheet(rows)
self.sheet.save_to_memory(
file_type='ods',
file_type=self.file_type,
stream=ods_file)
filename = f'{date.today()}.ods'
filename = f'{date.today()}.{self.file_type}'
super().__init__(
content=ods_file.getvalue(),
@ -152,3 +154,6 @@ class ODSResponse(Response):
'Content-Disposition': f'attachment; filename="{filename}"'},
status_code = 200)
class XLSXResponse(ODSResponse):
file_type = 'xlsx'

View File

@ -1,2 +1,3 @@
pyexcel>=0.6.3,<1
pyexcel-ods>=0.5.6,<1
pyexcel-xlsx=0.6.0,<1

View File

@ -68,6 +68,11 @@ setup(
"requests",
"pytest-asyncio",
"pylint"
],
"pyexcel":[
"pyexcel",
"pyexcel-ods3",
"pyexcel-xlsx"
]
},
entry_points={