"""Blueprint for legacy_contract API.""" from abacus_common_logic.views.create_view import CreateView 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_AUTHORIZATION, ERROR_INVALID_IDS from abacus_contract.logic import contract as logic from abacus_contract.logic import legacy_contract from abacus_contract.schemas.legacy_contract import LegacyContractSchema from abacus_contract.utils.format_error import validation_error from abacus_contract.utils.request import get_optional_numeric_list_from_params legacy_contract_api = Blueprint('legacy_contract_api', __name__) class LegacyContractCreateView(CreateView): """Handles legacy_contract creation.""" post_schema = LegacyContractSchema(only=('oa_contract_id',)) def post(self, **kwargs): """Create a legacy_contract.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify(response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, )) return super().post(**kwargs) def create_handler(self, **params): """Create a legacy_contract.""" return legacy_contract.create_legacy_contract(**params) legacy_contract_api.add_url_rule( '/contract//legacy-contract/', methods=['POST'], view_func=LegacyContractCreateView.as_view('create_legacy_contract') ) @legacy_contract_api.route('/oa-contracts/contracts', methods=['POST']) def get_contracts_by_oa_contract_ids(): """Get contracts by list of oa_contract_ids.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify(response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, )) try: oa_contract_ids = get_optional_numeric_list_from_params() or [] except ValueError: return flaskify( validation_error(ERROR_INVALID_IDS.format(object='OA Contract')) ) return flaskify(logic.get_contracts_by_oa_contract_ids(oa_contract_ids)) @legacy_contract_api.route( '/contract//legacy-contract', methods=['GET'] ) def get_legacy_contract_by_contract_id(contract_id: int): """Get legacy_contract by contract_id.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify(response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=401, )) return flaskify( legacy_contract.get_legacy_contract_by_contract_id(contract_id) )