project.server.api API documentation

Module project.server.api

Expand source code
import json
from dataclasses import asdict
from dacite import from_dict
import logging
from bson.objectid import ObjectId

from flask import jsonify, Blueprint, request, render_template
from flask import current_app as app

import project.server.shared as shared
from project.server.models import User
from project.server.models import Test
from project.server.models import Models, Fit
from project.server.tasks.analysis import _make_report

import project.server.tasks.plot as plot

main_blueprint = Blueprint('api', __name__)


@main_blueprint.route('/api/get_material', methods=['GET'])
def get_material():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified material object, list of material IDs or None
    """
    query = dict(request.args)
    collection_name = 'material_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                          query=query,
                                          )
    return jsonify(response)


@main_blueprint.route('/api/new_material', methods=['POST'])
def new_material():
    """Adds a material. If not unique, returns an existing material

    Returns:
        [str] -- The id of the material  
        [bool] -- If the material was added or not
    """
    material = json.loads(request.data)['material']
    _id, unique = shared.new_material(material)
    return jsonify(str(_id), unique)


@main_blueprint.route('/api/delete_material', methods=['POST'])
def _delete_material():
    """Deletes a material by id

    Returns:
        [bool] -- If the material was deleted or not
    """
    material_id = json.loads(request.data)['material_id']
    response = shared.delete_material(material_id)
    return jsonify(response)


@main_blueprint.route('/api/new_test', methods=['POST'])
def new_test():
    """Adds a new test

    Returns:
        [str] -- The action of the new test i.e. new, update or duplicate  
        [str] -- The ID of the test  
        [dict] -- The chosen models of the test fit  
        [str] -- The ID of the fit
    """
    test = json.loads(request.data)['test']
    dstr = json.loads(request.data)['dstr']

    darray = shared.split_dstr(dstr=dstr)

    file_input, data = shared.read_darray(darray=darray)

    test['data'] = data

    if not 'thickness' in test['input']:
        test['input']['thickness'] = file_input['thickness']
    test['input']['volume'] = file_input['volume']

    response = shared.new_test_and_fit(test)
    
    return jsonify(response)


@main_blueprint.route('/api/get_test', methods=['GET'])
def get_test():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified test object, list of test IDs or None
    """
    
    query = dict(request.args)
    if "internal_name" in query:
        query["material"] = str(shared.material_by_query({"internal_name": query["internal_name"]})["_id"])
        del query["internal_name"]

    collection_name = 'test_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                          query=query,
                                          )
    return jsonify(response)


@main_blueprint.route('/api/delete_test', methods=['POST'])
def _delete_test():
    """Deletes a test by id

    Returns:
        [bool] -- If the test was deleted or not
    """
    test_id = json.loads(request.data)['test_id']
    response = shared.delete_test(test_id)
    return jsonify(response)


@main_blueprint.route('/api/add_datastr', methods=['POST'])
def _add_datastr():
    dstr = json.loads(request.data)['dstr']
    test_id = json.loads(request.data)['test_id']
    
    darray = shared.split_dstr(dstr=dstr)

    input, data = shared.read_darray(darray=darray)

    shared.add_data(test_id, data)
    return jsonify('data string added')


@main_blueprint.route('/api/new_fit', methods=['POST'])
def new_fit():
    """Adds a new fit to the chosen test

    Returns:
        [str] -- The ID of the fit  
        [bool] -- If a new fit was added or not  
        [dict] - The models of the fit
    """
    _models = json.loads(request.data)['models']
    test_id = json.loads(request.data)['test_id']

    models = Models(diffusion=_models['diffusion'],
                    material=_models['material'],
                    gas=_models['gas'])

    fit_id, unique, models = shared.new_fit(test_id, models)
    
    return jsonify(str(fit_id), unique, models)


@main_blueprint.route('/api/get_fit', methods=['GET'])
def get_fit():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified fit object, list of fit IDs or None
    """
    
    query = dict(request.args)
    collection_name = 'fit_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                               query=query,
                                               )
    return jsonify(response)


@main_blueprint.route('/api/view_fit', methods=['GET'])
def view_fit():
    """
    Generates a view of the fit on the data
    
    Returns:
        [str] -- A HTML string showing the fit
    """
    fit_id = json.loads(request.data)['fit_id']

    _fit = shared.get_fit(fit_id=fit_id)
    fit = from_dict(data_class=Fit,
                    data=_fit)

    _test = shared.get_test(test_id=fit.test_id)
    test = from_dict(data_class=Test,
                     data=_test)

    script, div = plot.fit(test=test,
                           fit=fit,
                           )

    return jsonify(render_template('test/view_fit.html',
                                   test_id=fit.test_id,
                                   test=test,
                                   script=script,
                                   div=div,
                                   ))

@main_blueprint.route('/api/delete_fit', methods=['POST'])
def _delete_fit():
    """Deletes a fit by id

    Returns:
        [bool] -- If the fit was deleted or not
    """
    fit_id = json.loads(request.data)['fit_id']
    response = shared.delete_fit(fit_id)
    return jsonify(response)


@main_blueprint.route('/api/_generate_arrhenius_prediction', methods=['POST'])
def _generate_arrhenius_prediction():
    data = json.loads(request.data)
    prediction = _make_report(data=data)
    return jsonify(prediction)


@main_blueprint.route('/api/get_report', methods=['GET'])
def get_report():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified report object, list of report IDs or None
    """
    query = dict(request.args)
    if "internal_name" in query:
        query["material"] = str(shared.material_by_query({"internal_name": query["internal_name"]})["_id"])
        del query["internal_name"]
    collection_name = 'report_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                               query=query,
                                               )
    return jsonify(response)


@main_blueprint.route('/api/get_version', methods=['GET'])
def _get_version():
    version = {'version': app.version}
    return jsonify(version)

Functions

def get_fit()

Gets all documents matching any of the arguments

Returns

[dict] – Jsonified fit object, list of fit IDs or None

Expand source code
@main_blueprint.route('/api/get_fit', methods=['GET'])
def get_fit():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified fit object, list of fit IDs or None
    """
    
    query = dict(request.args)
    collection_name = 'fit_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                               query=query,
                                               )
    return jsonify(response)
def get_material()

Gets all documents matching any of the arguments

Returns

[dict] – Jsonified material object, list of material IDs or None

Expand source code
@main_blueprint.route('/api/get_material', methods=['GET'])
def get_material():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified material object, list of material IDs or None
    """
    query = dict(request.args)
    collection_name = 'material_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                          query=query,
                                          )
    return jsonify(response)
def get_report()

Gets all documents matching any of the arguments

Returns

[dict] – Jsonified report object, list of report IDs or None

Expand source code
@main_blueprint.route('/api/get_report', methods=['GET'])
def get_report():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified report object, list of report IDs or None
    """
    query = dict(request.args)
    if "internal_name" in query:
        query["material"] = str(shared.material_by_query({"internal_name": query["internal_name"]})["_id"])
        del query["internal_name"]
    collection_name = 'report_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                               query=query,
                                               )
    return jsonify(response)
def get_test()

Gets all documents matching any of the arguments

Returns

[dict] – Jsonified test object, list of test IDs or None

Expand source code
@main_blueprint.route('/api/get_test', methods=['GET'])
def get_test():
    """
    Gets all documents matching any of the arguments
    
    Returns:
        [dict] -- Jsonified test object, list of test IDs or None
    """
    
    query = dict(request.args)
    if "internal_name" in query:
        query["material"] = str(shared.material_by_query({"internal_name": query["internal_name"]})["_id"])
        del query["internal_name"]

    collection_name = 'test_collection'
    response = shared.get_document_or_ids(collection_name=collection_name,
                                          query=query,
                                          )
    return jsonify(response)
def new_fit()

Adds a new fit to the chosen test

Returns

[str] – The ID of the fit
[bool] – If a new fit was added or not
[dict] - The models of the fit

Expand source code
@main_blueprint.route('/api/new_fit', methods=['POST'])
def new_fit():
    """Adds a new fit to the chosen test

    Returns:
        [str] -- The ID of the fit  
        [bool] -- If a new fit was added or not  
        [dict] - The models of the fit
    """
    _models = json.loads(request.data)['models']
    test_id = json.loads(request.data)['test_id']

    models = Models(diffusion=_models['diffusion'],
                    material=_models['material'],
                    gas=_models['gas'])

    fit_id, unique, models = shared.new_fit(test_id, models)
    
    return jsonify(str(fit_id), unique, models)
def new_material()

Adds a material. If not unique, returns an existing material

Returns

[str] – The id of the material
[bool] – If the material was added or not

Expand source code
@main_blueprint.route('/api/new_material', methods=['POST'])
def new_material():
    """Adds a material. If not unique, returns an existing material

    Returns:
        [str] -- The id of the material  
        [bool] -- If the material was added or not
    """
    material = json.loads(request.data)['material']
    _id, unique = shared.new_material(material)
    return jsonify(str(_id), unique)
def new_test()

Adds a new test

Returns

[str] – The action of the new test i.e. new, update or duplicate
[str] – The ID of the test
[dict] – The chosen models of the test fit
[str] – The ID of the fit

Expand source code
@main_blueprint.route('/api/new_test', methods=['POST'])
def new_test():
    """Adds a new test

    Returns:
        [str] -- The action of the new test i.e. new, update or duplicate  
        [str] -- The ID of the test  
        [dict] -- The chosen models of the test fit  
        [str] -- The ID of the fit
    """
    test = json.loads(request.data)['test']
    dstr = json.loads(request.data)['dstr']

    darray = shared.split_dstr(dstr=dstr)

    file_input, data = shared.read_darray(darray=darray)

    test['data'] = data

    if not 'thickness' in test['input']:
        test['input']['thickness'] = file_input['thickness']
    test['input']['volume'] = file_input['volume']

    response = shared.new_test_and_fit(test)
    
    return jsonify(response)
def view_fit()

Generates a view of the fit on the data

Returns

[str] – A HTML string showing the fit

Expand source code
@main_blueprint.route('/api/view_fit', methods=['GET'])
def view_fit():
    """
    Generates a view of the fit on the data
    
    Returns:
        [str] -- A HTML string showing the fit
    """
    fit_id = json.loads(request.data)['fit_id']

    _fit = shared.get_fit(fit_id=fit_id)
    fit = from_dict(data_class=Fit,
                    data=_fit)

    _test = shared.get_test(test_id=fit.test_id)
    test = from_dict(data_class=Test,
                     data=_test)

    script, div = plot.fit(test=test,
                           fit=fit,
                           )

    return jsonify(render_template('test/view_fit.html',
                                   test_id=fit.test_id,
                                   test=test,
                                   script=script,
                                   div=div,
                                   ))