From bb8d747de5c541cb07c16b0e77890a170a4be2fb Mon Sep 17 00:00:00 2001 From: esensar Date: Wed, 24 Oct 2018 22:11:55 +0200 Subject: [PATCH 1/2] Make POST and PUT return created devices objects --- app/api/resources/device.py | 25 ++++----- .../swagger/create_device_recording_spec.yaml | 9 ++- .../resources/swagger/create_device_spec.yaml | 9 ++- .../swagger/create_device_type_spec.yaml | 9 ++- .../update_device_configuration_spec.yaml | 9 ++- app/devices/api.py | 56 +++++++++++++++---- app/devices/models.py | 8 +-- 7 files changed, 92 insertions(+), 33 deletions(-) diff --git a/app/api/resources/device.py b/app/api/resources/device.py index bf23d98..8f6e120 100644 --- a/app/api/resources/device.py +++ b/app/api/resources/device.py @@ -44,7 +44,7 @@ class DeviceResource(ProtectedResource): @swag_from('swagger/get_device_spec.yaml') def get(self, device_id): validate_device_ownership(device_id) - return DeviceSchema().dump( + return DeviceWithConfigurationSchema().dump( devices.get_device(device_id)), 200 @swag_from('swagger/delete_device_spec.yaml') @@ -68,10 +68,9 @@ class DeviceTypeListResource(ProtectedResource): if g.current_account.role_id != 1: abort(403, message='Only admin may create device types', status='error') - success = devices.create_device_type( + created_device_type = devices.create_device_type( args['name']) - if success: - return '', 201 + return DeviceTypeSchema().dump(created_device_type), 201 @swag_from('swagger/get_device_types_spec.yaml') def get(self): @@ -94,21 +93,20 @@ class DeviceRecordingResource(ProtectedResource): @swag_from('swagger/create_device_recording_spec.yaml') def post(self, device_id): validate_device_ownership(device_id) - success = devices.create_recording(device_id, request.json) - if success: - return '', 201 + created_recording = devices.create_recording_and_return( + device_id, request.json) + return RecordingsSchema().dump(created_recording), 201 class DeviceListResource(ProtectedResource): @use_args(DeviceSchema(), locations=('json',)) @swag_from('swagger/create_device_spec.yaml') def post(self, args): - success = devices.create_device( + created_device = devices.create_device( args['name'], g.current_account.id, args['device_type_id']) - if success: - return '', 201 + return DeviceSchema().dump(created_device), 201 @swag_from('swagger/get_devices_spec.yaml') def get(self): @@ -120,9 +118,10 @@ class DeviceConfigurationResource(ProtectedResource): @swag_from('swagger/update_device_configuration_spec.yaml') def put(self, device_id): validate_device_ownership(device_id) - success = devices.set_device_configuration(device_id, request.json) - if success: - return '', 204 + updated_device = devices.set_device_configuration( + device_id, request.json) + return DeviceWithConfigurationSchema().dump( + updated_device), 200 @swag_from('swagger/get_device_configuration_spec.yaml') def get(self, device_id): diff --git a/app/api/resources/swagger/create_device_recording_spec.yaml b/app/api/resources/swagger/create_device_recording_spec.yaml index 5d947d4..25a77e7 100644 --- a/app/api/resources/swagger/create_device_recording_spec.yaml +++ b/app/api/resources/swagger/create_device_recording_spec.yaml @@ -16,4 +16,11 @@ parameters: $ref: '#/definitions/RecordingCreation' responses: 201: - description: Successful creation + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/Recording' diff --git a/app/api/resources/swagger/create_device_spec.yaml b/app/api/resources/swagger/create_device_spec.yaml index df480b8..ba43c9d 100644 --- a/app/api/resources/swagger/create_device_spec.yaml +++ b/app/api/resources/swagger/create_device_spec.yaml @@ -12,4 +12,11 @@ parameters: $ref: '#/definitions/DeviceCreation' responses: 201: - description: Successful creation + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/DeviceWithConfig' diff --git a/app/api/resources/swagger/create_device_type_spec.yaml b/app/api/resources/swagger/create_device_type_spec.yaml index 373705e..bb12745 100644 --- a/app/api/resources/swagger/create_device_type_spec.yaml +++ b/app/api/resources/swagger/create_device_type_spec.yaml @@ -12,4 +12,11 @@ parameters: $ref: '#/definitions/DeviceType' responses: 201: - description: Successful creation + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/DeviceType' diff --git a/app/api/resources/swagger/update_device_configuration_spec.yaml b/app/api/resources/swagger/update_device_configuration_spec.yaml index b96673a..fc3d605 100644 --- a/app/api/resources/swagger/update_device_configuration_spec.yaml +++ b/app/api/resources/swagger/update_device_configuration_spec.yaml @@ -15,5 +15,12 @@ parameters: schema: type: object responses: - 204: + 200: description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/DeviceWithConfig' diff --git a/app/devices/api.py b/app/devices/api.py index d7fcba6..23b3540 100644 --- a/app/devices/api.py +++ b/app/devices/api.py @@ -20,6 +20,7 @@ def create_device(name, account_id, device_type=1): device.save() device_association = DeviceAssociation(device.id, account_id) device_association.save() + return device def create_device_type(name): @@ -33,6 +34,7 @@ def create_device_type(name): """ device_type = DeviceType(name) device_type.save() + return device_type 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.save() send_config.delay(device_id, str(configuration_json)) + return device def get_device_configuration(device_id): @@ -173,6 +176,47 @@ def get_device_types(): return DeviceType.get_many() +def parse_raw_json_recording(device_id, json_msg): + """ + Parses raw json recording and creates Recrding object + + :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 + """ + try: + return Recording(device_id=device_id, + record_type=json_msg["record_type"], + record_value=json_msg["record_value"], + recorded_at=json_msg["recorded_at"], + raw_json=json_msg) + except KeyError: + error_type, error_instance, traceback = sys.exc_info() + raise ValueError("JSON parsing failed! Key error: " + + 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 @@ -183,18 +227,6 @@ def create_recording(device_id, raw_json): :type raw_json: json :raises: ValueError if parsing fails or device does not exist """ - def parse_raw_json_recording(device_id, json_msg): - try: - return Recording(device_id=device_id, - record_type=json_msg["record_type"], - record_value=json_msg["record_value"], - recorded_at=json_msg["recorded_at"], - raw_json=json_msg) - except KeyError: - error_type, error_instance, traceback = sys.exc_info() - raise ValueError("JSON parsing failed! Key error: " - + str(error_instance)) - if not Device.exists(id=device_id): raise ValueError("Device does not exist!") diff --git a/app/devices/models.py b/app/devices/models.py index 46cc9ae..341f070 100644 --- a/app/devices/models.py +++ b/app/devices/models.py @@ -8,7 +8,7 @@ from sqlalchemy.dialects.postgresql import JSON class Recording(db.Model): __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, default=db.func.current_timestamp()) received_at = db.Column(db.DateTime, index=True, @@ -115,7 +115,7 @@ class Recording(db.Model): class Device(db.Model): __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, nullable=False, default=db.func.current_timestamp()) @@ -267,7 +267,7 @@ class DeviceAssociation(db.Model): class DeviceType(db.Model): __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) def __init__(self, name): @@ -320,7 +320,7 @@ class DeviceType(db.Model): class AccessLevel(db.Model): __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) def __init__(self, name): From 42c3566ab0d7fc4cb7ca27883e8b9842cf03de8c Mon Sep 17 00:00:00 2001 From: esensar Date: Wed, 24 Oct 2018 22:20:50 +0200 Subject: [PATCH 2/2] Make POST and PUT return created roles/accounts --- app/accounts/api.py | 6 ++++-- app/accounts/models.py | 4 ++-- app/api/resources/account.py | 17 ++++++++--------- .../resources/swagger/create_account_spec.yaml | 9 ++++++++- app/api/resources/swagger/create_role_spec.yaml | 9 ++++++++- .../swagger/update_account_role_spec.yaml | 9 ++++++++- 6 files changed, 38 insertions(+), 16 deletions(-) diff --git a/app/accounts/api.py b/app/accounts/api.py index 95df5c4..b1a1d57 100644 --- a/app/accounts/api.py +++ b/app/accounts/api.py @@ -14,7 +14,7 @@ def create_account(username, email, password): :type username: string :type email: string :type password: string - :returns: Email confirmation token if creation was successful + :returns: Account and Email confirmation token if creation was successful :rtype: string :raises: ValueError if account already exists """ @@ -24,7 +24,7 @@ def create_account(username, email, password): account.save() emailtoken = generate_confirmation_token(account.email) - return emailtoken + return account, emailtoken raise ValueError("Account with given parameters already exists") @@ -58,6 +58,7 @@ def update_account_role(account_id, role_id): acc = Account.get(id=account_id) acc.role_id = role_id acc.save() + return acc def create_role(display_name, permissions): @@ -74,6 +75,7 @@ def create_role(display_name, permissions): """ role = Role(display_name, permissions) role.save() + return role def get_role(role_id): diff --git a/app/accounts/models.py b/app/accounts/models.py index e3fc958..8a47fc3 100644 --- a/app/accounts/models.py +++ b/app/accounts/models.py @@ -7,7 +7,7 @@ from calendar import timegm class Account(db.Model): __tablename__ = 'accounts' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String, index=True, unique=True) password = db.Column(db.String) email = db.Column(db.String, index=True, unique=True) @@ -123,7 +123,7 @@ class Account(db.Model): class Role(db.Model): __tablename__ = 'roles' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(db.Integer, primary_key=True, autoincrement=True) display_name = db.Column(db.String, unique=True) permissions = db.Column(db.ARRAY(db.String)) diff --git a/app/api/resources/account.py b/app/api/resources/account.py index eef7e51..1eae2ea 100644 --- a/app/api/resources/account.py +++ b/app/api/resources/account.py @@ -60,10 +60,9 @@ class RolesResource(ProtectedResource): @use_args(RoleCreationSchema(), locations=('json',)) @swag_from('swagger/create_role_spec.yaml') def post(self, args): - success = accounts.create_role(args['display_name'], - args['permissions']) - if success: - return '', 201 + created_role = accounts.create_role(args['display_name'], + args['permissions']) + return RoleSchema().dump(created_role), 201 @swag_from('swagger/get_roles_spec.yaml') def get(self): @@ -77,9 +76,9 @@ class AccountRoleResource(ProtectedResource): if g.current_account.id == account_id: abort(403, message='You may not change your own roles', status='error') - success = accounts.update_account_role(account_id, args['role_id']) - if success: - return '', 204 + updated_account = accounts.update_account_role( + account_id, args['role_id']) + return UserSchema().dump(updated_account), 200 class AccountListResource(Resource): @@ -87,7 +86,7 @@ class AccountListResource(Resource): @swag_from('swagger/create_account_spec.yaml') def post(self, args): try: - emailtoken = accounts.create_account( + created_account, emailtoken = accounts.create_account( args['username'], args['email'], args['password']) @@ -101,7 +100,7 @@ class AccountListResource(Resource): args['email'], 'Please confirm your email', html) - return '', 201 + return UserSchema().dump(created_account), 201 except ValueError: abort(422, message='Account already exists', status='error') diff --git a/app/api/resources/swagger/create_account_spec.yaml b/app/api/resources/swagger/create_account_spec.yaml index 0d94b40..48167ef 100644 --- a/app/api/resources/swagger/create_account_spec.yaml +++ b/app/api/resources/swagger/create_account_spec.yaml @@ -14,7 +14,14 @@ parameters: security: [] responses: 201: - description: Successful creation + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/User' 422: description: Account already exists schema: diff --git a/app/api/resources/swagger/create_role_spec.yaml b/app/api/resources/swagger/create_role_spec.yaml index e5e9fd5..7a7a3f9 100644 --- a/app/api/resources/swagger/create_role_spec.yaml +++ b/app/api/resources/swagger/create_role_spec.yaml @@ -12,4 +12,11 @@ parameters: $ref: '#/definitions/Role' responses: 201: - description: Successful creation + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/Role' diff --git a/app/api/resources/swagger/update_account_role_spec.yaml b/app/api/resources/swagger/update_account_role_spec.yaml index 26de064..b8d85b0 100644 --- a/app/api/resources/swagger/update_account_role_spec.yaml +++ b/app/api/resources/swagger/update_account_role_spec.yaml @@ -20,5 +20,12 @@ parameters: role_id: type: integer responses: - 204: + 200: description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/User'