From 066b6a5fd34c4401da1b6d9d19b6d4671bdab9d3 Mon Sep 17 00:00:00 2001 From: esensar Date: Thu, 3 May 2018 15:28:57 +0200 Subject: [PATCH] Add email field to account --- app/mod_accounts/models.py | 11 ++++---- app/mod_devices/models.py | 5 ++-- app/mod_devices/mqtt_client.py | 17 ++++++----- migrations/versions/151f16e166d2_.py | 42 ++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 migrations/versions/151f16e166d2_.py diff --git a/app/mod_accounts/models.py b/app/mod_accounts/models.py index 80582bc..71bbd12 100644 --- a/app/mod_accounts/models.py +++ b/app/mod_accounts/models.py @@ -1,13 +1,14 @@ -from sqlalchemy import (Model, Column, String, - Integer, ForeignKey, relationship) +from sqlalchemy import Column, Integer, String, ForeignKey, relationship +from app import db -class Account(Model): +class Account(db.Model): __tablename__ = 'accounts' id = Column(Integer, primary_key=True) - username = Column(String) + username = Column(String, index=True, unique=True) password = Column(String) + emails = Column(String, index=True, unique=True) role_id = Column(Integer, ForeignKey("roles.id")) role = relationship("Role", foreign_keys=[role_id]) @@ -23,7 +24,7 @@ class Account(Model): return '' % self.username, self.role -class Role(Model): +class Role(db.Model): __tablename__ = 'roles' id = Column(Integer, primary_key=True) diff --git a/app/mod_devices/models.py b/app/mod_devices/models.py index ee79701..6ad5e92 100644 --- a/app/mod_devices/models.py +++ b/app/mod_devices/models.py @@ -1,9 +1,10 @@ from datetime import datetime -from sqlalchemy import Model, DateTime, String, Column, Integer +from app import db +from sqlalchemy import Column, Integer, DateTime, String from sqlalchemy.dialects.postgresql import JSON -class Recording(Model): +class Recording(db.Model): __tablename__ = 'recordings' id = Column(Integer, primary_key=True) diff --git a/app/mod_devices/mqtt_client.py b/app/mod_devices/mqtt_client.py index e3ee203..66ff0ce 100644 --- a/app/mod_devices/mqtt_client.py +++ b/app/mod_devices/mqtt_client.py @@ -2,6 +2,7 @@ import sys import json from flask_mqtt import Mqtt from .models import Recording +from app import db, app mqtt = Mqtt() @@ -37,16 +38,16 @@ 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()) try: # If type is JSON recording = parse_json_message(message.topic, message.payload.decode()) - db.session.add(recording) - db.session.commit() - print(recording) + with app.app_context(): + db.session + db.session.add(recording) + db.session.commit() except ValueError: print("ERROR!") error_type, error_instance, traceback = sys.exc_info() @@ -59,9 +60,11 @@ def parse_json_message(topic, payload) -> Recording: try: json_msg = json.loads(payload) device_id = get_device_id(topic) - return Recording(device_id, json_msg["record_type"], - json_msg["record_value"], json_msg["recorded_at"], - json_msg) + return Recording(device_id=device_id, + record_type=json_msg["record_type"], + record_value=json_msg["record_value"], + recorded_at=json_msg["recorded_at"], + raw_json=json_msg) except KeyError: error_type, error_instance, traceback = sys.exc_info() raise ValueError("JSON parsing failed! Key error: " diff --git a/migrations/versions/151f16e166d2_.py b/migrations/versions/151f16e166d2_.py new file mode 100644 index 0000000..b10e2ca --- /dev/null +++ b/migrations/versions/151f16e166d2_.py @@ -0,0 +1,42 @@ +"""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 ###