Clean up imports and bad code

master
esensar 2018-05-03 14:23:24 +02:00
parent 9615a07e49
commit e188141a2a
7 changed files with 32 additions and 41 deletions

View File

@ -8,12 +8,15 @@ app.config.from_pyfile('config.py')
db = SQLAlchemy(app) db = SQLAlchemy(app)
# Blueprints setup def setup_blueprints(app):
from app.mod_devices import devices from .mod_devices import devices
from app.mod_accounts import accounts from .mod_accounts import accounts
app.register_blueprint(devices, url_prefix='/devices') app.register_blueprint(devices, url_prefix='/devices')
app.register_blueprint(accounts, url_prefix='/accounts') app.register_blueprint(accounts, url_prefix='/accounts')
setup_blueprints(app)
@app.route("/") @app.route("/")

View File

@ -1,12 +1,7 @@
from flask import Blueprint from flask import Blueprint
from .. import db
accounts = Blueprint('accounts', __name__) accounts = Blueprint('accounts', __name__)
# Models
from .models import Account
from .models import Role
# Routes # Routes
@accounts.route("/") @accounts.route("/")

View File

@ -1,14 +1,15 @@
from . import db from sqlalchemy import (Model, Column, String,
Integer, ForeignKey, relationship)
class Account(db.Model): class Account(Model):
__tablename__ = 'accounts' __tablename__ = 'accounts'
id = db.Column(db.Integer, primary_key=True) id = Column(Integer, primary_key=True)
username = db.Column(db.String) username = Column(String)
password = db.Column(db.String) password = Column(String)
role_id = db.Column(db.Integer, db.ForeignKey("roles.id")) role_id = Column(Integer, ForeignKey("roles.id"))
role = db.relationship("Role", foreign_keys=[role_id]) role = relationship("Role", foreign_keys=[role_id])
def __init__(self, username, password, role): def __init__(self, username, password, role):
self.username = str(username) self.username = str(username)
@ -22,11 +23,11 @@ class Account(db.Model):
return '<Account (name=%s, role=%s)>' % self.username, self.role return '<Account (name=%s, role=%s)>' % self.username, self.role
class Role(db.Model): class Role(Model):
__tablename__ = 'roles' __tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True) id = Column(Integer, primary_key=True)
display_name = db.Column(db.String) display_name = Column(String)
def __init__(self, name): def __init__(self, name):
self.display_name = str(name) self.display_name = str(name)

View File

@ -1,15 +1,9 @@
import atexit import atexit
from flask import Blueprint from flask import Blueprint
from .. import db from .mqtt_client import tear_down_mqtt, setup_mqtt
devices = Blueprint('devices', __name__) devices = Blueprint('devices', __name__)
# Models
from .models import Recording
# Mqtt
from .mqtt_client import tear_down_mqtt, setup_mqtt
# When app dies, stop mqtt connection # When app dies, stop mqtt connection
def on_stop(): def on_stop():

View File

@ -1,20 +1,20 @@
from datetime import datetime from datetime import datetime
from . import db from sqlalchemy import Model, DateTime, String, Column, Integer
from sqlalchemy.dialects.postgresql import JSON from sqlalchemy.dialects.postgresql import JSON
class Recording(db.Model): class Recording(Model):
__tablename__ = 'recordings' __tablename__ = 'recordings'
id = db.Column(db.Integer, primary_key=True) id = Column(Integer, primary_key=True)
recorded_at = db.Column(db.DateTime, index=True, recorded_at = Column(DateTime, index=True,
default=datetime.utcnow()) default=datetime.utcnow())
received_at = db.Column(db.DateTime, index=True, received_at = Column(DateTime, index=True,
default=datetime.utcnow()) default=datetime.utcnow())
device_id = db.Column(db.Integer) device_id = Column(Integer)
record_type = db.Column(db.Integer, nullable=False) record_type = Column(Integer, nullable=False)
record_value = db.Column(db.String, nullable=False) record_value = Column(String, nullable=False)
raw_record = db.Column(JSON, nullable=True) raw_record = Column(JSON, nullable=True)
def __init__(self, device_id, record_type, def __init__(self, device_id, record_type,
record_value, recorded_at, raw_json): record_value, recorded_at, raw_json):

View File

@ -2,7 +2,6 @@ import sys
import json import json
from flask_mqtt import Mqtt from flask_mqtt import Mqtt
from .models import Recording from .models import Recording
from . import db
mqtt = Mqtt() mqtt = Mqtt()
@ -38,6 +37,7 @@ def handle_subscribe(client, userdata, mid, granted_qos):
def handle_mqtt_message(client, userdata, message): def handle_mqtt_message(client, userdata, message):
from .. import db
print("Received message!") print("Received message!")
print("Topic: " + message.topic) print("Topic: " + message.topic)
print("Payload: " + message.payload.decode()) print("Payload: " + message.payload.decode())

View File

@ -1,7 +1,5 @@
import os
from flask_script import Manager from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand from flask_migrate import Migrate, MigrateCommand
from app import app, db from app import app, db