"""Blueprint for Reference Payment Type.""" 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 import constants from abacus_contract.constants import error from abacus_contract.models.reference_payment_type import ( ReferencePaymentType ) from abacus_contract.schemas.reference_payment_type import ( ReferencePaymentTypeSchema ) from abacus_contract.utils import authorization reference_payment_type_api = Blueprint( 'reference_payment_type_api', __name__ ) class ReferencePaymentTypeItemView(ItemView): """View for getting a reference payment type by ID.""" model_class = ReferencePaymentType object_detail_schema = ReferencePaymentTypeSchema() def get(self, object_id, **kwargs): """Get reference payment type 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_payment_type', ) 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 ReferencePaymentTypeListView(ListView): """View for getting a list of reference payment types.""" model_class = ReferencePaymentType list_entry_schema = ReferencePaymentTypeSchema() def get(self): """Get list of reference payment types.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return response.create_error_response( code=error.ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) return super().get() def base_list_query(self): """Overridden query to return only payment_type == 'advance'.""" return self.model_class.query.filter_by( payment_type=constants.PAYMENT_TYPES.ADVANCE ) reference_payment_type_api.add_url_rule( '/reference-payment-type/', methods=['GET'], view_func=ReferencePaymentTypeItemView.as_view( 'reference_payment_type' ) ) reference_payment_type_api.add_url_rule( '/reference-payment-types', methods=['GET'], view_func=ReferencePaymentTypeListView.as_view( 'reference_payment_types' ) )