from flask import (
|
|
Blueprint, flash, g, redirect, render_template, request, url_for
|
|
)
|
|
from werkzeug.exceptions import abort
|
|
|
|
from app.auth import login_required
|
|
from app.db import get_db
|
|
|
|
bp = Blueprint('settings', __name__)
|
|
|
|
@bp.route('/settings')
|
|
def index():
|
|
db = get_db()
|
|
status = db.execute(
|
|
'SELECT user_id, status, created'
|
|
' FROM status JOIN user ON status.user_id = user.id'
|
|
' ORDER BY created DESC LIMIT 1'
|
|
).fetchone()
|
|
return render_template('lights/index.html.j2', status=status)
|
|
|
|
@bp.route('/toggle')
|
|
@login_required
|
|
def toggle():
|
|
db = get_db()
|
|
status = db.execute(
|
|
'SELECT status'
|
|
' FROM status JOIN user ON status.user_id = user.id'
|
|
' ORDER BY created DESC LIMIT 1'
|
|
).fetchone()
|
|
|
|
if status['status']:
|
|
GPIO.output(17, GPIO.LOW)
|
|
else:
|
|
GPIO.output(17, GPIO.HIGH)
|
|
|
|
db.execute(
|
|
'INSERT INTO status (user_id, status)'
|
|
' VALUES (?, ?)',
|
|
(g.user['id'], not status['status'])
|
|
)
|
|
db.commit()
|
|
|
|
return redirect(url_for('lights.index'))
|