"""Rights Attribute 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.constants import header from backend.logic import rights_attribute as rights_attribute_logic from backend.schemas.rights_attribute import TrackRightsAttributeUpsertSchema from backend.utils import api as api_utils from backend.utils import handlers as handlers_util @app.route('/rights-attributes', methods=['GET']) def get_rights_attributes(): """Get all available rights attributes. Returns: flask.Response: list of rights attributes """ 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 = rights_attribute_logic.get_rights_attributes() return flaskify(result) @app.route('/suggested-rights-attributes/', methods=['GET']) def get_suggested_rights_attributes(tuid): """Get all suggested rights attributes. Returns: flask.Response: list of suggested rights attributes """ 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 = rights_attribute_logic.get_suggested_rights_attributes(tuid) handlers_util.persist_and_strip_suggestions( result, tuid, attribute_id_key='rights_attribute_id', write_func=rights_attribute_logic.write_rights_attribute_suggestions, attr_type='rights attribute', ) return flaskify(result) @app.route('/rights-attributes/', methods=['GET']) def get_track_rights_attributes(tuid): """Get all rights attributes associated with the given tuid. Returns: flask.Response: list of rights attribute ids """ 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 = rights_attribute_logic.get_track_rights_attributes(tuid) items = result.message.get('items', []) if isinstance(result.message, dict) else [] # The presence of the 'reasons' key indicates these attributes were suggested if any('reasons' in item for item in items): handlers_util.persist_and_strip_suggestions( result, tuid, attribute_id_key='rights_attribute_id', write_func=rights_attribute_logic.write_rights_attribute_suggestions, attr_type='rights attribute', ) return flaskify(result) @app.route('/rights-attributes-edits/', methods=['GET']) def get_track_rights_attributes_edits(tuid): """Get all rights attributes edits associated with the given tuid. Returns: flask.Response: list of rights attribute ids """ 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 = rights_attribute_logic.get_track_rights_attributes_edits(tuid) return flaskify(result) @app.route('/upsert-rights-attributes', methods=['POST']) @handlers_util.get_account_from_grass def upsert_track_rights_attributes(account): """Bulk upsert rights attributes for tracks. Args: account (namedtuple): Account data from GRASS headers. Returns: flask.Response: success message or database error """ orchard_identity_id = request.headers.get(header.ORCHARD_IDENTITY_ID) if not orchard_identity_id: return flaskify(api_utils.create_validation_error_response(error.ERROR_MESSAGE_MISSING_ORCHARD_IDENTITY_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=TrackRightsAttributeUpsertSchema) result = rights_attribute_logic.update_track_rights_attributes( json_data['items'], orchard_identity_id) return flaskify(result) @app.route('/upsert-rights-attributes-edits', methods=['POST']) @handlers_util.get_account_from_grass def upsert_track_rights_attributes_edits(account): """Bulk upsert rights attributes edits for tracks. Args: account (namedtuple): Account data from GRASS headers. Returns: flask.Response: success message or database error """ 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=TrackRightsAttributeUpsertSchema) result = rights_attribute_logic.update_track_rights_attributes_edits( json_data['items']) return flaskify(result)