Make POST and PUT return created devices objects

develop
esensar 2018-10-24 22:11:55 +02:00
parent c024e1be76
commit bb8d747de5
7 changed files with 92 additions and 33 deletions

View File

@ -44,7 +44,7 @@ class DeviceResource(ProtectedResource):
@swag_from('swagger/get_device_spec.yaml') @swag_from('swagger/get_device_spec.yaml')
def get(self, device_id): def get(self, device_id):
validate_device_ownership(device_id) validate_device_ownership(device_id)
return DeviceSchema().dump( return DeviceWithConfigurationSchema().dump(
devices.get_device(device_id)), 200 devices.get_device(device_id)), 200
@swag_from('swagger/delete_device_spec.yaml') @swag_from('swagger/delete_device_spec.yaml')
@ -68,10 +68,9 @@ class DeviceTypeListResource(ProtectedResource):
if g.current_account.role_id != 1: if g.current_account.role_id != 1:
abort(403, message='Only admin may create device types', abort(403, message='Only admin may create device types',
status='error') status='error')
success = devices.create_device_type( created_device_type = devices.create_device_type(
args['name']) args['name'])
if success: return DeviceTypeSchema().dump(created_device_type), 201
return '', 201
@swag_from('swagger/get_device_types_spec.yaml') @swag_from('swagger/get_device_types_spec.yaml')
def get(self): def get(self):
@ -94,21 +93,20 @@ class DeviceRecordingResource(ProtectedResource):
@swag_from('swagger/create_device_recording_spec.yaml') @swag_from('swagger/create_device_recording_spec.yaml')
def post(self, device_id): def post(self, device_id):
validate_device_ownership(device_id) validate_device_ownership(device_id)
success = devices.create_recording(device_id, request.json) created_recording = devices.create_recording_and_return(
if success: device_id, request.json)
return '', 201 return RecordingsSchema().dump(created_recording), 201
class DeviceListResource(ProtectedResource): class DeviceListResource(ProtectedResource):
@use_args(DeviceSchema(), locations=('json',)) @use_args(DeviceSchema(), locations=('json',))
@swag_from('swagger/create_device_spec.yaml') @swag_from('swagger/create_device_spec.yaml')
def post(self, args): def post(self, args):
success = devices.create_device( created_device = devices.create_device(
args['name'], args['name'],
g.current_account.id, g.current_account.id,
args['device_type_id']) args['device_type_id'])
if success: return DeviceSchema().dump(created_device), 201
return '', 201
@swag_from('swagger/get_devices_spec.yaml') @swag_from('swagger/get_devices_spec.yaml')
def get(self): def get(self):
@ -120,9 +118,10 @@ class DeviceConfigurationResource(ProtectedResource):
@swag_from('swagger/update_device_configuration_spec.yaml') @swag_from('swagger/update_device_configuration_spec.yaml')
def put(self, device_id): def put(self, device_id):
validate_device_ownership(device_id) validate_device_ownership(device_id)
success = devices.set_device_configuration(device_id, request.json) updated_device = devices.set_device_configuration(
if success: device_id, request.json)
return '', 204 return DeviceWithConfigurationSchema().dump(
updated_device), 200
@swag_from('swagger/get_device_configuration_spec.yaml') @swag_from('swagger/get_device_configuration_spec.yaml')
def get(self, device_id): def get(self, device_id):

View File

@ -16,4 +16,11 @@ parameters:
$ref: '#/definitions/RecordingCreation' $ref: '#/definitions/RecordingCreation'
responses: responses:
201: 201:
description: Successful creation description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/Recording'

View File

@ -12,4 +12,11 @@ parameters:
$ref: '#/definitions/DeviceCreation' $ref: '#/definitions/DeviceCreation'
responses: responses:
201: 201:
description: Successful creation description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/DeviceWithConfig'

View File

@ -12,4 +12,11 @@ parameters:
$ref: '#/definitions/DeviceType' $ref: '#/definitions/DeviceType'
responses: responses:
201: 201:
description: Successful creation description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/DeviceType'

View File

@ -15,5 +15,12 @@ parameters:
schema: schema:
type: object type: object
responses: responses:
204: 200:
description: Success description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/DeviceWithConfig'

View File

@ -20,6 +20,7 @@ def create_device(name, account_id, device_type=1):
device.save() device.save()
device_association = DeviceAssociation(device.id, account_id) device_association = DeviceAssociation(device.id, account_id)
device_association.save() device_association.save()
return device
def create_device_type(name): def create_device_type(name):
@ -33,6 +34,7 @@ def create_device_type(name):
""" """
device_type = DeviceType(name) device_type = DeviceType(name)
device_type.save() device_type.save()
return device_type
def set_device_configuration(device_id, configuration_json): def set_device_configuration(device_id, configuration_json):
@ -50,6 +52,7 @@ def set_device_configuration(device_id, configuration_json):
device.configuration = configuration_json device.configuration = configuration_json
device.save() device.save()
send_config.delay(device_id, str(configuration_json)) send_config.delay(device_id, str(configuration_json))
return device
def get_device_configuration(device_id): def get_device_configuration(device_id):
@ -173,17 +176,16 @@ def get_device_types():
return DeviceType.get_many() return DeviceType.get_many()
def create_recording(device_id, raw_json): def parse_raw_json_recording(device_id, json_msg):
""" """
Tries to create recording with given parameters. Raises error on failure Parses raw json recording and creates Recrding object
:param device_id: Id of device :param device_id: Id of device
:type device_id: int :type device_id: int
:param raw_json: Raw json received :param raw_json: Raw json received
:type raw_json: json :type raw_json: json
:raises: ValueError if parsing fails or device does not exist :raises: ValueError if parsing fails
""" """
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"],
@ -195,6 +197,36 @@ def create_recording(device_id, raw_json):
raise ValueError("JSON parsing failed! Key error: " raise ValueError("JSON parsing failed! Key error: "
+ str(error_instance)) + str(error_instance))
def create_recording_and_return(device_id, raw_json):
"""
Tries to create recording with given parameters and returns it.
Raises error on failure
:param device_id: Id of device
:type device_id: int
:param raw_json: Raw json received
:type raw_json: json
:raises: ValueError if parsing fails or device does not exist
"""
if not Device.exists(id=device_id):
raise ValueError("Device does not exist!")
recording = parse_raw_json_recording(device_id, raw_json)
recording.save()
return recording
def create_recording(device_id, raw_json):
"""
Tries to create recording with given parameters. Raises error on failure
:param device_id: Id of device
:type device_id: int
:param raw_json: Raw json received
:type raw_json: json
:raises: ValueError if parsing fails or device does not exist
"""
if not Device.exists(id=device_id): if not Device.exists(id=device_id):
raise ValueError("Device does not exist!") raise ValueError("Device does not exist!")

View File

@ -8,7 +8,7 @@ from sqlalchemy.dialects.postgresql import JSON
class Recording(db.Model): class Recording(db.Model):
__tablename__ = 'recordings' __tablename__ = 'recordings'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
recorded_at = db.Column(db.DateTime, index=True, recorded_at = db.Column(db.DateTime, index=True,
default=db.func.current_timestamp()) default=db.func.current_timestamp())
received_at = db.Column(db.DateTime, index=True, received_at = db.Column(db.DateTime, index=True,
@ -115,7 +115,7 @@ class Recording(db.Model):
class Device(db.Model): class Device(db.Model):
__tablename__ = 'devices' __tablename__ = 'devices'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
created_at = db.Column(db.DateTime, created_at = db.Column(db.DateTime,
nullable=False, nullable=False,
default=db.func.current_timestamp()) default=db.func.current_timestamp())
@ -267,7 +267,7 @@ class DeviceAssociation(db.Model):
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, autoincrement=True)
name = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False)
def __init__(self, name): def __init__(self, name):
@ -320,7 +320,7 @@ class DeviceType(db.Model):
class AccessLevel(db.Model): class AccessLevel(db.Model):
__tablename__ = 'access_levels' __tablename__ = 'access_levels'
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False)
def __init__(self, name): def __init__(self, name):