"""Blueprint for Reference Payment Entity.""" from http import HTTPStatus from abacus_common_logic.views.item_view import ItemView from abacus_common_logic.views.list_view import ListView from common_apispec import doc, marshal_with 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.logic import reference_payment_entity as logic from abacus_contract.models.reference_payment_entity import ReferencePaymentEntity from abacus_contract.schemas.reference_payment_entity import ( ReferencePaymentEntityDataloaderSchema, ReferencePaymentEntitySchema, ) from abacus_contract.utils import authorization from abacus_contract.utils.format_error import validation_error from abacus_contract.utils.request import get_optional_numeric_list_from_params from core.cache import cache_reference_data from core.config import Config reference_payment_entity_api = Blueprint('reference_payment_entity_api', __name__) class ReferencePaymentEntityItemView(ItemView): """View to get a reference_payment_entity by ID.""" model_class = ReferencePaymentEntity object_detail_schema = ReferencePaymentEntitySchema() @cache_reference_data def get(self, object_id, **kwargs): """Get reference_payment_entity 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_entity', ) 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 ReferencePaymentEntityListView(ListView): """View to get a list of reference_payment_entity.""" model_class = ReferencePaymentEntity list_entry_schema = ReferencePaymentEntitySchema @cache_reference_data 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.ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, ) ) reference_payment_entities = self.base_list_query().all() result = ReferencePaymentEntitySchema(many=True).dump( reference_payment_entities ) count = self.base_list_query().count() return flaskify(response.Response({'items': result, 'total_count': count})) reference_payment_entity_api.add_url_rule( '/reference-payment-entity//', methods=['GET'], view_func=ReferencePaymentEntityItemView.as_view('reference_payment_entity'), ) reference_payment_entity_api.add_url_rule( '/reference-payment-entities/', methods=['GET'], view_func=ReferencePaymentEntityListView.as_view('reference_payment_entities'), ) @reference_payment_entity_api.route( '/reference-payment-entity/dataloader', methods=['POST'], ) @doc( summary='Fetch payment entities by IDs.', description='Retrieves reference payment entities for the ids passed in the request body.', ) @marshal_with( ReferencePaymentEntityDataloaderSchema(many=True), code=HTTPStatus.OK, description='One ordered entry per requested id (the payment entity or null).', ) @marshal_with( None, code=HTTPStatus.BAD_REQUEST, description='Invalid request body payload or validation error.', ) @marshal_with( None, code=HTTPStatus.FORBIDDEN, description=HTTPStatus.FORBIDDEN.phrase, ) def get_payment_entities_dataloader(): """GET payment entities for the given ids.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorization.pdp_authorize_resource( # Id of 0 means "all reference payment entities"; all-or-nothing, # matching the single-id GET's authorization. resource_id=0, resource_type='reference_payment_entity', ) if not authorized: return flaskify( response.create_error_response( code=error.ERROR_CODE_FORBIDDEN, message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403, ) ) try: payment_entity_ids = get_optional_numeric_list_from_params() except (ValueError, TypeError): return flaskify( validation_error( error.ERROR_INVALID_IDS.format(object='ReferencePaymentEntity') ) ) if len(payment_entity_ids) > Config.OWS_BATCH_LIMIT: return flaskify( validation_error( error.ERROR_TOO_MANY_DATALOADER_IDS.format(limit=Config.OWS_BATCH_LIMIT) ) ) return flaskify(logic.get_payment_entities_by_ids(payment_entity_ids))