Merged in develop (pull request #62)

Version 0.4.6 release
master
Ensar Sarajcic 2018-12-11 19:19:00 +00:00
commit 0f8b8c51df
6 changed files with 57 additions and 1 deletions

View File

@ -19,6 +19,7 @@ def add_resources():
from .resources.token import TokenResource, ValidateTokenResource from .resources.token import TokenResource, ValidateTokenResource
from .resources.device import (DeviceResource, from .resources.device import (DeviceResource,
DeviceRecordingResource, DeviceRecordingResource,
DeviceLatestRecordingResource,
DeviceRecordingQueryResource, DeviceRecordingQueryResource,
DeviceListResource, DeviceListResource,
DeviceTypeResource, DeviceTypeResource,
@ -49,6 +50,8 @@ def add_resources():
api.add_resource(DeviceResource, '/v1/devices/<int:device_id>') api.add_resource(DeviceResource, '/v1/devices/<int:device_id>')
api.add_resource(DeviceRecordingResource, api.add_resource(DeviceRecordingResource,
'/v1/devices/<int:device_id>/recordings') '/v1/devices/<int:device_id>/recordings')
api.add_resource(DeviceLatestRecordingResource,
'/v1/devices/<int:device_id>/recordings/latest')
api.add_resource(DeviceRecordingQueryResource, api.add_resource(DeviceRecordingQueryResource,
'/v1/devices/<int:device_id>/recordings/jsonql') '/v1/devices/<int:device_id>/recordings/jsonql')
api.add_resource(DeviceListResource, '/v1/devices') api.add_resource(DeviceListResource, '/v1/devices')

View File

@ -129,6 +129,14 @@ class DeviceRecordingResource(ProtectedResource):
return RecordingsSchema().dump(created_recording), 201 return RecordingsSchema().dump(created_recording), 201
class DeviceLatestRecordingResource(ProtectedResource):
@swag_from('swagger/get_latest_device_recording_spec.yaml')
def get(self, device_id):
validate_device_ownership(device_id)
return RecordingsSchema().dump(
devices.get_latest_device_recording(device_id)), 200
class DeviceRecordingQueryResource(ProtectedResource): class DeviceRecordingQueryResource(ProtectedResource):
@use_args(RecordingsQuerySchema(), locations=('json',)) @use_args(RecordingsQuerySchema(), locations=('json',))
@swag_from('swagger/create_device_recording_query_spec.yaml') @swag_from('swagger/create_device_recording_query_spec.yaml')

View File

@ -0,0 +1,21 @@
Gets latest recording for given device
---
tags:
- Device
- Recording
parameters:
- in: path
name: device_id
required: true
type: integer
description: Id of the device
responses:
200:
description: Success
schema:
type: object
required:
- content
properties:
content:
$ref: '#/definitions/Recording'

View File

@ -142,6 +142,23 @@ def get_device_recordings_filtered(device_id, record_type=None,
start_date, end_date) start_date, end_date)
def get_latest_device_recording(device_id):
"""
Tries to get most recent recording for device with given parameters. Raises
error on failure
:param device_id: Id of device
:type device_id: int
:returns: Single recording (last recording)
:rtpe: Recording
:raises: ValueError if device does not exist
"""
if not Device.exists(id=device_id):
raise NotPresentError("Device with id %s does not exist" % device_id)
return Recording.get_latest(device_id)
def get_device(device_id): def get_device(device_id):
""" """
Tries to get device with given parameters. Raises error on failure Tries to get device with given parameters. Raises error on failure

View File

@ -108,6 +108,13 @@ class Recording(db.Model):
""" """
return Recording.query.filter_by(**kwargs).first_or_404() return Recording.query.filter_by(**kwargs).first_or_404()
@staticmethod
def get_latest(device_id):
"""
Get latest recording for device with id device_id
"""
return Recording.query.order_by('recorded_at desc').first_or_404()
def __repr__(self): def __repr__(self):
return '<Recording (value=%s, recorded_at=%s)>' % ( return '<Recording (value=%s, recorded_at=%s)>' % (
self.record_value, self.recorded_at) self.record_value, self.recorded_at)

View File

@ -2,7 +2,7 @@ import os
# App configuration # App configuration
DEBUG = os.environ['DEBUG'] DEBUG = os.environ['DEBUG']
APP_VERSION = '0.4.5' APP_VERSION = '0.4.6'
APP_RELEASE_VERSION_STRING = (os.environ.get('HEROKU_RELEASE_VERSION') APP_RELEASE_VERSION_STRING = (os.environ.get('HEROKU_RELEASE_VERSION')
or 'Unknown') or 'Unknown')