"""Blueprint for account_tax_info API.""" from abacus_common_logic.utils.authorization import permissions_authorize_many_accounts 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_DONT_HAVE_PERMISSIONS 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_tax_info as logic from abacus_account.models.account_tax_info import AccountTaxInfo from abacus_account.schemas.account_tax_info import ( AccountTaxInfoDetailSchema, AccountTaxInfoListInputSchema, AccountTaxInfoPutSchema ) 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 ) from abacus_account.utils.validations import validate_payload, validate_record_owner account_tax_info_api = Blueprint('account_tax_info_api', __name__) class AccountTaxInfoItemView(ItemView): """View for finding an account_tax_info by ID.""" model_class = AccountTaxInfo object_detail_schema = AccountTaxInfoDetailSchema() put_schema = AccountTaxInfoPutSchema() def get(self, object_id, **kwargs): """Get object account_tax_info by its id.""" account_tax_info_response = super().get(object_id, **kwargs) account_id = account_tax_info_response.get_json().get('account_id') access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: authorized = authorize_many_accounts([account_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, [account_id] ) if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_FORBIDDEN, message=ERROR_MESSAGE_FORBIDDEN_USER, status=403 )) return account_tax_info_response def put(self, object_id, **kwargs): """Update object account_tax_info by its id.""" account_tax_info_response = super().get(object_id, **kwargs) account_id = account_tax_info_response.get_json().get('account_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, )) authorized = permissions_authorize_many_accounts( ows_client, g.request_context.profile_type, g.request_context.profile_id, [account_id] ) if not authorized: return flaskify(response.create_error_response( code=ERROR_CODE_FORBIDDEN, message=ERROR_MESSAGE_FORBIDDEN_USER, status=403 )) return super().put(object_id, **kwargs) def update_handler(self, obj, **params): """Update account_tax_info.""" result = validate_record_owner(request, obj.account_id) if not result: return response.Response( message={'error': ERROR_DONT_HAVE_PERMISSIONS}, status=401 ) return logic.update_account_tax_info(obj, **params) account_tax_info_api.add_url_rule( '/account-tax-info/', methods=['GET', 'PUT'], view_func=AccountTaxInfoItemView.as_view('account_tax_info') ) @account_tax_info_api.route( '/account//account-tax-info/', methods=['GET'] ) def get_account_tax_info_by_account_id(object_id): """Endpoint to GET account_tax_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_account_tax_info_by_account_id(object_id)) @account_tax_info_api.route('/account-tax-info/snapshot', methods=['POST']) def get_account_tax_info_snapshot_tsv(): """Get account tax info 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=401, )) 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_tax_info_export(optional_account_ids)), mimetype='text/tsv' ) @account_tax_info_api.route('/accounts/account-tax-info', methods=['POST']) def get_account_tax_info_list(): """ Get accounts tax info list with optional filter by accounts ids. 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=401, )) params = validate_payload( {**request.args, 'account_ids': request.json}, AccountTaxInfoListInputSchema ) return flaskify(logic.get_account_tax_info_list(**params))