"""Network Participant Handlers.""" from flask import request from marshmallow import ValidationError from owsrequest.flask_request import request_context_from_headers from owsresponse import response from owsresponse.adaptors.flask import flaskify from participant.api import app from participant.constants import error from participant.logic import network_participant from participant.schemas.input.create_network_participant import ( CreateNetworkParticipantSchema, ) from participant.schemas.input.search_network_participant import ( SearchNetworkParticipantSchema, ) from participant.schemas.input.update_network_participant import ( UpdateNetworkParticipantSchema, ) from participant.utils import json_encoder from participant.utils.handler import validate_request_data @app.route('/network-participants/search', methods=['GET']) def search_network_participant(): """Find Network Participant by name or name substring. Args: name (str): Name to search for. network_id (int): Network Participant's network id to search for. Returns: flask.Response: Response with all matching network participant nodes. """ name = request.args.get('name') network_id = request.args.get('network_id') params = dict(name=name, network_id=network_id) try: cleaned = SearchNetworkParticipantSchema().load(params) except ValidationError as err: return flaskify( response.create_error_response( code=error.ERROR_CODE_INVALID_USAGE, message=err.messages ) ) ownership_response = network_participant.current_user_owns_network_id( cleaned['network_id'] ) if not ownership_response: return flaskify(ownership_response) return flaskify( network_participant.search_network_participant( cleaned['name'], cleaned['network_id'] ), encoder=json_encoder.JSONNeo4jEncoder, ) @app.route('/network-participants/', methods=['GET']) def get_network_participant_by_id(network_participant_id): """Find Network Participant by id. Args: network_participant_id (int): The ID of the network participant. """ return flaskify( network_participant.get_network_participant_by_id(network_participant_id), encoder=json_encoder.JSONNeo4jEncoder, ) @app.route('/network-participants', methods=['POST']) @validate_request_data(schema=CreateNetworkParticipantSchema()) def create_network_participant(): """Create a new Network Participant record. Args: name (str): Label Participant name. network_id (int): Network Participant's network id. """ data = request.get_json() cleaned = CreateNetworkParticipantSchema().load(data) ownership_response = network_participant.current_user_owns_network_id( cleaned['network_id'] ) if not ownership_response: return flaskify(ownership_response) return flaskify( network_participant.create_network_participant(**cleaned), encoder=json_encoder.JSONNeo4jEncoder, ) @app.route( '/network-participants//network/', methods=['PUT'], ) @validate_request_data(schema=UpdateNetworkParticipantSchema()) def update_network_participant(network_participant_id, network_id): """Update Network Participant. Args: network_participant_id (int): Network Participant id. network_id (int): Network id. """ data = request.get_json() cleaned = UpdateNetworkParticipantSchema().load(data, partial=True) ownership_response = network_participant.current_user_owns_network_id(network_id) if not ownership_response: return flaskify(ownership_response) return flaskify( network_participant.update_network_participant( network_participant_id, network_id, cleaned ), encoder=json_encoder.JSONNeo4jEncoder, ) @app.before_request @request_context_from_headers() def before_request(): """Code to be executed before each request."""