Update /api/movements endpoint.

This commit is contained in:
sfigato 2019-09-03 16:59:07 +02:00 committed by blallo
parent 1e4bb57cd8
commit 7b8d577f03
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -1,5 +1,4 @@
# -*- encoding: utf-8 -*-
"""
The REST endpoints.
"""
@ -20,7 +19,6 @@ from bot_z.async_operator import AsyncOperator, push_to_loop
from api.async_bot import login, logout, checkin, checkout, status
from api import BASE_URI, DEBUG
alog = logging.getLogger("api")
routes = web.RouteTableDef()
OPERATORS = {} # type: T.Dict[T.Text, AsyncOperator]
@ -177,6 +175,7 @@ async def movements_handle(request: web.Request) -> web.Response:
session = await get_session(request)
op = OPERATORS.get(session.get("async_operator"))
if not op:
alog.debug("Missing session")
return web.json_response(
{"error": "No session", "logged_in": False}, status=401
)
@ -184,12 +183,19 @@ async def movements_handle(request: web.Request) -> web.Response:
alog.debug("movements result: %s", res)
if not res:
return web.json_response(
{"error": "No movements found", "logged_in": True}, status=404
{"error": "No movements found", "logged_in": True, "checked_in": False},
status=404,
)
movements = []
for r in res:
if r and len(r) == 2:
movements.append({"time": r[1], "type": r[0]})
resp_data = {"movements": movements}
resp_data["logged_in"] = True # type: ignore
resp_data: T.Dict[T.Text, T.Any] = {"movements": movements}
resp_data["logged_in"] = True
try:
last_movement = list(movements[-1])
resp_data["checked_in"] = True if last_movement == "Entrata" else False
except IndexError:
alog.info("No movements found")
return web.json_response(resp_data, status=200)