Changeset - 0096d4dce428
[Not reviewed]
default
0 1 0
Dennis Fink - 9 years ago 2016-09-19 17:41:03
dennis.fink@c3l.lu
Simplified open endpoint
1 file changed with 6 insertions and 6 deletions:
0 comments (0 inline, 0 general)
spaceapi/views.py
Show inline comments
 
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
 

	
 
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'))
 
            new_state = False
 
        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'))
 
            new_state = True
 

	
 
        active.set_new_state(value=new_state, 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')
0 comments (0 inline, 0 general)