From 9615a07e4988af3dbf2961f7dd09211cc3cecbaa Mon Sep 17 00:00:00 2001 From: esensar Date: Thu, 3 May 2018 09:48:24 +0200 Subject: [PATCH] Add account and role db models --- app/__init__.py | 4 ++- app/mod_accounts/__init__.py | 14 ++++++++++ app/mod_accounts/models.py | 35 ++++++++++++++++++++++++ app/mod_devices/__init__.py | 6 ++-- migrations/versions/5bdb53d5de37_.py | 41 ++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 app/mod_accounts/__init__.py create mode 100644 app/mod_accounts/models.py create mode 100644 migrations/versions/5bdb53d5de37_.py diff --git a/app/__init__.py b/app/__init__.py index e74bf07..5106072 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -9,9 +9,11 @@ db = SQLAlchemy(app) # Blueprints setup -from app.mod_devices import setup_mqtt, tear_down_mqtt, devices +from app.mod_devices import devices +from app.mod_accounts import accounts app.register_blueprint(devices, url_prefix='/devices') +app.register_blueprint(accounts, url_prefix='/accounts') @app.route("/") diff --git a/app/mod_accounts/__init__.py b/app/mod_accounts/__init__.py new file mode 100644 index 0000000..1dafe23 --- /dev/null +++ b/app/mod_accounts/__init__.py @@ -0,0 +1,14 @@ +from flask import Blueprint +from .. import db + +accounts = Blueprint('accounts', __name__) + +# Models +from .models import Account +from .models import Role + + +# Routes +@accounts.route("/") +def hello(): + return "Hello from accounts!" diff --git a/app/mod_accounts/models.py b/app/mod_accounts/models.py new file mode 100644 index 0000000..c24ea92 --- /dev/null +++ b/app/mod_accounts/models.py @@ -0,0 +1,35 @@ +from . import db + + +class Account(db.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]) + + def __init__(self, username, password, role): + self.username = str(username) + self.password = str(password) + if isinstance(role, Role): + self.role_id = role.id + else: + self.role_id = int(role) + + def __repr__(self): + return '' % self.username, self.role + + +class Role(db.Model): + __tablename__ = 'roles' + + id = db.Column(db.Integer, primary_key=True) + display_name = db.Column(db.String) + + def __init__(self, name): + self.display_name = str(name) + + def __repr__(self): + return '' % self.name diff --git a/app/mod_devices/__init__.py b/app/mod_devices/__init__.py index 7843e20..a3564ab 100644 --- a/app/mod_devices/__init__.py +++ b/app/mod_devices/__init__.py @@ -4,18 +4,21 @@ from .. import db devices = Blueprint('devices', __name__) -from .models import Recording # Models +from .models import Recording # Mqtt from .mqtt_client import tear_down_mqtt, setup_mqtt + # When app dies, stop mqtt connection def on_stop(): tear_down_mqtt() + atexit.register(on_stop) + # Routes @devices.route("/") def hello(): @@ -25,4 +28,3 @@ def hello(): @devices.record def on_blueprint_setup(setup_state): setup_mqtt(setup_state.app) - diff --git a/migrations/versions/5bdb53d5de37_.py b/migrations/versions/5bdb53d5de37_.py new file mode 100644 index 0000000..042291e --- /dev/null +++ b/migrations/versions/5bdb53d5de37_.py @@ -0,0 +1,41 @@ +"""empty message + +Revision ID: 5bdb53d5de37 +Revises: 56ec2b819bd8 +Create Date: 2018-05-03 09:48:10.275137 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '5bdb53d5de37' +down_revision = '56ec2b819bd8' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('roles', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('display_name', sa.String(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('accounts', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(), nullable=True), + sa.Column('password', sa.String(), nullable=True), + sa.Column('role_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['role_id'], ['roles.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('accounts') + op.drop_table('roles') + # ### end Alembic commands ###