"""Blueprint for Reference Agreement Type API.""" 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_account.constants.error import ERROR_CODE_AUTHORIZATION from abacus_account.logic.reference_agreement_type import get_agreement_types from abacus_account.models import ReferenceAgreementType from abacus_account.schemas import ReferenceAgreementTypeDetailSchema from abacus_account.utils.authorization import authorize_resource reference_agreement_type_api = Blueprint( 'reference_agreement_type_api', __name__ ) class ReferenceAgreementTypeItemView(ItemView): """View for finding reference_agreement_type by ID.""" model_class = ReferenceAgreementType object_detail_schema = ReferenceAgreementTypeDetailSchema() def get(self, object_id, **kwargs): """Get object reference_agreement_type by its id.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorize_resource(object_id, 'reference_agreement_type') if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=403, )) return super().get(object_id, **kwargs) class ReferenceAgreementTypeList(ListView): """View for listing reference_agreement_types.""" model_class = ReferenceAgreementType list_entry_schema = ReferenceAgreementTypeDetailSchema() def get(self): """GET agreement types.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorize_resource(0, 'reference_agreement_type') if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=403, )) return flaskify(get_agreement_types()) reference_agreement_type_api.add_url_rule( '/reference-agreement-type/', methods=['GET'], view_func=ReferenceAgreementTypeItemView.as_view('reference_agreement_type') ) reference_agreement_type_api.add_url_rule( '/reference-agreement-types', methods=['GET'], view_func=ReferenceAgreementTypeList.as_view('list_reference_agreement_types') )