Add email field to account

master
esensar 2018-05-03 15:28:57 +02:00
parent e188141a2a
commit 066b6a5fd3
4 changed files with 61 additions and 14 deletions

View File

@ -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 '<Account (name=%s, role=%s)>' % self.username, self.role
class Role(Model):
class Role(db.Model):
__tablename__ = 'roles'
id = Column(Integer, primary_key=True)

View File

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

View File

@ -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())
with app.app_context():
db.session
db.session.add(recording)
db.session.commit()
print(recording)
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: "

View File

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