Source code for luna_admin.app.handlers.accounts_handler

"""
Module realize a handler for work with several accounts
"""
from sanic.response import HTTPResponse

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


[docs]class AccountsHandler(BaseHandler): """ Handler for work with several accounts """
[docs] async def get(self) -> HTTPResponse: """ Get accounts with pagination. See `spec_get_accounts`_. .. _spec_get_accounts: _static/api.html#operation/getAccounts Returns: response with account list and account count """ page, pageSize = self.getPagination() accountResponse = await self.luna3Client.lunaAccounts.getAccounts(page=page, pageSize=pageSize, raiseError=True) accounts, accountsCount = accountResponse.json["accounts"], accountResponse.json["total_count"] for account in accounts: account["email"] = account["login"] account["organization_name"] = account["description"] return self.success(200, outputJson={"accounts": accounts, "account_count": accountsCount})
[docs] async def post(self) -> HTTPResponse: """ Register a new account in luna-admin. See `spec_create_account`_. .. _spec_create_account: _static/api.html#operation/registerAccount Returns: response with account id """ data = self.request.json self.validateJson(data, NEW_ACCOUNT_SCHEMA, False) accountResponse = await self.luna3Client.lunaAccounts.createAccount( 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, ) accountId = accountResponse.json["account_id"] return self.success( 201, outputJson={"account_id": accountId}, extraHeaders={"Location": f"/{self.request.app.ctx.apiVersion}/accounts/{accountId}"}, )