Changeset - 411cab5fae51
[Not reviewed]
default
0 2 0
Dennis Fink - 9 years ago 2016-10-25 19:39:18
dennis.fink@c3l.lu
Use pass_active_status
2 files changed with 6 insertions and 10 deletions:
0 comments (0 inline, 0 general)
spaceapi/sensors.py
Show inline comments
 
import json
 

	
 
from functools import partial
 

	
 
from pkg_resources import resource_filename
 

	
 
import jsonschema
 
from flask import Blueprint, jsonify, request, abort, current_app
 

	
 
from .auth import httpauth
 
from .utils import first, fuzzy_list_find, ActiveStatus
 
from .utils import first, fuzzy_list_find, pass_active_status
 

	
 
sensors_views = Blueprint('sensors', __name__)
 

	
 
ALLOWED_SENSORS_KEYS = json.load(
 
    open(resource_filename('spaceapi', 'schema/sensors.json'),
 
         encoding='utf-8')
 
)
 

	
 
RADIATON_SUBKEYS = frozenset(('alpha', 'beta', 'gamma', 'beta_gamma'))
 

	
 
get_identification_key = partial(first, keys=frozenset(('name', 'location')))
 

	
 
@@ -29,25 +29,24 @@ def set_value(data, key):
 
    try:
 
        subkey = get_identification_key(data)
 
    except ValueError:
 
        return abort(400)
 

	
 
    try:
 
        index = fuzzy_list_find(active['sensors'][key],
 
                                subkey, data[subkey])
 
        active['sensors'][key][index].update(data)
 
    except ValueError:
 
        active['sensors'][key].append(data)
 

	
 
    active.save_last_state()
 
    return jsonify(active)
 

	
 

	
 
def set_radiation_value(data):
 

	
 
    active = ActiveStatus()
 

	
 
    radiation_keys = [k for k in RADIATON_SUBKEYS if k in data]
 

	
 
    if not radiation_keys:
 
        return abort(400)
 

	
 
@@ -59,33 +58,31 @@ def set_radiation_value(data):
 
            return abort(400)
 

	
 
        try:
 
            index = fuzzy_list_find(
 
                active['sensors']['radiation'][first_subkey],
 
                second_subkey,
 
                data[first_subkey][second_subkey])
 
            active['sensors']['radiation'][first_subkey][index].update(
 
                data)
 
        except ValueError:
 
            active['sensors']['radiaton'][first_subkey].append(data)
 

	
 
    active.save_last_state()
 
    return jsonify(active)
 

	
 

	
 
@sensors_views.route('/set/<key>', methods=['POST'])
 
@httpauth.login_required
 
def set_sensors(key):
 

	
 
    active = ActiveStatus()
 
@pass_active_status
 
def set_sensors(active, key):
 

	
 
    if key in ALLOWED_SENSORS_KEYS and key in active['sensors']:
 
        data = request.data.decode('utf-8')
 
        try:
 
            data = json.loads(data)
 

	
 
            try:
 
                jsonschema.validate(data, ALLOWED_SENSORS_KEYS[key])
 
            except jsonschema.ValidationError:
 
                current_app.logger.info('Validation Error')
 
                return abort(400)
 

	
spaceapi/state.py
Show inline comments
 
import json
 

	
 
from time import time
 

	
 
from flask import Blueprint, jsonify, request, current_app, abort
 

	
 
from .utils import ActiveStatus
 
from .utils import pass_active_status
 
from .auth import httpauth
 

	
 
state_views = Blueprint('state', __name__)
 

	
 

	
 
@state_views.route('/set/<key>', methods=['POST'])
 
@httpauth.login_required
 
def set_state(key):
 
    active = ActiveStatus()
 
@pass_active_status
 
def set_state(active, key):
 
    value = json.loads(request.data.decode('utf-8'))['value']
 
    current_app.logger.info(value)
 
    current_app.logger.info(type(value))
 
    active.set_new_state(**{key: value})
 
    active.save_last_state()
 
    return jsonify(active)
0 comments (0 inline, 0 general)