# Copyright (C) 2016 Antoine Carme <Antoine.Carme@Laposte.net>
# All rights reserved.

# This file is part of the Python Automatic Forecasting (PyAF) library and is made available under
# the terms of the 3 Clause BSD license

from __future__ import absolute_import

from flask import Flask, jsonify, request #import objects from the Flask model

# from .
import WS_Backend as be;

app = Flask(__name__) #define app using Flask
backends = {};

@app.route('/', methods=['GET'])
def test():
    return jsonify({'message' : 'It works!'})

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

def get_backend():
    backend = backends.get(request.remote_addr)
    if(backend is None):
        backend = be.cFlaskBackend();
        backends[request.remote_addr] = backend;
    return backend;



# MODELS

def jsonify_models():
    backend = get_backend();
    dict1 = backend.models
    return jsonify({'models' : [{k : v.as_dict()} for k,v in dict1.items()]})    


# GET requests


@app.route('/models', methods=['GET'])
def returnAllModels():
    backend = get_backend();
    return jsonify_models();

@app.route('/model/<string:name>', methods=['GET'])
def returnOneModel(name):
    backend = get_backend();
    model = backend.get_model(name);
    if(model):
        return jsonify({'model' : model.as_dict()})
    return jsonify({})

@app.route('/model/<string:name>/plot/<string:plot_type>', methods=['GET'])
def returnOneModelPlot(name, plot_type):
    backend = get_backend();
    model = backend.get_model(name);
    if(model):
        if(plot_type != "all"):
            lPlot_PNG_Base64 = model.mPlots[plot_type];
            lOutput = "<img src=\"data:image/png;base64," + str(lPlot_PNG_Base64) + "\"\>";
        else:
            lOutput = "";
            for k in model.mPlots.keys():
                lPlot_PNG_Base64 = model.mPlots[k];
                lOutput = lOutput + "<img src=\"data:image/png;base64," + str(lPlot_PNG_Base64) + "\"\>\n\n\n";            
        return lOutput;
    return jsonify({})

@app.route('/model/<string:name>/SQL/<string:sql_dialect>', methods=['GET'])
def returnOneModelSQL(name, sql_dialect):
    backend = get_backend();
    model = backend.get_model(name);
    if(model):
        lSQL = model.mSQL[sql_dialect];
        return lSQL
    return jsonify({})

# POST requests

@app.route('/model', methods=['POST'])
def addOneModel():
    backend = get_backend();
    print("JSON : " , request.json);
    print("JSON : " , request.__dict__);
    backend.add_model(request.json);
    return jsonify_models();

# PUT requests 


@app.route('/model/<string:name>', methods=['PUT'])
def editOneModel(name):
    backend = get_backend();
    backend.update_model(name , request.json['name']);
    model = backend.get_model(name);
    if(model):
        return jsonify({'model' : model.as_dict()})
    return jsonify({})

# DELETE requests

@app.route('/model/<string:name>', methods=['DELETE'])
def removeOneModel(name):
    backend = get_backend();
    backend.remove_model(name);
    return jsonify_models();

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)

@@ -3130,16 +3130,62 @@
 ain__':%0A
+    port = int(os.environ.get(%22PORT%22, 33507))%0A
     app.
@@ -3213,10 +3213,12 @@
 ort=
-80
+port
 , de

Update WS_REST_Flask.py