Source code for luna_admin.app.handlers.account_handler

"""
Module realize handlers for work with an account.
"""
from sanic.response import HTTPResponse

from app.handlers.base_handler import BaseHandler
from app.handlers.schemas import NEW_ACCOUNT_SCHEMA


[docs]class AccountHandler(BaseHandler): """ Handler for work with an account. """ # (tuple): uri params for resource "/accounts/{account_id}" uriParams = ("accountId",)
[docs] async def get(self, accountId: str) -> HTTPResponse: """ Get account info. See `spec_get_account_info`_. .. _spec_get_account_info: _static/api.html#operation/getAccount Returns: response with account id """ accountInfo = (await self.luna3Client.lunaAccounts.getAccount(accountId, raiseError=True)).json accountInfo["email"] = accountInfo["login"] accountInfo["organization_name"] = accountInfo["description"] accountStats = await self.objectGetters.getAccountStats(accountId) return self.success(200, outputJson=dict(info=accountInfo, stats=accountStats))
[docs] async def delete(self, accountId: str) -> HTTPResponse: """ Delete account id handler. See `spec_remove_account`_. .. _spec_remove_account: _static/api.html#operation/removeAccount Args: accountId: account id Returns: response with status code 204 """ await self.luna3Client.lunaAccounts.deleteAccount(accountId) return self.success(204)
[docs] async def put(self, accountId: str) -> HTTPResponse: """ Register a new account with the specified id in luna-admin. See `spec_put_account`_. .. _spec_put_account: _static/api.html#operation/putAccount Returns: response with account id """ data = self.request.json self.validateJson(data, NEW_ACCOUNT_SCHEMA, False) await self.luna3Client.lunaAccounts.createAccount( accountId=accountId, accountType=data.get("account_type", "advanced_user"), login=data.get("login") or data.get("email"), password=data.get("password") or data.get("email"), description=data.get("description") or data.get("organization_name"), raiseError=True, ) return self.success( 200, outputJson={"account_id": accountId}, extraHeaders={"Location": f"/{self.request.app.ctx.apiVersion}/accounts/{accountId}"}, )