Source code for luna_admin.app.handlers.account_lists_handler
"""
Module realize a handler for work with lists from luna-faces
"""
import asyncio
from luna3.common.exceptions import LunaApiException
from sanic.response import HTTPResponse
from app.handlers.base_handler import BaseHandler
from common.object_getters import ObjectGetters
from crutches_on_wheels.errors.errors import Error
from crutches_on_wheels.web.query_getters import uuidGetter
[docs]def getViewOfList(lunaList: dict, faceCount: int) -> dict:
"""
Convert luna list from luna-faces to luna-admin view
Args:
lunaList: luna list from luna-faces
faceCount: list face count
Returns:
new view
"""
return {"list_id": lunaList["list_id"], "account_id": lunaList["account_id"],
"last_update_time": lunaList["last_update_time"],
"face_count": faceCount,
"user_data": lunaList["user_data"]}
[docs]class AccountListsHandler(BaseHandler):
"""
Handler for lists.
"""
[docs] async def get(self) -> HTTPResponse:
"""
Get lists with filters. See `spec_get_lists`_.
.. _spec_get_lists:
_static/api.html#operation/getLists
Returns:
response with list info
"""
page, pageSize = self.getPagination()
accountId = self.getQueryParam("account_id", uuidGetter, default=None)
facesClient = self.luna3Client.lunaFaces
lists, listCount = await asyncio.gather(facesClient.getLists(page=page, pageSize=pageSize, raiseError=True,
accountId=accountId),
facesClient.getListsCount(raiseError=True, accountId=accountId))
countRequests = []
for lunaList in lists.json["lists"]:
countRequests.append(facesClient.getFacesCount(listId=lunaList["list_id"], raiseError=True))
responses = await asyncio.gather(*countRequests)
res = []
for listIndex, lunaList in enumerate(lists.json["lists"]):
res.append(getViewOfList(lunaList, responses[listIndex].json["faces_count"]))
return self.success(200, outputJson={"lists": res, "list_count": listCount.json["lists_count"]})
[docs]class AccountListHandler(BaseHandler):
"""
Luna-faces list handler.
"""
# (tuple): uri params for resource "/lists/{listId}"
uriParams = ('listId',)
[docs] async def get(self, listId: str) -> HTTPResponse:
"""
Get list info. See `spec_get_list_info`_.
.. _spec_get_list_info:
_static/api.html#operation/getListById
Returns:
response with list info
"""
try:
lunaList = await ObjectGetters(self.request).getListInfo(listId)
return self.success(200, outputJson=lunaList)
except LunaApiException as e:
if e.statusCode == 404:
return self.error(404, error=Error.ListNotFound.format(listId))
else:
raise