2019-11-11 08:56:08 +00:00
|
|
|
from starlette.requests import Request
|
|
|
|
from starlette.responses import *
|
2019-11-28 01:30:40 +00:00
|
|
|
from royalnet.constellation import *
|
2019-11-11 08:56:08 +00:00
|
|
|
from royalnet.utils import *
|
2019-11-28 01:30:40 +00:00
|
|
|
from ..tables import *
|
2019-11-11 08:56:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ApiDiarioListStar(PageStar):
|
|
|
|
path = "/api/diario/list"
|
|
|
|
|
|
|
|
async def page(self, request: Request) -> JSONResponse:
|
|
|
|
page_str = request.query_params.get("page", "0")
|
|
|
|
try:
|
|
|
|
page = int(page_str)
|
|
|
|
except (ValueError, TypeError):
|
2019-11-28 01:30:40 +00:00
|
|
|
return shoot(400, "Invalid offset")
|
2019-11-11 08:56:08 +00:00
|
|
|
async with self.alchemy.session_acm() as session:
|
|
|
|
if page < 0:
|
|
|
|
page = -page-1
|
2019-11-28 01:30:40 +00:00
|
|
|
entries: typing.List[Diario] = await asyncify(
|
|
|
|
session.query(self.alchemy.get(Diario))
|
|
|
|
.order_by(self.alchemy.get(Diario).diario_id.desc()).limit(500)
|
|
|
|
.offset(page * 500)
|
|
|
|
.all
|
|
|
|
)
|
2019-11-11 08:56:08 +00:00
|
|
|
else:
|
2019-11-28 01:30:40 +00:00
|
|
|
entries: typing.List[Diario] = await asyncify(
|
|
|
|
session.query(self.alchemy.get(Diario))
|
|
|
|
.order_by(self.alchemy.get(Diario).diario_id)
|
|
|
|
.limit(500)
|
|
|
|
.offset(page * 500)
|
|
|
|
.all)
|
2019-11-11 08:56:08 +00:00
|
|
|
response = [entry.json() for entry in entries]
|
|
|
|
return JSONResponse(response)
|