Add roles in migration

master
esensar 2018-05-03 16:40:30 +02:00
parent 066b6a5fd3
commit 40f5e4f546
9 changed files with 45 additions and 64 deletions

View File

@ -1,8 +1,8 @@
# App initialization
from flask import Flask
from flask_api import FlaskAPI
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__, instance_relative_config=True)
app = FlaskAPI(__name__, instance_relative_config=True)
app.config.from_object('config')
app.config.from_pyfile('config.py')
db = SQLAlchemy(app)

View File

@ -1,9 +1,6 @@
from flask import Blueprint
from .controllers import initialize_routes
accounts = Blueprint('accounts', __name__)
# Routes
@accounts.route("/")
def hello():
return "Hello from accounts!"
initialize_routes(accounts)

View File

@ -0,0 +1,16 @@
import json
from app import db
from flask import request, current_app
from .models import Account, Role
def initialize_routes(accounts):
@accounts.route("/", methods=['POST'])
def create():
json_body = json.loads(request.data)
with current_app.app_context():
acct = Account(2, json_body["user.username"],
json_body["user.password"])
db.session.add(acct)
db.session.commit()
return "Success!"

View File

@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, String, ForeignKey, relationship
from sqlalchemy import Column, Integer, String, ForeignKey
from app import db
@ -8,9 +8,9 @@ class Account(db.Model):
id = Column(Integer, primary_key=True)
username = Column(String, index=True, unique=True)
password = Column(String)
emails = Column(String, index=True, unique=True)
email = Column(String, index=True, unique=True)
role_id = Column(Integer, ForeignKey("roles.id"))
role = relationship("Role", foreign_keys=[role_id])
role = db.relationship("Role", foreign_keys=[role_id])
def __init__(self, username, password, role):
self.username = str(username)

View File

@ -45,7 +45,6 @@ def handle_mqtt_message(client, userdata, message):
# If type is JSON
recording = parse_json_message(message.topic, message.payload.decode())
with app.app_context():
db.session
db.session.add(recording)
db.session.commit()
except ValueError:

View File

@ -1 +1 @@
{"last_check":"2018-04-26T08:33:46Z","pypi_version":"10.0.1"}
{"last_check":"2018-05-03T14:02:01Z","pypi_version":"10.0.1"}

View File

@ -1,42 +0,0 @@
"""empty message
Revision ID: 151f16e166d2
Revises: 5bdb53d5de37
Create Date: 2018-05-03 14:59:15.784509
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '151f16e166d2'
down_revision = '5bdb53d5de37'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('accounts')
op.drop_table('roles')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('roles',
sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('roles_id_seq'::regclass)"), nullable=False),
sa.Column('display_name', sa.VARCHAR(), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('id', name='roles_pkey'),
postgresql_ignore_search_path=False
)
op.create_table('accounts',
sa.Column('id', sa.INTEGER(), nullable=False),
sa.Column('username', sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column('password', sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column('role_id', sa.INTEGER(), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(['role_id'], ['roles.id'], name='accounts_role_id_fkey'),
sa.PrimaryKeyConstraint('id', name='accounts_pkey')
)
# ### end Alembic commands ###

View File

@ -1,8 +1,8 @@
"""empty message
Revision ID: 56ec2b819bd8
Revision ID: d1db8dcc190d
Revises:
Create Date: 2018-04-27 16:05:14.796856
Create Date: 2018-05-03 16:21:14.351605
"""
from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '56ec2b819bd8'
revision = 'd1db8dcc190d'
down_revision = None
branch_labels = None
depends_on = None

View File

@ -1,8 +1,8 @@
"""empty message
Revision ID: 5bdb53d5de37
Revises: 56ec2b819bd8
Create Date: 2018-05-03 09:48:10.275137
Revision ID: eeb3e64b76c4
Revises: d1db8dcc190d
Create Date: 2018-05-03 16:24:30.369255
"""
from alembic import op
@ -10,32 +10,43 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '5bdb53d5de37'
down_revision = '56ec2b819bd8'
revision = 'eeb3e64b76c4'
down_revision = 'd1db8dcc190d'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('roles',
roles_table = 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',
accounts_table = 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('emails', sa.String(), nullable=True),
sa.Column('role_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['role_id'], ['roles.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_accounts_emails'), 'accounts', ['emails'], unique=True)
op.create_index(op.f('ix_accounts_username'), 'accounts', ['username'], unique=True)
op.bulk_insert(roles_table,
[
{'id':1, 'display_name':'ADMIN'},
{'id':2, 'display_name':'USER'}
]
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_accounts_username'), table_name='accounts')
op.drop_index(op.f('ix_accounts_emails'), table_name='accounts')
op.drop_table('accounts')
op.drop_table('roles')
# ### end Alembic commands ###