[lib/responses] ajout format XLSX, car ODS bug avec les dates
https://github.com/pyexcel/pyexcel-ods/issues/31
This commit is contained in:
parent
0d9dc2a018
commit
99d4aaeb8d
|
@ -16,7 +16,7 @@ import yaml
|
||||||
from starlette.exceptions import HTTPException
|
from starlette.exceptions import HTTPException
|
||||||
|
|
||||||
from halfapi.lib import acl
|
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.router import read_router
|
||||||
from halfapi.lib.constants import VERBS
|
from halfapi.lib.constants import VERBS
|
||||||
|
|
||||||
|
@ -93,6 +93,14 @@ def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine:
|
||||||
|
|
||||||
return ODSResponse(res)
|
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
|
raise NotImplementedError
|
||||||
|
|
||||||
except NotImplementedError as exc:
|
except NotImplementedError as exc:
|
||||||
|
|
|
@ -118,6 +118,8 @@ class HJSONResponse(ORJSONResponse):
|
||||||
return super().render(list(content))
|
return super().render(list(content))
|
||||||
|
|
||||||
class ODSResponse(Response):
|
class ODSResponse(Response):
|
||||||
|
file_type = 'ods'
|
||||||
|
|
||||||
def __init__(self, d_rows: typing.List[typing.Dict]):
|
def __init__(self, d_rows: typing.List[typing.Dict]):
|
||||||
try:
|
try:
|
||||||
import pyexcel as pe
|
import pyexcel as pe
|
||||||
|
@ -140,10 +142,10 @@ class ODSResponse(Response):
|
||||||
|
|
||||||
self.sheet = pe.Sheet(rows)
|
self.sheet = pe.Sheet(rows)
|
||||||
self.sheet.save_to_memory(
|
self.sheet.save_to_memory(
|
||||||
file_type='ods',
|
file_type=self.file_type,
|
||||||
stream=ods_file)
|
stream=ods_file)
|
||||||
|
|
||||||
filename = f'{date.today()}.ods'
|
filename = f'{date.today()}.{self.file_type}'
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
content=ods_file.getvalue(),
|
content=ods_file.getvalue(),
|
||||||
|
@ -152,3 +154,6 @@ class ODSResponse(Response):
|
||||||
'Content-Disposition': f'attachment; filename="{filename}"'},
|
'Content-Disposition': f'attachment; filename="{filename}"'},
|
||||||
status_code = 200)
|
status_code = 200)
|
||||||
|
|
||||||
|
|
||||||
|
class XLSXResponse(ODSResponse):
|
||||||
|
file_type = 'xlsx'
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
pyexcel>=0.6.3,<1
|
pyexcel>=0.6.3,<1
|
||||||
pyexcel-ods>=0.5.6,<1
|
pyexcel-ods>=0.5.6,<1
|
||||||
|
pyexcel-xlsx=0.6.0,<1
|
||||||
|
|
Loading…
Reference in New Issue