From fefa8dbf7785c31772e482e242fe47e818062d70 Mon Sep 17 00:00:00 2001 From: esensar Date: Wed, 19 Sep 2018 22:51:57 +0200 Subject: [PATCH] Use SQLAlchemy 404 responses instead of errors --- app/devices/__init__.py | 13 ------------- app/devices/models.py | 6 +++--- app/mqtt/mqtt_client.py | 2 +- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/app/devices/__init__.py b/app/devices/__init__.py index ece082e..1280c28 100644 --- a/app/devices/__init__.py +++ b/app/devices/__init__.py @@ -63,11 +63,7 @@ def get_device(device_id): :type device_id: int :returns: Requested device :rtype: Device - :raises: ValueError if device does not exist """ - if not Device.exists(id=device_id): - raise ValueError("Device with id %s does not exist" % device_id) - return Device.get(id=device_id) @@ -79,23 +75,14 @@ def get_device_type(device_type_id): :type device_type_id: int :returns: Requested device type :rtype: DeviceType - :raises: ValueError if device type does not exist """ - if not DeviceType.exists(id=device_type_id): - raise ValueError("Device type with id %s does not exist" % - device_type_id) - return DeviceType.get(id=device_type_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() diff --git a/app/devices/models.py b/app/devices/models.py index 07b4bbf..88a5632 100644 --- a/app/devices/models.py +++ b/app/devices/models.py @@ -76,7 +76,7 @@ class Recording(db.Model): * raw_record (useless) """ - return Recording.query.filter_by(**kwargs).first() + return Recording.query.filter_by(**kwargs).first_or_404() def __repr__(self): return '' % ( @@ -163,7 +163,7 @@ class Device(db.Model): * configuration (useless) """ - return Device.query.filter_by(**kwargs).first() + return Device.query.filter_by(**kwargs).first_or_404() @staticmethod def exists(**kwargs): @@ -271,7 +271,7 @@ class DeviceType(db.Model): * id * name """ - return DeviceType.query.filter_by(**kwargs).first() + return DeviceType.query.filter_by(**kwargs).first_or_404() @staticmethod def exists(**kwargs): diff --git a/app/mqtt/mqtt_client.py b/app/mqtt/mqtt_client.py index a722fbe..fbf9682 100644 --- a/app/mqtt/mqtt_client.py +++ b/app/mqtt/mqtt_client.py @@ -59,7 +59,7 @@ class MqttClient: return @staticmethod - def get_device_id(topic) -> int: + def get_device_id(topic): device_token, device_id = topic.split("/") if device_token == "device": return int(device_id)