Source code for luna_backport3.app.handlers.accounts_handler
from sanic.response import HTTPResponse
from app.handlers.base_handler import BaseHandler
from app.handlers.handlers_mixin import HandlersMixin
from app.handlers.schemas import CREATE_ACCOUNT_SCHEMA
[docs]class AccountHandler(BaseHandler):
    """
    Handler to get account information, only authorization is required.
    """
[docs]    async def get(self) -> HTTPResponse:
        """
        Account info handler. See `spec_get_account`_.
        .. _spec_get_account:
            _static/api.html#operation/getAccount
        Returns:
            json with email, organization name and account status
        """
        account = await self.dbContext.getAccountByAccountId(accountId=self.accountId)
        return self.success(
            200,
            outputJson={
                "email": account.email,
                "organization_name": account.organization_name,
                "suspended": (not account.active),
            },
        )  
[docs]class AccountsHandler(HandlersMixin, BaseHandler):
    """
    Accounts handler
    """
[docs]    async def post(self) -> HTTPResponse:
        """
        Accounts registration. See `spec_create_account`_.
        .. _spec_create_account:
            _static/api.html#operation/createAccount
        Returns:
            json with token
        """
        reqJson = self.request.json
        self.validateJson(reqJson, CREATE_ACCOUNT_SCHEMA, useJsonSchema=False)
        organizationName = reqJson["organization_name"]
        password = reqJson["password"]
        email = reqJson["email"].lower()
        account = await self.dbContext.createAccount(organizationName=organizationName, email=email, password=password)
        token = account["token"]
        accountId = account["accountId"]
        await self.createHandlers(accountId)
        return self.success(201, outputJson={"token": str(token)})