From e188141a2a849d15f02ca0bde48eb338c17a5646 Mon Sep 17 00:00:00 2001 From: esensar Date: Thu, 3 May 2018 14:23:24 +0200 Subject: [PATCH] Clean up imports and bad code --- app/__init__.py | 13 ++++++++----- app/mod_accounts/__init__.py | 5 ----- app/mod_accounts/models.py | 21 +++++++++++---------- app/mod_devices/__init__.py | 8 +------- app/mod_devices/models.py | 22 +++++++++++----------- app/mod_devices/mqtt_client.py | 2 +- manage.py | 2 -- 7 files changed, 32 insertions(+), 41 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 5106072..421f935 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -8,12 +8,15 @@ app.config.from_pyfile('config.py') db = SQLAlchemy(app) -# Blueprints setup -from app.mod_devices import devices -from app.mod_accounts import accounts +def setup_blueprints(app): + from .mod_devices import devices + from .mod_accounts import accounts -app.register_blueprint(devices, url_prefix='/devices') -app.register_blueprint(accounts, url_prefix='/accounts') + app.register_blueprint(devices, url_prefix='/devices') + app.register_blueprint(accounts, url_prefix='/accounts') + + +setup_blueprints(app) @app.route("/") diff --git a/app/mod_accounts/__init__.py b/app/mod_accounts/__init__.py index 1dafe23..1e4275b 100644 --- a/app/mod_accounts/__init__.py +++ b/app/mod_accounts/__init__.py @@ -1,12 +1,7 @@ from flask import Blueprint -from .. import db accounts = Blueprint('accounts', __name__) -# Models -from .models import Account -from .models import Role - # Routes @accounts.route("/") diff --git a/app/mod_accounts/models.py b/app/mod_accounts/models.py index c24ea92..80582bc 100644 --- a/app/mod_accounts/models.py +++ b/app/mod_accounts/models.py @@ -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' - id = db.Column(db.Integer, primary_key=True) - username = db.Column(db.String) - password = db.Column(db.String) - role_id = db.Column(db.Integer, db.ForeignKey("roles.id")) - role = db.relationship("Role", foreign_keys=[role_id]) + id = Column(Integer, primary_key=True) + username = Column(String) + password = Column(String) + role_id = Column(Integer, ForeignKey("roles.id")) + role = relationship("Role", foreign_keys=[role_id]) def __init__(self, username, password, role): self.username = str(username) @@ -22,11 +23,11 @@ class Account(db.Model): return '' % self.username, self.role -class Role(db.Model): +class Role(Model): __tablename__ = 'roles' - id = db.Column(db.Integer, primary_key=True) - display_name = db.Column(db.String) + id = Column(Integer, primary_key=True) + display_name = Column(String) def __init__(self, name): self.display_name = str(name) diff --git a/app/mod_devices/__init__.py b/app/mod_devices/__init__.py index a3564ab..b7f6d07 100644 --- a/app/mod_devices/__init__.py +++ b/app/mod_devices/__init__.py @@ -1,15 +1,9 @@ import atexit from flask import Blueprint -from .. import db +from .mqtt_client import tear_down_mqtt, setup_mqtt 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 def on_stop(): diff --git a/app/mod_devices/models.py b/app/mod_devices/models.py index 572c46e..ee79701 100644 --- a/app/mod_devices/models.py +++ b/app/mod_devices/models.py @@ -1,20 +1,20 @@ from datetime import datetime -from . import db +from sqlalchemy import Model, DateTime, String, Column, Integer from sqlalchemy.dialects.postgresql import JSON -class Recording(db.Model): +class Recording(Model): __tablename__ = 'recordings' - id = db.Column(db.Integer, primary_key=True) - recorded_at = db.Column(db.DateTime, index=True, - default=datetime.utcnow()) - received_at = db.Column(db.DateTime, index=True, - default=datetime.utcnow()) - device_id = db.Column(db.Integer) - record_type = db.Column(db.Integer, nullable=False) - record_value = db.Column(db.String, nullable=False) - raw_record = db.Column(JSON, nullable=True) + id = Column(Integer, primary_key=True) + recorded_at = Column(DateTime, index=True, + default=datetime.utcnow()) + received_at = Column(DateTime, index=True, + default=datetime.utcnow()) + device_id = Column(Integer) + record_type = Column(Integer, nullable=False) + record_value = Column(String, nullable=False) + raw_record = Column(JSON, nullable=True) def __init__(self, device_id, record_type, record_value, recorded_at, raw_json): diff --git a/app/mod_devices/mqtt_client.py b/app/mod_devices/mqtt_client.py index 134e77d..e3ee203 100644 --- a/app/mod_devices/mqtt_client.py +++ b/app/mod_devices/mqtt_client.py @@ -2,7 +2,6 @@ import sys import json from flask_mqtt import Mqtt from .models import Recording -from . import db mqtt = Mqtt() @@ -38,6 +37,7 @@ def handle_subscribe(client, userdata, mid, granted_qos): def handle_mqtt_message(client, userdata, message): + from .. import db print("Received message!") print("Topic: " + message.topic) print("Payload: " + message.payload.decode()) diff --git a/manage.py b/manage.py index 7508b88..b3f4d2e 100644 --- a/manage.py +++ b/manage.py @@ -1,7 +1,5 @@ -import os from flask_script import Manager from flask_migrate import Migrate, MigrateCommand - from app import app, db