From 004bdbd0f32c73a02389bdc0d8244fa2aeb3b71e Mon Sep 17 00:00:00 2001 From: esensar Date: Tue, 8 May 2018 12:58:14 +0200 Subject: [PATCH] Add DAO methods for device models and add device_type to device --- app/accounts/models.py | 2 +- app/devices/models.py | 96 +++++++++++++++++++++++++++- migrations/versions/6b444e5e2eef_.py | 38 +++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/6b444e5e2eef_.py diff --git a/app/accounts/models.py b/app/accounts/models.py index 8df0985..90a0858 100644 --- a/app/accounts/models.py +++ b/app/accounts/models.py @@ -67,7 +67,7 @@ class Account(db.Model): @staticmethod def get(**kwargs): """ - Get accounts with given filters + Get account with given filters Available filters: * username diff --git a/app/devices/models.py b/app/devices/models.py index 07dcb20..4500e5a 100644 --- a/app/devices/models.py +++ b/app/devices/models.py @@ -26,6 +26,10 @@ class Recording(db.Model): self.raw_record = raw_json def save(self): + """ + Stores this recording to database + This may raise errors + """ db.session.add(self) db.session.commit() @@ -33,6 +37,40 @@ class Recording(db.Model): def get_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): return '' % ( self.record_value, self.recorded_at) @@ -50,12 +88,68 @@ class Device(db.Model): default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) 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) + 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 '' % ( + self.name, self.device_type_id) + class DeviceType(db.Model): __tablename__ = 'device_types' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) + + def __init__(self, name): + self.name = name + + def __repr__(self): + return '' % self.name diff --git a/migrations/versions/6b444e5e2eef_.py b/migrations/versions/6b444e5e2eef_.py new file mode 100644 index 0000000..fbc4ecd --- /dev/null +++ b/migrations/versions/6b444e5e2eef_.py @@ -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 ###