1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-18 07:34:20 +00:00
royalnet/royalpack/stars/api_diario_list.py

46 lines
1.4 KiB
Python
Raw Normal View History

2020-06-26 14:13:11 +00:00
from typing import *
import royalnet.constellation.api as rca
import royalnet.utils as ru
from ..tables import *
2019-11-11 08:56:08 +00:00
2020-06-26 14:13:11 +00:00
class ApiDiarioPagesStar(rca.ApiStar):
2020-06-22 17:27:11 +00:00
path = "/api/diario/pages/v1"
2020-03-09 20:21:07 +00:00
parameters = {
2020-06-22 17:27:11 +00:00
"get": {
"page": "The diario page you want to get. Can be negative to get the entries in reverse order."
}
2020-03-09 20:21:07 +00:00
}
tags = ["diario"]
2020-06-26 14:13:11 +00:00
@rca.magic
async def get(self, data: rca.ApiData) -> ru.JSON:
2020-06-22 17:27:11 +00:00
"""Get a diario page made of up to 500 diario entries."""
2020-02-08 00:49:07 +00:00
page_str = data["page"]
2019-11-11 08:56:08 +00:00
try:
page = int(page_str)
2020-02-08 00:49:07 +00:00
except ValueError:
2020-06-26 14:13:11 +00:00
raise rca.InvalidParameterError("'page' is not a valid int.")
2020-02-08 00:49:07 +00:00
if page < 0:
page = -page-1
2020-06-26 14:13:11 +00:00
entries: List[Diario] = await ru.asyncify(
2020-02-08 00:49:07 +00:00
data.session
.query(self.alchemy.get(Diario))
.order_by(self.alchemy.get(Diario).diario_id.desc()).limit(500)
.offset(page * 500)
.all
)
else:
2020-06-26 14:13:11 +00:00
entries: List[Diario] = await ru.asyncify(
2020-02-08 00:49:07 +00:00
data.session
.query(self.alchemy.get(Diario))
.order_by(self.alchemy.get(Diario).diario_id)
.limit(500)
.offset(page * 500)
.all
)
response = [entry.json() for entry in entries]
return response