"""Blueprint for account_payment_term API.""" from abacus_common_logic.utils.authorization import permissions_authorize_many_accounts from abacus_common_logic.views.create_view import CreateView from abacus_common_logic.views.item_view import ItemView from flask import Blueprint from flask import g from flask import request from flask import Response from flask import stream_with_context from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from abacus_account.config import ows_client from abacus_account.constants.error import ERROR_CODE_AUTHORIZATION from abacus_account.constants.error import ERROR_CODE_FORBIDDEN from abacus_account.constants.error import ERROR_INVALID_IDS from abacus_account.constants.error import ERROR_MESSAGE_FORBIDDEN_USER from abacus_account.logic import account_payment_term as logic from abacus_account.models import AccountPaymentTerm from abacus_account.schemas.account_payment_term import \ AccountPaymentTermDetailSchema, AccountPaymentTermPostSchema, \ AccountPaymentTermPutSchema from abacus_account.utils.authorization import authorize_many_accounts from abacus_account.utils.format_error import validation_error from abacus_account.utils.request import get_optional_numeric_list_from_params account_payment_term_api = Blueprint('account_payment_term_api', __name__) class AccountPaymentTermItemView(ItemView): """View for fetching an account_payement_term by ID.""" model_class = AccountPaymentTerm object_detail_schema = AccountPaymentTermDetailSchema() put_schema = AccountPaymentTermPutSchema(exclude=('account_id',)) def put(self, object_id, **kwargs): """Update account_payment_term by 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=403, )) return super().put(object_id, **kwargs) def update_handler(self, obj, **params): """Update account_payment_term.""" return logic.update_account_payment_term(obj, **params) class AccountPaymentTermCreateView(CreateView): """Handles an account_payement_term creation.""" post_schema = AccountPaymentTermPostSchema() def post(self, **kwargs): """Create an account_payment_term.""" 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=403, )) return super().post(**kwargs) def create_handler(self, **params): """Create an account_payment_term.""" 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=403, )) return logic.create_account_payment_term(**params) account_payment_term_api.add_url_rule( '/account-payment-term//', view_func=AccountPaymentTermItemView.as_view('account_payment_term') ) account_payment_term_api.add_url_rule( '/account-payment-term/', view_func=AccountPaymentTermCreateView.as_view('create_account_payment_term') ) @account_payment_term_api.route( '/account//account-payment-term/', methods=['GET'] ) def get_account_payment_term_by_account_id(object_id): """Endpoint to GET payment term info for a specified account.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorize_many_accounts([object_id]) if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=403, )) authorized = permissions_authorize_many_accounts( ows_client, g.request_context.profile_type, g.request_context.profile_id, [object_id] ) if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_FORBIDDEN, message=ERROR_MESSAGE_FORBIDDEN_USER, status=403 )) return flaskify(logic.get_payment_term_by_account_id(object_id)) @account_payment_term_api.route( '/account/account-payment-term/dataloader', methods=['POST'] ) def get_account_payment_term_by_account_id_dataloaded(): """Endpoint to GET payment term info for dataloaded accounts. NOTE: This endpoint uses a POST method to allow for a larger list of query args. """ try: account_ids = get_optional_numeric_list_from_params() except ValueError: return flaskify( validation_error(ERROR_INVALID_IDS.format(object='Account')) ) access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorize_many_accounts(account_ids) if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=403, )) authorized = permissions_authorize_many_accounts( ows_client, g.request_context.profile_type, g.request_context.profile_id, account_ids ) if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_FORBIDDEN, message=ERROR_MESSAGE_FORBIDDEN_USER, status=403 )) return flaskify( logic.get_payment_term_by_account_id_dataloaded(account_ids) ) @account_payment_term_api.route('/account-payment-terms/snapshot', methods=['POST']) def get_account_payment_terms_snapshot_tsv(): """Get account payment terms as a TSV snapshot. NOTE: This endpoint uses a POST method to allow for a larger list of query args. """ 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=403, )) try: optional_account_ids = get_optional_numeric_list_from_params() except ValueError: return flaskify(validation_error(ERROR_INVALID_IDS.format(object='Account'))) return Response( stream_with_context(logic.account_payment_terms_export(optional_account_ids)), mimetype='text/tsv' )