"""Reference Flowthrough Calculation blueprint.""" from abacus_common_logic.views.item_view import ItemView from abacus_common_logic.views.list_view import ListView from flask import Blueprint, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from abacus_contract.constants import error from abacus_contract.models import ReferenceFlowthroughCalculation from abacus_contract.schemas.reference_flowthrough_calculation \ import ReferenceFlowthroughCalculationSchema from abacus_contract.utils import authorization reference_flowthrough_calculation_api = Blueprint( 'reference_flowthrough_calculation_api', __name__ ) class ReferenceFlowthroughCalculationItemView(ItemView): """View for existing reference flowthrough calculations.""" model_class = ReferenceFlowthroughCalculation object_detail_schema = ReferenceFlowthroughCalculationSchema() def get(self, object_id, **kwargs): """Get reference flowthrough calculation by id.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorization.pdp_authorize_resource( resource_id=0, resource_type='reference_flowthrough_calculation', ) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) return super().get(object_id, **kwargs) class ReferenceFlowthroughCalculationListView(ListView): """View for listing reference flowthrough calculations.""" model_class = ReferenceFlowthroughCalculation list_entry_schema = ReferenceFlowthroughCalculationSchema() def get(self): """List reference flowthrough calculations.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify(response.create_error_response( code=error.ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, )) return super().get() reference_flowthrough_calculation_api.add_url_rule( '/reference-flowthrough-calculations/', methods=['GET'], view_func=ReferenceFlowthroughCalculationListView.as_view( 'list_reference_flowthrough_calculations' ) ) reference_flowthrough_calculation_api.add_url_rule( '/reference-flowthrough-calculation/', methods=['GET'], view_func=ReferenceFlowthroughCalculationItemView.as_view( 'get_reference_flowthrough_calculation' ) )