"""Track publishing handlers.""" from flask import request from oto.adaptors.flask import flaskify from owsrequest import flask_request from owsresponse import response from backend.api import app from backend.constants import error from backend.logic import track_publishing as track_publishing_logic from backend.utils import handlers as handlers_util from backend.validation import api_schema_validators @app.route( '/product//tracks/publishing-obligation', methods=['GET']) @handlers_util.get_account_from_grass def get_tracks_publishing_obligation_data_for_product(product_id, account): """GET publishing obligation data for tracks in product. 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 = track_publishing_logic.get_all_for_product( product_id=product_id, account_type=account.type, account_id=account.id) return flaskify(result) @app.route( '/product//tracks/publishing-obligation', methods=['PUT']) @handlers_util.validate_body(api_schema_validators.PUT_PUB_OBL_BODY_VALIDATOR) @handlers_util.get_account_info def update_tracks_publishing_obligation(product_id, account_type, account_id): """Update publishing obligation for tracks in product. Args: product_id (int): The product primary key. account_type (string): The user account type. account_id (int): The user account id. 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 = track_publishing_logic.update( product_id=product_id, request_data=request.get_json(), account_type=account_type, account_id=account_id) return flaskify(result) @app.route( '/product//tracks/validate/publishing-obligation', methods=['GET']) @handlers_util.get_account_info def validate_tracks_pub_obl_for_product(product_id, account_type, account_id): """Validate the publishing obligation for all tracks of a product. Returns the total number of tracks and the number of valid ones. Args: product_id (int): The product primary key. account_type (string): The user account type. account_id (int): The user account id. Returns: flask.response: on successful, 200 status with JSON body Example: { "total_tracks": 10, "valid_tracks": 8, "errors": {...} } """ 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 = track_publishing_logic.validate_for_product( product_id=product_id, account_type=account_type, account_id=account_id, ) return flaskify(result)