Add simple registration
parent
40f5e4f546
commit
2f9fac3540
|
@ -1,4 +1,3 @@
|
||||||
import json
|
|
||||||
from app import db
|
from app import db
|
||||||
from flask import request, current_app
|
from flask import request, current_app
|
||||||
from .models import Account, Role
|
from .models import Account, Role
|
||||||
|
@ -6,11 +5,13 @@ from .models import Account, Role
|
||||||
def initialize_routes(accounts):
|
def initialize_routes(accounts):
|
||||||
@accounts.route("/", methods=['POST'])
|
@accounts.route("/", methods=['POST'])
|
||||||
def create():
|
def create():
|
||||||
json_body = json.loads(request.data)
|
|
||||||
with current_app.app_context():
|
with current_app.app_context():
|
||||||
acct = Account(2, json_body["user.username"],
|
print(request.data)
|
||||||
json_body["user.password"])
|
user = request.data.get('user')
|
||||||
db.session.add(acct)
|
acct = Account(user.get('username'),
|
||||||
db.session.commit()
|
user.get('password'),
|
||||||
|
user.get('email'),
|
||||||
|
2)
|
||||||
|
acct.save()
|
||||||
return "Success!"
|
return "Success!"
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,37 @@
|
||||||
from sqlalchemy import Column, Integer, String, ForeignKey
|
|
||||||
from app import db
|
from app import db
|
||||||
|
|
||||||
|
|
||||||
class Account(db.Model):
|
class Account(db.Model):
|
||||||
__tablename__ = 'accounts'
|
__tablename__ = 'accounts'
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = Column(String, index=True, unique=True)
|
username = db.Column(db.String, index=True, unique=True)
|
||||||
password = Column(String)
|
password = db.Column(db.String)
|
||||||
email = Column(String, index=True, unique=True)
|
email = db.Column(db.String, index=True, unique=True)
|
||||||
role_id = Column(Integer, ForeignKey("roles.id"))
|
role_id = db.Column(db.Integer, db.ForeignKey("roles.id"))
|
||||||
role = db.relationship("Role", foreign_keys=[role_id])
|
role = db.relationship("Role", foreign_keys=[role_id])
|
||||||
|
|
||||||
def __init__(self, username, password, role):
|
def __init__(self, username, password, email, role):
|
||||||
self.username = str(username)
|
self.username = str(username)
|
||||||
self.password = str(password)
|
self.password = str(password)
|
||||||
|
self.email = str(email)
|
||||||
if isinstance(role, Role):
|
if isinstance(role, Role):
|
||||||
self.role_id = role.id
|
self.role_id = role.id
|
||||||
else:
|
else:
|
||||||
self.role_id = int(role)
|
self.role_id = int(role)
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
db.session.add(self)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_all():
|
||||||
|
return Account.query.all()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get(accId):
|
||||||
|
return Account.query.filter_by(id = accId)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Account (name=%s, role=%s)>' % self.username, self.role
|
return '<Account (name=%s, role=%s)>' % self.username, self.role
|
||||||
|
|
||||||
|
@ -27,11 +39,23 @@ class Account(db.Model):
|
||||||
class Role(db.Model):
|
class Role(db.Model):
|
||||||
__tablename__ = 'roles'
|
__tablename__ = 'roles'
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
display_name = Column(String)
|
display_name = db.Column(db.String)
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.display_name = str(name)
|
self.display_name = str(name)
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
db.session.add(self)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_all():
|
||||||
|
return Role.query.all()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get(roleId):
|
||||||
|
return Role.query.filter_by(id = accId)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Role %s>' % self.name
|
return '<Role %s>' % self.name
|
||||||
|
|
|
@ -1,21 +1,20 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from app import db
|
from app import db
|
||||||
from sqlalchemy import Column, Integer, DateTime, String
|
|
||||||
from sqlalchemy.dialects.postgresql import JSON
|
from sqlalchemy.dialects.postgresql import JSON
|
||||||
|
|
||||||
|
|
||||||
class Recording(db.Model):
|
class Recording(db.Model):
|
||||||
__tablename__ = 'recordings'
|
__tablename__ = 'recordings'
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
recorded_at = Column(DateTime, index=True,
|
recorded_at = db.Column(db.DateTime, index=True,
|
||||||
default=datetime.utcnow())
|
default=db.func.current_timestamp())
|
||||||
received_at = Column(DateTime, index=True,
|
received_at = db.Column(db.DateTime, index=True,
|
||||||
default=datetime.utcnow())
|
default=db.func.current_timestamp())
|
||||||
device_id = Column(Integer)
|
device_id = db.Column(db.Integer)
|
||||||
record_type = Column(Integer, nullable=False)
|
record_type = db.Column(db.Integer, nullable=False)
|
||||||
record_value = Column(String, nullable=False)
|
record_value = db.Column(db.String, nullable=False)
|
||||||
raw_record = Column(JSON, nullable=True)
|
raw_record = db.Column(JSON, nullable=True)
|
||||||
|
|
||||||
def __init__(self, device_id, record_type,
|
def __init__(self, device_id, record_type,
|
||||||
record_value, recorded_at, raw_json):
|
record_value, recorded_at, raw_json):
|
||||||
|
@ -26,6 +25,14 @@ class Recording(db.Model):
|
||||||
self.received_at = datetime.utcnow()
|
self.received_at = datetime.utcnow()
|
||||||
self.raw_record = raw_json
|
self.raw_record = raw_json
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
db.session.add(self)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_all():
|
||||||
|
return Recording.query.all()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Recording (value=%s, recorded_at=%s)>' % (
|
return '<Recording (value=%s, recorded_at=%s)>' % (
|
||||||
self.record_value, self.recorded_at)
|
self.record_value, self.recorded_at)
|
||||||
|
|
|
@ -45,8 +45,7 @@ def handle_mqtt_message(client, userdata, message):
|
||||||
# If type is JSON
|
# If type is JSON
|
||||||
recording = parse_json_message(message.topic, message.payload.decode())
|
recording = parse_json_message(message.topic, message.payload.decode())
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.session.add(recording)
|
recording.save()
|
||||||
db.session.commit()
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("ERROR!")
|
print("ERROR!")
|
||||||
error_type, error_instance, traceback = sys.exc_info()
|
error_type, error_instance, traceback = sys.exc_info()
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 7e9220844b2f
|
||||||
|
Revises: eeb3e64b76c4
|
||||||
|
Create Date: 2018-05-03 16:58:10.100904
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '7e9220844b2f'
|
||||||
|
down_revision = 'eeb3e64b76c4'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('accounts', sa.Column('email', sa.String(), nullable=True))
|
||||||
|
op.create_index(op.f('ix_accounts_email'), 'accounts', ['email'], unique=True)
|
||||||
|
op.drop_index('ix_accounts_emails', table_name='accounts')
|
||||||
|
op.drop_column('accounts', 'emails')
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('accounts', sa.Column('emails', sa.VARCHAR(), autoincrement=False, nullable=True))
|
||||||
|
op.create_index('ix_accounts_emails', 'accounts', ['emails'], unique=True)
|
||||||
|
op.drop_index(op.f('ix_accounts_email'), table_name='accounts')
|
||||||
|
op.drop_column('accounts', 'email')
|
||||||
|
# ### end Alembic commands ###
|
Loading…
Reference in New Issue