Add DAO methods for device models and add device_type to device

master
esensar 2018-05-08 12:58:14 +02:00
parent b9fb4aa708
commit 004bdbd0f3
3 changed files with 134 additions and 2 deletions

View File

@ -67,7 +67,7 @@ class Account(db.Model):
@staticmethod @staticmethod
def get(**kwargs): def get(**kwargs):
""" """
Get accounts with given filters Get account with given filters
Available filters: Available filters:
* username * username

View File

@ -26,6 +26,10 @@ class Recording(db.Model):
self.raw_record = raw_json self.raw_record = raw_json
def save(self): def save(self):
"""
Stores this recording to database
This may raise errors
"""
db.session.add(self) db.session.add(self)
db.session.commit() db.session.commit()
@ -33,6 +37,40 @@ class Recording(db.Model):
def get_all(): def get_all():
return Recording.query.all() return Recording.query.all()
@staticmethod
def get_many(**kwargs):
"""
Get many recording with given filters as a list
Available filters:
* id
* device_id
* record_type
* recorded_at
* received_at
* record_value (probably useless)
* raw_record (useless)
"""
return Recording.query.filter_by(**kwargs).all()
@staticmethod
def get(**kwargs):
"""
Get recordings with given filters
Available filters:
* id
* device_id
* record_type
* recorded_at
* received_at
* record_value (probably useless)
* raw_record (useless)
"""
return Recording.query.filter_by(**kwargs).first()
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)
@ -50,12 +88,68 @@ class Device(db.Model):
default=db.func.current_timestamp(), default=db.func.current_timestamp(),
onupdate=db.func.current_timestamp()) onupdate=db.func.current_timestamp())
name = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False)
device_type = db.Column(db.Integer, db.ForeignKey('device_types.id')) device_type_id = db.Column(db.Integer, db.ForeignKey('device_types.id'))
device_type = db.relationship("DeviceType", foreign_keys=[device_type_id])
configuration = db.Column(JSON, nullable=True) configuration = db.Column(JSON, nullable=True)
def __init__(self, name, configuration=None, device_type=1):
self.name = name
self.configuration = configuration
self.device_type_id = device_type
def save(self):
"""
Stores this device to database
This may raise errors
"""
db.session.add(self)
db.session.commit()
@staticmethod
def get_many(**kwargs):
"""
Get many devices with given filters as a list
Available filters:
* id
* name
* device_type_id
* created_at
* modified_at
* configuration (useless)
"""
return Device.query.filter_by(**kwargs).all()
@staticmethod
def get(**kwargs):
"""
Get device with given filters
Available filters:
* id
* name
* device_type_id
* created_at
* modified_at
* configuration (useless)
"""
return Device.query.filter_by(**kwargs).first()
def __repr__(self):
return '<Device (name=%s, type=%s)>' % (
self.name, self.device_type_id)
class DeviceType(db.Model): class DeviceType(db.Model):
__tablename__ = 'device_types' __tablename__ = 'device_types'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False)
def __init__(self, name):
self.name = name
def __repr__(self):
return '<DeviceType (name %s)>' % self.name

View File

@ -0,0 +1,38 @@
"""empty message
Revision ID: 6b444e5e2eef
Revises: c580745330a9
Create Date: 2018-05-08 11:16:27.441468
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '6b444e5e2eef'
down_revision = 'c580745330a9'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('devices', 'device_type', new_column_name='device_type_id')
device_types_table = sa.Table('device_types', sa.MetaData(),
sa.Column('id', sa.Integer),
sa.Column('name', sa.String))
op.bulk_insert(device_types_table,
[
{'id':1, 'name':'REGULAR'}
]
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('devices', 'device_type_id', new_column_name='device_type')
# ### end Alembic commands ###