diff --git a/interruptor/__init__.py b/app/__init__.py similarity index 76% rename from interruptor/__init__.py rename to app/__init__.py index 70c2662..148e4dd 100644 --- a/interruptor/__init__.py +++ b/app/__init__.py @@ -7,7 +7,7 @@ def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', - DATABASE=os.path.join(app.instance_path, 'interruptor.sqlite'), + DATABASE=os.path.join(app.instance_path, 'control.sqlite'), ) if test_config is None: @@ -24,9 +24,9 @@ def create_app(test_config=None): pass # setup GPIO - import RPi.GPIO as GPIO - GPIO.setmode(GPIO.BCM) - GPIO.setup(17, GPIO.OUT) + # import RPi.GPIO as GPIO + # GPIO.setmode(GPIO.BCM) + # GPIO.setup(17, GPIO.OUT) from . import db db.init_app(app) @@ -34,8 +34,8 @@ def create_app(test_config=None): from . import auth app.register_blueprint(auth.bp) - from . import interruptor - app.register_blueprint(interruptor.bp) + from . import lights + app.register_blueprint(lights.bp) app.add_url_rule('/', endpoint='index') return app diff --git a/interruptor/auth.py b/app/auth.py similarity index 98% rename from interruptor/auth.py rename to app/auth.py index 07a6104..8c0dc4b 100644 --- a/interruptor/auth.py +++ b/app/auth.py @@ -5,7 +5,7 @@ from flask import ( ) from werkzeug.security import check_password_hash, generate_password_hash -from interruptor.db import get_db +from app.db import get_db bp = Blueprint('auth', __name__, url_prefix='/auth') diff --git a/interruptor/db.py b/app/db.py similarity index 100% rename from interruptor/db.py rename to app/db.py diff --git a/app/lights.py b/app/lights.py new file mode 100644 index 0000000..45a2ae7 --- /dev/null +++ b/app/lights.py @@ -0,0 +1,45 @@ +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 + +# import RPi.GPIO as GPIO + +bp = Blueprint('lights', __name__) + +@bp.route('/') +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')) diff --git a/interruptor/schema.sql b/app/schema.sql similarity index 55% rename from interruptor/schema.sql rename to app/schema.sql index 97c0d83..a2ba560 100644 --- a/interruptor/schema.sql +++ b/app/schema.sql @@ -15,4 +15,13 @@ CREATE TABLE status ( FOREIGN KEY (user_id) REFERENCES user (id) ); -INSERT INTO STATUS (user_id, status) VALUES (1, 1); +INSERT INTO status (user_id, status) VALUES (1, 1); + +CREATE TABLE settings ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT UNIQUE NOT NULL, + value TEXT UNIQUE NOT NULL +); + +INSERT INTO settings (name, value) VALUES ('GPIO pin', '17'); +INSERT INTO settings (name, value) VALUES ('Registration', 'enabled'); diff --git a/interruptor/interruptor.py b/app/settings.py similarity index 73% rename from interruptor/interruptor.py rename to app/settings.py index 751cd88..4e6d23a 100644 --- a/interruptor/interruptor.py +++ b/app/settings.py @@ -3,14 +3,12 @@ from flask import ( ) from werkzeug.exceptions import abort -from interruptor.auth import login_required -from interruptor.db import get_db +from app.auth import login_required +from app.db import get_db -import RPi.GPIO as GPIO +bp = Blueprint('settings', __name__) -bp = Blueprint('interruptor', __name__) - -@bp.route('/') +@bp.route('/settings') def index(): db = get_db() status = db.execute( @@ -18,7 +16,7 @@ def index(): ' FROM status JOIN user ON status.user_id = user.id' ' ORDER BY created DESC LIMIT 1' ).fetchone() - return render_template('interruptor/index.html.j2', status=status) + return render_template('lights/index.html.j2', status=status) @bp.route('/toggle') @login_required @@ -42,4 +40,4 @@ def toggle(): ) db.commit() - return redirect(url_for('interruptor.index')) + return redirect(url_for('lights.index')) diff --git a/interruptor/static/style.css b/app/static/style.css similarity index 100% rename from interruptor/static/style.css rename to app/static/style.css diff --git a/interruptor/templates/auth/login.html.j2 b/app/templates/auth/login.html.j2 similarity index 100% rename from interruptor/templates/auth/login.html.j2 rename to app/templates/auth/login.html.j2 diff --git a/interruptor/templates/auth/register.html.j2 b/app/templates/auth/register.html.j2 similarity index 100% rename from interruptor/templates/auth/register.html.j2 rename to app/templates/auth/register.html.j2 diff --git a/interruptor/templates/base.html.j2 b/app/templates/base.html.j2 similarity index 89% rename from interruptor/templates/base.html.j2 rename to app/templates/base.html.j2 index 259b0eb..15ba3b7 100644 --- a/interruptor/templates/base.html.j2 +++ b/app/templates/base.html.j2 @@ -1,9 +1,9 @@ -{% block title %}{% endblock %} - Interruptor +{% block title %}{% endblock %} - Espertocasa