Files @ 1963e193d7ca
Branch filter:

Location: C3L-NOC/spaceapi/spaceapi/views.py

Dennis Fink
Move state set into ActiveStatus
from flask import Blueprint, jsonify, render_template, abort, request, redirect, url_for, current_app

from .utils import request_wants_json, ActiveStatus
from .auth import httpauth
from .state import set_new_state

root_views = Blueprint('root', __name__)


@root_views.route('/')
def index():
    if request_wants_json():
        return jsonify(ActiveStatus())
    return render_template('index.html', status=ActiveStatus())


@root_views.route('/status.json')
def status_json():
    return jsonify(ActiveStatus())


@root_views.route('/reload')
@httpauth.login_required
def reload():
    active = ActiveStatus()
    active.reload()
    return jsonify(active)


@root_views.route('/open', methods=('GET', 'POST'))
@httpauth.login_required
def open():
    if request.method == 'POST':
        active = ActiveStatus()

        try:
            if httpauth.username() in current_app.config['STATE_TRIGGER_PERSON_ALLOWED']:
                trigger_person = httpauth.username()
            else:
                trigger_person = None
        except KeyError:
            trigger_person = None

        if 'close' in request.form:
            active.set_new_state(value=False, trigger_person=trigger_person)
            active.save_last_state()
            return redirect(url_for('root.index'))
        elif 'open' in request.form:
            active.set_new_state(value=True, trigger_person=trigger_person)
            active.save_last_state()
            return redirect(url_for('root.index'))

    return render_template('open.html')


@root_views.route('/present', methods=('GET', 'POST'))
@httpauth.login_required
def present():
    if request.method == 'POST':

        active = ActiveStatus()

        if active['state']['open']:
            if 'present' in request.form:
                active.add_user_present(httpauth.username())
            elif 'leave' in request.form:
                active.remove_user_present(httpauth.username())
            active.save_last_state()
        return redirect(url_for('root.index'))

    return render_template('present.html')