Add delete device route
parent
f56fb88b45
commit
6d303e1d99
|
@ -14,9 +14,11 @@ class DeviceSchema(Schema):
|
||||||
class DeviceWrapperSchema(Schema):
|
class DeviceWrapperSchema(Schema):
|
||||||
device = fields.Nested(DeviceSchema, required=True, location='json')
|
device = fields.Nested(DeviceSchema, required=True, location='json')
|
||||||
|
|
||||||
|
|
||||||
class DevicesWrapperSchema(Schema):
|
class DevicesWrapperSchema(Schema):
|
||||||
devices = fields.Nested(DeviceSchema, required=True,
|
devices = fields.Nested(DeviceSchema, required=True,
|
||||||
location='json', many=True)
|
location='json', many=True)
|
||||||
|
|
||||||
|
|
||||||
class RecordingsSchema(Schema):
|
class RecordingsSchema(Schema):
|
||||||
recorded_at = fields.DateTime()
|
recorded_at = fields.DateTime()
|
||||||
|
@ -35,6 +37,11 @@ class DeviceResource(ProtectedResource):
|
||||||
return DeviceWrapperSchema().dump(
|
return DeviceWrapperSchema().dump(
|
||||||
{'device': devices.get_device(device_id)}), 200
|
{'device': devices.get_device(device_id)}), 200
|
||||||
|
|
||||||
|
@swag_from('swagger/delete_device_spec.yaml')
|
||||||
|
def delete(self, device_id):
|
||||||
|
devices.delete_device(device_id)
|
||||||
|
return '', 204
|
||||||
|
|
||||||
|
|
||||||
class DeviceRecordingResource(ProtectedResource):
|
class DeviceRecordingResource(ProtectedResource):
|
||||||
@swag_from('swagger/get_device_recordings_spec.yaml')
|
@swag_from('swagger/get_device_recordings_spec.yaml')
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
Deletes a device
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- Device
|
||||||
|
parameters:
|
||||||
|
- in: path
|
||||||
|
name: device_id
|
||||||
|
required: true
|
||||||
|
type: integer
|
||||||
|
description: Id of the device
|
||||||
|
responses:
|
||||||
|
204:
|
||||||
|
description: Success
|
|
@ -58,6 +58,17 @@ def get_device(device_id):
|
||||||
return Device.get(id=device_id)
|
return Device.get(id=device_id)
|
||||||
|
|
||||||
|
|
||||||
|
def delete_device(device_id):
|
||||||
|
"""
|
||||||
|
Tries to delete device with given parameters. Does not raise errors
|
||||||
|
|
||||||
|
"""
|
||||||
|
if not Device.exists(id=device_id):
|
||||||
|
return
|
||||||
|
|
||||||
|
Device.get(id=device_id).delete()
|
||||||
|
|
||||||
|
|
||||||
def get_devices(account_id):
|
def get_devices(account_id):
|
||||||
"""
|
"""
|
||||||
Tries to get all devices associated to account. Raises error on
|
Tries to get all devices associated to account. Raises error on
|
||||||
|
@ -68,6 +79,7 @@ def get_devices(account_id):
|
||||||
"""
|
"""
|
||||||
return Device.get_many_for_user(account_id)
|
return Device.get_many_for_user(account_id)
|
||||||
|
|
||||||
|
|
||||||
def create_recording(device_id, raw_json):
|
def create_recording(device_id, raw_json):
|
||||||
"""
|
"""
|
||||||
Tries to create recording with given parameters. Raises error on failure
|
Tries to create recording with given parameters. Raises error on failure
|
||||||
|
@ -78,7 +90,7 @@ def create_recording(device_id, raw_json):
|
||||||
:type raw_json: json
|
:type raw_json: json
|
||||||
:raises: ValueError if parsing fails or device does not exist
|
:raises: ValueError if parsing fails or device does not exist
|
||||||
"""
|
"""
|
||||||
def parse_raw_json_recording(device_id, json_msg) -> Recording:
|
def parse_raw_json_recording(device_id, json_msg):
|
||||||
try:
|
try:
|
||||||
return Recording(device_id=device_id,
|
return Recording(device_id=device_id,
|
||||||
record_type=json_msg["record_type"],
|
record_type=json_msg["record_type"],
|
||||||
|
|
|
@ -33,6 +33,13 @@ class Recording(db.Model):
|
||||||
db.session.add(self)
|
db.session.add(self)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
"""
|
||||||
|
Deletes this recording from database
|
||||||
|
"""
|
||||||
|
db.session.delete(self)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all():
|
def get_all():
|
||||||
return Recording.query.all()
|
return Recording.query.all()
|
||||||
|
@ -92,7 +99,10 @@ class Device(db.Model):
|
||||||
device_type = db.relationship("DeviceType", foreign_keys=[device_type_id])
|
device_type = db.relationship("DeviceType", foreign_keys=[device_type_id])
|
||||||
configuration = db.Column(JSON, nullable=True)
|
configuration = db.Column(JSON, nullable=True)
|
||||||
|
|
||||||
users = db.relationship("DeviceAssociation")
|
users = db.relationship("DeviceAssociation",
|
||||||
|
cascade="save-update, merge, delete")
|
||||||
|
recordings = db.relationship("Recording",
|
||||||
|
cascade="save-update, merge, delete")
|
||||||
|
|
||||||
def __init__(self, name, configuration=None, device_type=1):
|
def __init__(self, name, configuration=None, device_type=1):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -107,6 +117,13 @@ class Device(db.Model):
|
||||||
db.session.add(self)
|
db.session.add(self)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
"""
|
||||||
|
Deletes this recording from database
|
||||||
|
"""
|
||||||
|
db.session.delete(self)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_many(**kwargs):
|
def get_many(**kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -128,7 +145,9 @@ class Device(db.Model):
|
||||||
"""
|
"""
|
||||||
Get many devices which are associated to account
|
Get many devices which are associated to account
|
||||||
"""
|
"""
|
||||||
return Device.query.filter(Device.users.any(account_id=account_id)).all()
|
return Device.query.filter(
|
||||||
|
Device.users.any(account_id=account_id)
|
||||||
|
).all()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get(**kwargs):
|
def get(**kwargs):
|
||||||
|
@ -159,14 +178,16 @@ class Device(db.Model):
|
||||||
return '<Device (name=%s, type=%s)>' % (
|
return '<Device (name=%s, type=%s)>' % (
|
||||||
self.name, self.device_type_id)
|
self.name, self.device_type_id)
|
||||||
|
|
||||||
|
|
||||||
class DeviceAssociation(db.Model):
|
class DeviceAssociation(db.Model):
|
||||||
__tablename__ = 'device_associations'
|
__tablename__ = 'device_associations'
|
||||||
|
|
||||||
device_id = db.Column(db.Integer, db.ForeignKey('devices.id'), primary_key=True)
|
device_id = db.Column(db.Integer, db.ForeignKey('devices.id'),
|
||||||
account_id = db.Column(db.Integer, db.ForeignKey('accounts.id'), primary_key=True)
|
primary_key=True)
|
||||||
|
account_id = db.Column(db.Integer, db.ForeignKey('accounts.id'),
|
||||||
|
primary_key=True)
|
||||||
access_level = db.Column(db.Integer, db.ForeignKey('access_levels.id'),
|
access_level = db.Column(db.Integer, db.ForeignKey('access_levels.id'),
|
||||||
nullable=False)
|
nullable=False)
|
||||||
|
|
||||||
def __init__(self, device_id, account_id, access_level=1):
|
def __init__(self, device_id, account_id, access_level=1):
|
||||||
self.device_id = device_id
|
self.device_id = device_id
|
||||||
|
@ -198,7 +219,7 @@ class DeviceAssociation(db.Model):
|
||||||
Get many device associations for user with account id passed in
|
Get many device associations for user with account id passed in
|
||||||
parameter
|
parameter
|
||||||
"""
|
"""
|
||||||
return get_many(account_id=account_id)
|
return DeviceAssociation.get_many(account_id=account_id)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_for_device(device_id):
|
def get_for_device(device_id):
|
||||||
|
@ -206,10 +227,11 @@ class DeviceAssociation(db.Model):
|
||||||
Get many device associations for device with account id passed in
|
Get many device associations for device with account id passed in
|
||||||
parameter
|
parameter
|
||||||
"""
|
"""
|
||||||
return get_many(device_id=device_id)
|
return DeviceAssociation.get_many(device_id=device_id)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<DeviceAssociation (device_id=%s, accoount_id=%s)>' % (self.device_id, self.account_id)
|
return '<DeviceAssociation (device_id=%s, accoount_id=%s)>' % (
|
||||||
|
self.device_id, self.account_id)
|
||||||
|
|
||||||
|
|
||||||
class DeviceType(db.Model):
|
class DeviceType(db.Model):
|
||||||
|
|
Loading…
Reference in New Issue