Module licenseware.app_builder.reports_namespace.report_components_namespace

Expand source code
from flask import request
from flask_restx import Namespace, Resource, fields
from licenseware.decorators.auth_decorators import authorization_check
from licenseware.decorators import failsafe
from licenseware.utils.logger import log
from licenseware.common.constants import envs

from licenseware.report_builder import ReportBuilder
from licenseware.report_components import BaseReportComponent
from typing import List

from licenseware.download import download_as


def create_individual_report_component_resource(component: BaseReportComponent):
    class ComponentRes(Resource):

        @failsafe(fail_code=500)
        @authorization_check
        def post(self):
            return component.get_data(request)

        @failsafe(fail_code=500)
        @authorization_check
        def get(self):
            file_type = request.args.get('download_as')
            tenant_id = request.headers.get('Tenantid')
            data = component.get_data(request)

            if file_type is None:
                return data

            return download_as(
                file_type,
                data,
                tenant_id,
                filename=component.component_id
            )

    return ComponentRes


def get_report_components_namespace(ns: Namespace, reports: List[ReportBuilder]):
    restx_model = ns.model('ComponentFilter', dict(
        column=fields.String,
        filter_type=fields.String,
        filter_value=fields.List(fields.String)
    )
                           )

    for report in reports:
        for comp in report.components:
            ComponentRes = create_individual_report_component_resource(comp)

            docs = {
                'get': {
                    'description': 'Get component data',
                    'params': {
                        'limit': {'description': 'Limit the number of results'},
                        'skip': {'description': 'Skip the first n results'},
                        'download_as': {'description': 'Download table component as file type: csv, xlsx, json'}
                    },
                    'responses': {
                        200: 'Success',
                        403: 'Missing `Tenantid` or `Authorization` information',
                        500: 'Something went wrong while handling the request'
                    }
                },
                'post': {
                    'description': 'Get component data with an optional filter payload',
                    'params': {
                        'limit': {'description': 'Limit the number of results'},
                        'skip': {'description': 'Skip the first n results'}
                    },
                    'validate': None,
                    'expect': [restx_model],
                    'responses': {
                        200: 'Success',
                        403: 'Missing `Tenantid` or `Authorization` information',
                        500: 'Something went wrong while handling the request'
                    }
                }
            }

            ComponentRes.__apidoc__ = docs

            IRCResource = type(
                comp.component_id.replace("_", "").capitalize() + 'individual_component',
                (ComponentRes,),
                {}
            )

            ns.add_resource(IRCResource, report.report_path + comp.component_path)

    return ns

Functions

def create_individual_report_component_resource(component: BaseReportComponent)
Expand source code
def create_individual_report_component_resource(component: BaseReportComponent):
    class ComponentRes(Resource):

        @failsafe(fail_code=500)
        @authorization_check
        def post(self):
            return component.get_data(request)

        @failsafe(fail_code=500)
        @authorization_check
        def get(self):
            file_type = request.args.get('download_as')
            tenant_id = request.headers.get('Tenantid')
            data = component.get_data(request)

            if file_type is None:
                return data

            return download_as(
                file_type,
                data,
                tenant_id,
                filename=component.component_id
            )

    return ComponentRes
def get_report_components_namespace(ns: flask_restx.namespace.Namespace, reports: List[ReportBuilder])
Expand source code
def get_report_components_namespace(ns: Namespace, reports: List[ReportBuilder]):
    restx_model = ns.model('ComponentFilter', dict(
        column=fields.String,
        filter_type=fields.String,
        filter_value=fields.List(fields.String)
    )
                           )

    for report in reports:
        for comp in report.components:
            ComponentRes = create_individual_report_component_resource(comp)

            docs = {
                'get': {
                    'description': 'Get component data',
                    'params': {
                        'limit': {'description': 'Limit the number of results'},
                        'skip': {'description': 'Skip the first n results'},
                        'download_as': {'description': 'Download table component as file type: csv, xlsx, json'}
                    },
                    'responses': {
                        200: 'Success',
                        403: 'Missing `Tenantid` or `Authorization` information',
                        500: 'Something went wrong while handling the request'
                    }
                },
                'post': {
                    'description': 'Get component data with an optional filter payload',
                    'params': {
                        'limit': {'description': 'Limit the number of results'},
                        'skip': {'description': 'Skip the first n results'}
                    },
                    'validate': None,
                    'expect': [restx_model],
                    'responses': {
                        200: 'Success',
                        403: 'Missing `Tenantid` or `Authorization` information',
                        500: 'Something went wrong while handling the request'
                    }
                }
            }

            ComponentRes.__apidoc__ = docs

            IRCResource = type(
                comp.component_id.replace("_", "").capitalize() + 'individual_component',
                (ComponentRes,),
                {}
            )

            ns.add_resource(IRCResource, report.report_path + comp.component_path)

    return ns