Source code for luna_faces.app.handlers.list_handler

from sanic.response import HTTPResponse

from app.handlers.base_handler import BaseRequestHandler
from app.handlers.helpers import SearchListsFilters
from app.handlers.query_validators import validateIntAsBool
from app.handlers.schemas import PATCH_LIST_USER_SCHEMA
from crutches_on_wheels.errors.errors import Error
from crutches_on_wheels.errors.exception import VLException
from crutches_on_wheels.web.query_getters import uuidGetter


[docs]class ListHandler(BaseRequestHandler): """ Handler for work with list Resource: "/{api_version}/lists/{list_id}" """
[docs] async def checkList(self, listId: str) -> None: """ Checking exist list or not. Args: listId: list id Raises: VLException(Error.ListNotFound, 404, isCriticalError=False): if list is not exist """ accountId = self.getQueryParam("account_id", uuidGetter) if not await self.facesContext.isListsExist({listId}, accountId): raise VLException(Error.ListNotFound.format(listId), 404, isCriticalError=False)
[docs] async def head(self, listId: str) -> HTTPResponse: """ Check list existence. See `spec_check_list`_. .. _spec_check_list: _static/api.html#operation/checkList Returns: response with status code 200 """ await self.checkList(listId) return self.success(200, extraHeaders={"Content-Type": "application/json"})
[docs] async def get(self, listId: str) -> HTTPResponse: """ Get list by id. See `spec_get_list`_. .. _spec_get_list: _static/api.html#operation/getList Returns: response json with list and status code 200 """ accountId = self.getQueryParam("account_id", uuidGetter) filters = SearchListsFilters(account_id=accountId, list_ids=[listId]) lists = await self.facesContext.getLists(filters=filters) if not len(lists): return self.error(404, error=Error.ListNotFound.format(listId)) return self.success(200, outputJson=lists[0])
[docs] async def patch(self, listId: str) -> HTTPResponse: """ Update list user data. See `spec_patch_list`_. .. _spec_patch_list: _static/api.html#operation/patchList Returns: response with status code 204 """ accountId = self.getQueryParam("account_id", uuidGetter) data = self.request.json self.validateJson(data, PATCH_LIST_USER_SCHEMA, False) updatedListCount = await self.facesContext.updateListUserData(listId, data["user_data"], accountId=accountId) if not updatedListCount: raise VLException(Error.ListNotFound.format(listId), 404, isCriticalError=False) return self.success(204)
[docs] async def delete(self, listId: str) -> HTTPResponse: """ Delete list. See `spec_delete_list`_. .. _spec_delete_list: _static/api.html#operation/deleteList Returns: response with status code 204 """ removeWithFaces = self.getQueryParam("with_faces", validateIntAsBool, default=0) accountId = self.getQueryParam("account_id", uuidGetter) removedListCount = await self.facesContext.deleteLists([listId], withFaces=removeWithFaces, accountId=accountId) if not removedListCount: raise VLException(Error.ListNotFound.format(listId), 404, isCriticalError=False) if removeWithFaces: return self.success(202) return self.success(204)