"""Instant Grat handlers.""" from flask import g from oto.adaptors.flask import flaskify from flask import request from owsrequest import flask_request from backend.api import app from backend.constants import error from owsresponse import response from backend.logic import instant_grat as instant_grat_logic from backend.schemas.instant_grat import InstantGratsUpdateSchema from backend.utils import handlers as handlers_util @app.route('/product//tracks/grats', methods=['GET']) @handlers_util.get_account_from_grass def get_all_instant_grats_by_product_id(product_id, account): """Get a list of instant grats by product_id. Get all the instant grats contained in a product using product_id (release_id in AR) as the identifier. Args: product_id (int): The product primary key. account (namedtuple): Account data from GRASS headers. Returns: flask.response: on successful, 200 status with JSON body Grat information related to the given product_id. """ access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: jwt_identity_id = g.request_context.jwt_identity_id if not jwt_identity_id: return flaskify(response.create_error_response( code=error.ERROR_CODE_NOT_FOUND, message='No valid identity found in request context.', status=401, )) if jwt_identity_id and not handlers_util.is_jwt_identity_authorized(jwt_identity_id): return flaskify(response.create_error_response( code=error.ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=403, )) result = instant_grat_logic.get_all_grats_by_product_id( product_id=product_id, account_type=account.type, account_id=account.id) return flaskify(result, encoder=handlers_util.DatetimeEncoder) @app.route( '/product//tracks/grats//store/' '', methods=['DELETE']) @handlers_util.get_account_from_grass def delete_instant_grat_by_store_id(product_id, track_id, store_id, account): """Delete the Instant Grat for provided product, track and store ids. For this Instant Grat active field in the AR track_instant_grat table will be set to N, so it won’t appear in result of GET request. Args: product_id (int): The product primary key. track_id (int): The track primary key. store_id (int): The store primary key. account (namedtuple): Account data from GRASS headers. Returns: flask.response: on successful, 200 status with JSON body. """ 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=403, )) result = instant_grat_logic.delete_grat_by_store_id( product_id=product_id, track_id=track_id, store_id=store_id, account_type=account.type, account_id=account.id) return flaskify(result) @app.route('/product//tracks/grats', methods=['DELETE']) @handlers_util.get_account_from_grass def delete_all_instant_grats_by_product_id(product_id, account): """Delete all Instant Grats by product_id. Delete all the Instant Grats for provided product. For these grats active field in the AR track_instant_grat table will be set to N, so they won’t appear in result of GET request. Args: product_id (int): The product primary key. account (namedtuple): Account data from GRASS headers. Returns: flask.response: on successful, 200 status with JSON body """ 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=403, )) result = instant_grat_logic.delete_all_grats_by_product_id( product_id=product_id, account_type=account.type, account_id=account.id) return flaskify(result) @app.route('/product//tracks/grats', methods=['POST']) @handlers_util.get_account_from_grass @handlers_util.get_user_from_grass def create_instant_grats_for_product_id(product_id, account, user): """Create a list of instant grats for a product_id by grats data. Args: product_id (int): The product primary key. account (namedtuple): Account data from GRASS headers. user (namedtuple): User data from GRASS headers. Returns: flask.response: on successful, 200 status with JSON body Grat information related to the given product_id. """ 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=403, )) json_data = handlers_util.parse_request_json( schema=InstantGratsUpdateSchema) result = instant_grat_logic.create_grats_for_product_id( product_id=product_id, grats_data=json_data, account_type=account.type, account_id=account.id, user_id=user.id, user_type=user.type) return flaskify(result, encoder=handlers_util.DatetimeEncoder) @app.route('/product//tracks/grats', methods=['PATCH']) @handlers_util.get_account_from_grass def update_instant_grats_for_product_id(product_id, account): """Update product instant grats by grats data. Args: product_id (int): The product primary key. account (namedtuple): Account data from GRASS headers. Returns: flask.response: on successful, 200 status with JSON body Grat information related to the given product_id. """ 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=403, )) json_data = handlers_util.parse_request_json( schema=InstantGratsUpdateSchema) result = instant_grat_logic.update_grats_by_product_id( product_id=product_id, grats_data=json_data, account_id=account.id, account_type=account.type) return flaskify(result, encoder=handlers_util.DatetimeEncoder)