"""Blueprint for accounting period report API.""" from abacus_common_logic.views.create_view import CreateView from flask import Blueprint, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from royalties.constants.error import ERROR_CODE_AUTHORIZATION from royalties.logic import accounting_period_report as logic from royalties.schemas.accounting_period_report import AccountingPeriodReportPostSchema accounting_period_report_api = Blueprint('accounting_period_report_api', __name__) class AccountingPeriodReportCreateView(CreateView): """Handles accounting period report creation.""" post_schema = AccountingPeriodReportPostSchema() def post(self, **kwargs): """Create accounting period report.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) return super().post(**kwargs) def create_handler(self, **params): """Create accounting period report.""" return logic.create_or_update_accounting_period_report(**params) accounting_period_report_api.add_url_rule( '/accounting-period//accounting-period-report', methods=['POST'], view_func=AccountingPeriodReportCreateView.as_view( 'create_accounting_period_report' ), ) @accounting_period_report_api.route( '/accounting-period//accounting-period-reports', methods=['GET'], ) def get_acc_period_reports_by_acc_period_id(accounting_period_id): """Get accounting period reports by accounting period id.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) result = logic.get_acc_period_reports_by_acc_period_id(accounting_period_id) return flaskify(result) @accounting_period_report_api.route( '/accounting-period//report-type//accounting-period-report', methods=['GET'], ) def get_acc_period_report(accounting_period_id, report_type): """Get accounting period report by accounting period id and report type.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) result = logic.get_acc_period_report(accounting_period_id, report_type) return flaskify(result)