<commit_before>#!/usr/bin/env python
'''
API v1.0 List

'''
__author__ = 'M@Campbell'

from flask import jsonify, request, current_app, url_for
from ooiservices.app.main import api
from ooiservices.app import db
from authentication import auth
from ooiservices.app.models import Array, PlatformDeployment, InstrumentDeployment
from ooiservices.app.models import Stream, StreamParameter, Organization, Instrumentname
import json
import yaml
from wtforms import ValidationError


@api.route('/arrays')
def get_arrays():
    arrays = Array.query.all()
    return jsonify( {'arrays' : [array.to_json() for array in arrays] })

@api.route('/arrays/', methods=['POST'])
@auth.login_required
def set_array():
    array = Array.from_json(request.json)
    db.session.add(array)
    db.session.commit()
    return jsonify(array.to_json())

@api.route('/arrays/<string:id>')
def get_array(id):
    array = Array.query.filter_by(array_code=id).first_or_404()
    return jsonify(array.to_json())

@api.route('/platform_deployments')
def get_platform_deployments():
    if 'array_id' in request.args:
        platform_deployments = \
        PlatformDeployment.query.filter_by(array_id=request.args['array_id']).order_by(PlatformDeployment.reference_designator).all()
    else:
        platform_deployments = PlatformDeployment.query.all()
    return jsonify({ 'platform_deployments' : [platform_deployment.to_json() for platform_deployment in platform_deployments] })

@api.route('/platform_deployments/<string:id>')
def get_platform_deployment(id):
    platform_deployment = PlatformDeployment.query.filter_by(reference_designator=id).first_or_404()
    return jsonify(platform_deployment.to_json())

@api.route('/instrument_deployment', methods=['GET'])
def get_instrument_deployments():
    if 'platform_deployment_id' in request.args:
        instrument_deployments = \
        InstrumentDeployment.query.filter_by(platform_deployment_id=request.args['platform_deployment_id']).all()
        # TODO: Actually link the tables
        for i_d in instrument_deployments:
            instrument_name = Instrumentname.query.filter(Instrumentname.instrument_class == i_d.display_name).first()
            if instrument_name:
                i_d.display_name = instrument_name.display_name
    else:
        instrument_deployments = InstrumentDeployment.query.all()
    return jsonify({ 'instrument_deployments' : [instrument_deployment.to_json() for instrument_deployment in instrument_deployments] })

@api.route('/instrument_deployment/<int:id>', methods=['GET'])
def get_instrument_deployment(id):
    instrument_deployment = InstrumentDeployment.query.get(id)
    if instrument_deployment is None:
        return jsonify(error="Instrument Deployment Not Found"), 404
    return jsonify(**instrument_deployment.to_json())

@api.route('/instrument_deployment', methods=['POST'])
def post_instrument_deployment():
    try:
        new_deploy = InstrumentDeployment.from_json(data)
        db.session.add(new_deploy)
        db.session.commit()
    except ValidationError as e:
        return jsonify(error=e.message), 400
    return jsonify(**new_deploy.to_json()), 201

@api.route('/instrument_deployment/<int:id>', methods=['PUT'])
def put_instrument_deployment(id):
    try:
        data = json.loads(request.data)
        existingDeply = InstrumentDeployment.query.get(id)
        if existingDeply is None:
            return jsonify(error="Invalid ID, record not found"), 404
        existingDeply.display_name = data.get('display_name', existingDeply.display_name)
        existingDeply.start_date = data.get('start_date', existingDeply.start_date)
        existingDeply.end_date = data.get('end_date', existingDeply.end_date)
        existingDeply.platform_deployment_id = data.get('platform_deployment_id', existingDeply.platform_deployment_id)
        existingDeply.depth = data.get('depth', existingDeply.depth)
        existingDeply.geo_location = data.get('geo_location', existingDeply.geo_location)
        db.session.add(existingDeply)
        db.session.commit()
    except ValidationError as e:
        return jsonify(error=e.message), 400
    return jsonify(**existingDeply.to_json()), 200

@api.route('/instrument_deployment/<int:id>', methods=['DELETE'])
def delete_instrument_deployment(id):
    deployment = InstrumentDeployment.query.get(id)
    if deployment is None:
        return jsonify(error="Instrument Deployment Not Found"), 404
    db.session.delete(deployment)
    db.session.commit()
    return jsonify(), 200




#@api.route('/instrument_deployment', methods=['POST'])
#@api.route('/instrument_deployment', methods=['PUT'])
#def submit_deployment():
#    '''
#    Acts as a pass-thru proxy to to the services
#    '''
#    #csrf_token = session.pop('_csrf_token', None)
#    #data = json.loads(request.data)
#    #removes unicode
#    data = yaml.load(request.data)
#
#    # check for delete for delete
#    #temp
#    if 'delete' in data:
#        try:
#            existingDeply = InstrumentDeployment.query.filter_by(id=data['id']).first_or_404()
#            db.session.delete(existingDeply)
#            db.session.commit()
#        except ValidationError as e:
#            return jsonify(error=e.message), 409
#    # check for existing id for edit
#    elif 'id' in data:
#        try:
#        except ValidationError as e:
#            return jsonify(error=e.message), 409
#    else:
#        #if not csrf_token or csrf_token != data['_csrf_token']:
#        #    return jsonify(error="CSRF Error"), 401
#        #api_key = app.config['UI_API_KEY']
#        try:
#        except ValidationError as e:
#            return jsonify(error=e.message), 409
#
#    return "success", 204

@api.route('/streams')
def get_streams():
    streams = Stream.query.all()
    return jsonify({ 'streams' : [stream.to_json() for stream in streams] })

@api.route('/streams/<string:id>')
def get_stream(id):
    stream = Stream.query.filter_by(stream_name=id).first_or_404()
    return jsonify(stream.to_json())

@api.route('/parameters')
def get_parameters():
    parameters = StreamParameter.query.all()
    return jsonify({ 'parameters' : [parameter.to_json() for parameter in parameters] })

@api.route('/parameters/<string:id>')
def get_parameter(id):
    parameter = StreamParameter.query.filter_by(stream_parameter_name=id).first_or_404()
    return jsonify(parameter.to_json())

@api.route('/organization', methods=['GET'])
def get_organizations():
    organizations = [o.serialize() for o in Organization.query.all()]
    return jsonify(organizations=organizations)<commit_msg>Removes spaces<commit_after>#!/usr/bin/env python
'''
API v1.0 List

'''
__author__ = 'M@Campbell'

from flask import jsonify, request, current_app, url_for
from ooiservices.app.main import api
from ooiservices.app import db
from authentication import auth
from ooiservices.app.models import Array, PlatformDeployment, InstrumentDeployment
from ooiservices.app.models import Stream, StreamParameter, Organization, Instrumentname
import json
import yaml
from wtforms import ValidationError


@api.route('/arrays')
def get_arrays():
    arrays = Array.query.all()
    return jsonify( {'arrays' : [array.to_json() for array in arrays] })

@api.route('/arrays/', methods=['POST'])
@auth.login_required
def set_array():
    array = Array.from_json(request.json)
    db.session.add(array)
    db.session.commit()
    return jsonify(array.to_json())

@api.route('/arrays/<string:id>')
def get_array(id):
    array = Array.query.filter_by(array_code=id).first_or_404()
    return jsonify(array.to_json())

@api.route('/platform_deployments')
def get_platform_deployments():
    if 'array_id' in request.args:
        platform_deployments = \
        PlatformDeployment.query.filter_by(array_id=request.args['array_id']).order_by(PlatformDeployment.reference_designator).all()
    else:
        platform_deployments = PlatformDeployment.query.all()
    return jsonify({ 'platform_deployments' : [platform_deployment.to_json() for platform_deployment in platform_deployments] })

@api.route('/platform_deployments/<string:id>')
def get_platform_deployment(id):
    platform_deployment = PlatformDeployment.query.filter_by(reference_designator=id).first_or_404()
    return jsonify(platform_deployment.to_json())

@api.route('/instrument_deployment', methods=['GET'])
def get_instrument_deployments():
    if 'platform_deployment_id' in request.args:
        instrument_deployments = \
        InstrumentDeployment.query.filter_by(platform_deployment_id=request.args['platform_deployment_id']).all()
        # TODO: Actually link the tables
        for i_d in instrument_deployments:
            instrument_name = Instrumentname.query.filter(Instrumentname.instrument_class == i_d.display_name).first()
            if instrument_name:
                i_d.display_name = instrument_name.display_name
    else:
        instrument_deployments = InstrumentDeployment.query.all()
    return jsonify({ 'instrument_deployments' : [instrument_deployment.to_json() for instrument_deployment in instrument_deployments] })

@api.route('/instrument_deployment/<int:id>', methods=['GET'])
def get_instrument_deployment(id):
    instrument_deployment = InstrumentDeployment.query.get(id)
    if instrument_deployment is None:
        return jsonify(error="Instrument Deployment Not Found"), 404
    return jsonify(**instrument_deployment.to_json())

@api.route('/instrument_deployment', methods=['POST'])
def post_instrument_deployment():
    try:
        new_deploy = InstrumentDeployment.from_json(data)
        db.session.add(new_deploy)
        db.session.commit()
    except ValidationError as e:
        return jsonify(error=e.message), 400
    return jsonify(**new_deploy.to_json()), 201

@api.route('/instrument_deployment/<int:id>', methods=['PUT'])
def put_instrument_deployment(id):
    try:
        data = json.loads(request.data)
        existingDeply = InstrumentDeployment.query.get(id)
        if existingDeply is None:
            return jsonify(error="Invalid ID, record not found"), 404
        existingDeply.display_name = data.get('display_name', existingDeply.display_name)
        existingDeply.start_date = data.get('start_date', existingDeply.start_date)
        existingDeply.end_date = data.get('end_date', existingDeply.end_date)
        existingDeply.platform_deployment_id = data.get('platform_deployment_id', existingDeply.platform_deployment_id)
        existingDeply.depth = data.get('depth', existingDeply.depth)
        existingDeply.geo_location = data.get('geo_location', existingDeply.geo_location)
        db.session.add(existingDeply)
        db.session.commit()
    except ValidationError as e:
        return jsonify(error=e.message), 400
    return jsonify(**existingDeply.to_json()), 200

@api.route('/instrument_deployment/<int:id>', methods=['DELETE'])
def delete_instrument_deployment(id):
    deployment = InstrumentDeployment.query.get(id)
    if deployment is None:
        return jsonify(error="Instrument Deployment Not Found"), 404
    db.session.delete(deployment)
    db.session.commit()
    return jsonify(), 200


@api.route('/streams')
def get_streams():
    streams = Stream.query.all()
    return jsonify({ 'streams' : [stream.to_json() for stream in streams] })

@api.route('/streams/<string:id>')
def get_stream(id):
    stream = Stream.query.filter_by(stream_name=id).first_or_404()
    return jsonify(stream.to_json())

@api.route('/parameters')
def get_parameters():
    parameters = StreamParameter.query.all()
    return jsonify({ 'parameters' : [parameter.to_json() for parameter in parameters] })

@api.route('/parameters/<string:id>')
def get_parameter(id):
    parameter = StreamParameter.query.filter_by(stream_parameter_name=id).first_or_404()
    return jsonify(parameter.to_json())

@api.route('/organization', methods=['GET'])
def get_organizations():
    organizations = [o.serialize() for o in Organization.query.all()]
    return jsonify(organizations=organizations)
{'fraction_non_alphanumeric': 0.12307564283931098, 'fraction_numerical': 0.007822251809935924, 'mean_word_length': 4.442934782608695, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4867599', 'n_tokens_mistral': 3790, 'n_tokens_neox': 3648, 'n_words': 692}