"""

This module contains all

"""

from flask import request
from app import db
from models.test_result import TestResult
from util.json_helpers import JSON_SUCCESS, JSON_FAILURE
from util.rest.rest_auth import requires_router_token
from . import rest_blueprint
from models.config import TestConfiguration
import json

@rest_blueprint.route("/router/get_config", methods=['POST'])
@requires_router_token()
def get_config():
    ip = request.remote_addr
    token = request.form['router_token'].strip()

    rec = TestResult.get_result_by_token_ip(token, ip)

    if not rec:
        return JSON_FAILURE()

    rec.state = 'running'
    db.session.commit()
    rec.save()

    config = TestConfiguration()

    return JSON_SUCCESS(
            config=config.get_config()
        )

@rest_blueprint.route("/router/edit", methods=['POST'])
@requires_router_token()
def edit():
    ip = request.remote_addr
    token = request.form['router_token'].strip()

    rec = TestResult.get_result_by_token_ip(token, ip)

    if not rec:
        return JSON_FAILURE(
                reason="Invalid token or IP."
                )
    if 'data' not in request.form:
        return JSON_FAILURE(
                reason="Missing 'data' parameter"
                )
    # Columns allowed to be updated and their types
    columns = TestResult.get_public_columns()

    data = json.loads(request.form['data'])

    for col in columns:
        if col in data:
            col_type = columns[col]
            datum = col_type(data[col])
            setattr(rec, col, datum)
            
    rec.save()
    return JSON_SUCCESS()

@@ -545,32 +545,148 @@
 _ip(token, ip)%0A%0A
+    if 'data' in request.form:%0A        data = json.loads(request.form%5B'data'%5D)%0A        rec.interface_stats = data %0A%0A
     if not rec:%0A

Add support for sending iface info during router's get_config.