Fix datetime fields in selection for recordings

develop
Ensar Sarajčić 2018-11-03 14:53:26 +01:00
parent 280419198c
commit c0f579ef2c
3 changed files with 21 additions and 6 deletions

1
.python-version 100644
View File

@ -0,0 +1 @@
3.6.6

View File

@ -7,7 +7,7 @@ from .models import (Device,
DeviceType, DeviceType,
AccessLevel) AccessLevel)
from itsdangerous import URLSafeSerializer from itsdangerous import URLSafeSerializer
from app.core import app from app.core import app, db
from app.jsonql import api as jsonql from app.jsonql import api as jsonql
@ -335,7 +335,7 @@ def run_custom_query(device_id, request):
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!")
def recording_field_provider(name): def recording_field_provider(name, formatted=False):
if name == 'record_value': if name == 'record_value':
return Recording.record_value return Recording.record_value
if name == 'record_type': if name == 'record_type':
@ -343,8 +343,14 @@ def run_custom_query(device_id, request):
if name == 'device_id': if name == 'device_id':
return Recording.device_id return Recording.device_id
if name == 'recorded_at': if name == 'recorded_at':
if formatted:
return db.func.to_char(
Recording.recorded_at, 'YYYY-MM-DD"T"HH24:MI:SSOF')
return Recording.recorded_at return Recording.recorded_at
if name == 'received_at': if name == 'received_at':
if formatted:
return db.func.to_char(
Recording.received_at, 'YYYY-MM-DD"T"HH24:MI:SSOF')
return Recording.received_at return Recording.received_at
resulting_query = jsonql.run_query_on(Recording.query.with_entities(), resulting_query = jsonql.run_query_on(Recording.query.with_entities(),

View File

@ -7,11 +7,20 @@ ORDERS = ['asc', 'desc']
def run_query_on(query_object, field_provider, **kwargs): def run_query_on(query_object, field_provider, **kwargs):
"""
Generates a query for target object based on query provided as kwargs
:param query_object: Initial query object as returned by SQLAlchemy for
target table
:type query_object: Query
:param field_provider: Function which provides fields based on name, with
optional parameter formatted which returns the field formatted using sql
functions
:type field_provider: func(col_name:String, formatted:Boolean)
"""
selections, filters, groups, orderings = validate_selections(**kwargs) selections, filters, groups, orderings = validate_selections(**kwargs)
entities = [] entities = []
print('Starting with args: ' + str(kwargs))
if selections is not None: if selections is not None:
if groups is not None: if groups is not None:
for group in groups.keys(): for group in groups.keys():
@ -26,9 +35,8 @@ def run_query_on(query_object, field_provider, **kwargs):
for selection in selections.keys(): for selection in selections.keys():
entities.append(get_column(selections[selection], entities.append(get_column(selections[selection],
field_provider(selection)).label(selection)) field_provider(selection, True)).label(selection))
print('New entities: ' + str(entities))
query_object = query_object.with_entities(*entities) query_object = query_object.with_entities(*entities)
if filters is not None: if filters is not None: