diff --git a/app/__init__.py b/app/__init__.py index 421f935..866de43 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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) diff --git a/app/mod_accounts/__init__.py b/app/mod_accounts/__init__.py index 1e4275b..347b528 100644 --- a/app/mod_accounts/__init__.py +++ b/app/mod_accounts/__init__.py @@ -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) diff --git a/app/mod_accounts/controllers.py b/app/mod_accounts/controllers.py new file mode 100644 index 0000000..489f212 --- /dev/null +++ b/app/mod_accounts/controllers.py @@ -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!" + diff --git a/app/mod_accounts/models.py b/app/mod_accounts/models.py index 71bbd12..f4ef51e 100644 --- a/app/mod_accounts/models.py +++ b/app/mod_accounts/models.py @@ -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) diff --git a/app/mod_devices/mqtt_client.py b/app/mod_devices/mqtt_client.py index 66ff0ce..a4aea2f 100644 --- a/app/mod_devices/mqtt_client.py +++ b/app/mod_devices/mqtt_client.py @@ -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: diff --git a/env/pip-selfcheck.json b/env/pip-selfcheck.json index a2f8627..fbd9f66 100644 --- a/env/pip-selfcheck.json +++ b/env/pip-selfcheck.json @@ -1 +1 @@ -{"last_check":"2018-04-26T08:33:46Z","pypi_version":"10.0.1"} \ No newline at end of file +{"last_check":"2018-05-03T14:02:01Z","pypi_version":"10.0.1"} \ No newline at end of file diff --git a/migrations/versions/151f16e166d2_.py b/migrations/versions/151f16e166d2_.py deleted file mode 100644 index b10e2ca..0000000 --- a/migrations/versions/151f16e166d2_.py +++ /dev/null @@ -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 ### diff --git a/migrations/versions/56ec2b819bd8_.py b/migrations/versions/d1db8dcc190d_.py similarity index 93% rename from migrations/versions/56ec2b819bd8_.py rename to migrations/versions/d1db8dcc190d_.py index fed502f..487e66c 100644 --- a/migrations/versions/56ec2b819bd8_.py +++ b/migrations/versions/d1db8dcc190d_.py @@ -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 diff --git a/migrations/versions/5bdb53d5de37_.py b/migrations/versions/eeb3e64b76c4_.py similarity index 54% rename from migrations/versions/5bdb53d5de37_.py rename to migrations/versions/eeb3e64b76c4_.py index 042291e..8b09a9d 100644 --- a/migrations/versions/5bdb53d5de37_.py +++ b/migrations/versions/eeb3e64b76c4_.py @@ -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 ###