Add account and role db models

master
esensar 2018-05-03 09:48:24 +02:00
parent 1dea242bce
commit 9615a07e49
5 changed files with 97 additions and 3 deletions

View File

@ -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("/")

View File

@ -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!"

View File

@ -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 '<Account (name=%s, role=%s)>' % 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 '<Role %s>' % self.name

View File

@ -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)

View File

@ -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 ###