<commit_before>"""

This module contains the basic CRUD functionality
for remotely managing test results.

Author: <NAME>
Date: 04/06/15

"""

from flask import request
from models.test_result import TestResult
from util.json_helpers import JSON_SUCCESS, JSON_FAILURE
from util.rest.rest_auth import requires_user_token
from . import rest_blueprint

@rest_blueprint.route('/test_result/<result_id>/edit', methods=['POST'])
@requires_user_token()
def edit_result(result_id=None):
    invalid_res = JSON_FAILURE(reason='Invalid Result')
    if not result_id: 
        return invalid_res

    result = TestResult.get_result_by_id(result_id)
    if not result:
        return invalid_res

    # Columns allowed to be updated and their types
    columns = TestResult.get_public_columns()

    for col in columns:
        if col in request.form:
            col_type = columns[col]
            datum = col_type(request.form[col])
            setattr(result, col, datum)
            
    result.save()

    return JSON_SUCCESS()

@rest_blueprint.route('/test_result/<result_id>/status', methods=['GET'])
@requires_user_token()
def result_status(result_id=None):
    invalid_res = JSON_FAILURE(reason='Invalid Result')
    if not result_id: 
        return invalid_res

    result = TestResult.get_result_by_id(result_id)
    if not result:
        return invalid_res
    return JSON_SUCCESS(
            state=result.state
            )

@rest_blueprint.route('/test_result/<result_id>', methods=['GET', 'POST'])
@requires_user_token()
def get_result(result_id=None):
    invalid_res = JSON_FAILURE(reason='Invalid Result')
    if not result_id: 
        return invalid_res

    result = TestResult.get_result_by_id(result_id)
    if not result:
        return invalid_res

    columns = TestResult.get_public_columns()

    return_vals = {}
    for col in columns:
        return_vals[col] = getattr(result, col)

    return JSON_SUCCESS(
            **return_vals
            )

<commit_msg>Set IP when mobile calls edit results.<commit_after>"""

This module contains the basic CRUD functionality
for remotely managing test results.

Author: <NAME>
Date: 04/06/15

"""

from flask import request
from models.test_result import TestResult
from util.json_helpers import JSON_SUCCESS, JSON_FAILURE
from util.rest.rest_auth import requires_user_token
from . import rest_blueprint

@rest_blueprint.route('/test_result/<result_id>/edit', methods=['POST'])
@requires_user_token()
def edit_result(result_id=None):
    invalid_res = JSON_FAILURE(reason='Invalid Result')
    if not result_id: 
        return invalid_res

    result = TestResult.get_result_by_id(result_id)
    result.device_ip = str(request.remote_addr)
    if not result:
        return invalid_res

    # Columns allowed to be updated and their types
    columns = TestResult.get_public_columns()

    for col in columns:
        if col in request.form:
            col_type = columns[col]
            datum = col_type(request.form[col])
            setattr(result, col, datum)
            
    result.save()

    return JSON_SUCCESS()

@rest_blueprint.route('/test_result/<result_id>/status', methods=['GET'])
@requires_user_token()
def result_status(result_id=None):
    invalid_res = JSON_FAILURE(reason='Invalid Result')
    if not result_id: 
        return invalid_res

    result = TestResult.get_result_by_id(result_id)
    if not result:
        return invalid_res
    return JSON_SUCCESS(
            state=result.state
            )

@rest_blueprint.route('/test_result/<result_id>', methods=['GET', 'POST'])
@requires_user_token()
def get_result(result_id=None):
    invalid_res = JSON_FAILURE(reason='Invalid Result')
    if not result_id: 
        return invalid_res

    result = TestResult.get_result_by_id(result_id)
    if not result:
        return invalid_res

    columns = TestResult.get_public_columns()

    return_vals = {}
    for col in columns:
        return_vals[col] = getattr(result, col)

    return JSON_SUCCESS(
            **return_vals
            )


{'fraction_non_alphanumeric': 0.09048207663782447, 'fraction_numerical': 0.0029666254635352285, 'mean_word_length': 3.345864661654135, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6932972', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1209, 'n_words': 292}