"""Blueprint for Reference SAP Profit Center.""" from abacus_common_logic.views.item_view import ItemView from abacus_common_logic.views.list_view import ListView from flask import Blueprint from flask import request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from abacus_contract.constants.error \ import ERROR_CODE_FORBIDDEN, ERROR_MESSAGE_FORBIDDEN_USER from abacus_contract.models.reference_sap_profit_center import ReferenceSapProfitCenter from abacus_contract.schemas.reference_sap_profit_center \ import ReferenceSapProfitCenterSchema from abacus_contract.utils.authorization import pdp_authorize_resource reference_sap_profit_center_api = Blueprint( 'reference_sap_profit_center_api', __name__, url_prefix='/reference-sap-profit-center' ) class ReferenceSapProfitCenterItemView(ItemView): """View for getting an sap_profit_center by ID.""" model_class = ReferenceSapProfitCenter object_detail_schema = ReferenceSapProfitCenterSchema() def get(self, object_id, **kwargs): """Get sap_profit_center by ID.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = pdp_authorize_resource( object_id, 'reference_sap_profit_center' ) if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_FORBIDDEN, message=ERROR_MESSAGE_FORBIDDEN_USER, status=403, )) return super().get(object_id, **kwargs) class ReferenceSapProfitCenterListView(ListView): """View for getting a list of sap_profit_centers.""" model_class = ReferenceSapProfitCenter list_entry_schema = ReferenceSapProfitCenterSchema def get(self): """Override base GET method.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify(response.create_error_response( code=ERROR_CODE_FORBIDDEN, message=ERROR_MESSAGE_FORBIDDEN_USER, status=403, )) profit_centers = ReferenceSapProfitCenter.query.order_by( ReferenceSapProfitCenter.profit_center ).all() result = ReferenceSapProfitCenterSchema(many=True).dump(profit_centers) return flaskify(response.Response({ 'items': result, 'total_count': len(result) })) reference_sap_profit_center_api.add_url_rule( '/', methods=['GET'], view_func=ReferenceSapProfitCenterItemView.as_view('reference_sap_profit_center') ) reference_sap_profit_center_api.add_url_rule( '/', methods=['GET'], view_func=ReferenceSapProfitCenterListView.as_view('reference_sap_profit_centers') )