Source code for luna_admin.app.handlers.search
from luna3.common.exceptions import LunaApiException
from vlutils.helpers import isUUID
from app.handlers.base_handler import BaseHandler
[docs]class SearchHandler(BaseHandler):
"""
Search objects handler.
"""
@property
def MapColumnGetter(self) -> dict:
"""
Returns MAP_COLUMN_GETTER
"""
return {
"list_id": {"getter": self.objectGetters.getListInfo, "name": "list"},
"account_id": {"getter": self.objectGetters.getAccountInfo, "name": "account"},
"face_id": {"getter": self.objectGetters.getFaceInfo, "name": "face"},
"attribute_id": {"getter": self.objectGetters.getAttributesInfo, "name": "attribute"},
}
[docs] async def getElementByIdAndColumn(self, column: str, elementId: str) -> dict:
"""
Get element by id from column.
Args:
column: column name
elementId: element id
Returns:
dict with element and element type
"""
data = await self.MapColumnGetter[column]["getter"](elementId)
return {"data": data, "type": self.MapColumnGetter[column]["name"]}
[docs] async def get(self):
"""
Search element by id or email. See `spec_search`_.
.. _spec_search:
_static/api.html#operation/search
Returns:
response with element info
"""
searchParam = self.getQueryParam("q")
if isUUID(searchParam):
for column in self.MapColumnGetter:
try:
element = await self.getElementByIdAndColumn(column, searchParam)
except LunaApiException as e:
if e.statusCode == 404:
continue
raise
if element["data"] is not None:
return self.success(200, outputJson=element)
return self.success(200, outputJson={"data": None, "type": None})
else:
accounts = await self.dbAdminContext.getAccounts(email=searchParam)
if not accounts:
return self.success(200, outputJson={"data": None, "type": None})
account = await self.objectGetters.getAccountInfo(accounts[0]["account_id"])
return self.success(200, outputJson={"data": account, "type": "account"})