diff --git a/halfapi/lib/domain.py b/halfapi/lib/domain.py index ecf3e0b..4126782 100644 --- a/halfapi/lib/domain.py +++ b/halfapi/lib/domain.py @@ -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: diff --git a/halfapi/lib/responses.py b/halfapi/lib/responses.py index b493025..b1f3923 100644 --- a/halfapi/lib/responses.py +++ b/halfapi/lib/responses.py @@ -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' diff --git a/optional-requirements.txt b/optional-requirements.txt index b28776d..835813a 100644 --- a/optional-requirements.txt +++ b/optional-requirements.txt @@ -1,2 +1,3 @@ pyexcel>=0.6.3,<1 pyexcel-ods>=0.5.6,<1 +pyexcel-xlsx=0.6.0,<1 diff --git a/setup.py b/setup.py index 6891fd2..05b0603 100755 --- a/setup.py +++ b/setup.py @@ -68,6 +68,11 @@ setup( "requests", "pytest-asyncio", "pylint" + ], + "pyexcel":[ + "pyexcel", + "pyexcel-ods3", + "pyexcel-xlsx" ] }, entry_points={