Add simple registration
parent
40f5e4f546
commit
2f9fac3540
|
@ -1,4 +1,3 @@
|
|||
import json
|
||||
from app import db
|
||||
from flask import request, current_app
|
||||
from .models import Account, Role
|
||||
|
@ -6,11 +5,13 @@ 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()
|
||||
print(request.data)
|
||||
user = request.data.get('user')
|
||||
acct = Account(user.get('username'),
|
||||
user.get('password'),
|
||||
user.get('email'),
|
||||
2)
|
||||
acct.save()
|
||||
return "Success!"
|
||||
|
||||
|
|
|
@ -1,25 +1,37 @@
|
|||
from sqlalchemy import Column, Integer, String, ForeignKey
|
||||
from app import db
|
||||
|
||||
|
||||
class Account(db.Model):
|
||||
__tablename__ = 'accounts'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
username = Column(String, index=True, unique=True)
|
||||
password = Column(String)
|
||||
email = Column(String, index=True, unique=True)
|
||||
role_id = Column(Integer, ForeignKey("roles.id"))
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String, index=True, unique=True)
|
||||
password = db.Column(db.String)
|
||||
email = db.Column(db.String, index=True, unique=True)
|
||||
role_id = db.Column(db.Integer, db.ForeignKey("roles.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.password = str(password)
|
||||
self.email = str(email)
|
||||
if isinstance(role, Role):
|
||||
self.role_id = role.id
|
||||
else:
|
||||
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):
|
||||
return '<Account (name=%s, role=%s)>' % self.username, self.role
|
||||
|
||||
|
@ -27,11 +39,23 @@ class Account(db.Model):
|
|||
class Role(db.Model):
|
||||
__tablename__ = 'roles'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
display_name = Column(String)
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
display_name = db.Column(db.String)
|
||||
|
||||
def __init__(self, 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):
|
||||
return '<Role %s>' % self.name
|
||||
|
|
|
@ -1,21 +1,20 @@
|
|||
from datetime import datetime
|
||||
from app import db
|
||||
from sqlalchemy import Column, Integer, DateTime, String
|
||||
from sqlalchemy.dialects.postgresql import JSON
|
||||
|
||||
|
||||
class Recording(db.Model):
|
||||
__tablename__ = 'recordings'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
recorded_at = Column(DateTime, index=True,
|
||||
default=datetime.utcnow())
|
||||
received_at = Column(DateTime, index=True,
|
||||
default=datetime.utcnow())
|
||||
device_id = Column(Integer)
|
||||
record_type = Column(Integer, nullable=False)
|
||||
record_value = Column(String, nullable=False)
|
||||
raw_record = Column(JSON, nullable=True)
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
recorded_at = db.Column(db.DateTime, index=True,
|
||||
default=db.func.current_timestamp())
|
||||
received_at = db.Column(db.DateTime, index=True,
|
||||
default=db.func.current_timestamp())
|
||||
device_id = db.Column(db.Integer)
|
||||
record_type = db.Column(db.Integer, nullable=False)
|
||||
record_value = db.Column(db.String, nullable=False)
|
||||
raw_record = db.Column(JSON, nullable=True)
|
||||
|
||||
def __init__(self, device_id, record_type,
|
||||
record_value, recorded_at, raw_json):
|
||||
|
@ -26,6 +25,14 @@ class Recording(db.Model):
|
|||
self.received_at = datetime.utcnow()
|
||||
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):
|
||||
return '<Recording (value=%s, recorded_at=%s)>' % (
|
||||
self.record_value, self.recorded_at)
|
||||
|
|
|
@ -45,8 +45,7 @@ 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.add(recording)
|
||||
db.session.commit()
|
||||
recording.save()
|
||||
except ValueError:
|
||||
print("ERROR!")
|
||||
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