university-final-iot-backend/app/dashboards/api.py

189 lines
4.6 KiB
Python
Raw Normal View History

2018-10-22 21:54:31 +00:00
from .models import Dashboard, DashboardWidget
2018-09-19 23:42:37 +00:00
# Public interface
2018-10-22 19:29:20 +00:00
def create_dashboard(dashboard_data, name, account_id):
2018-09-19 23:42:37 +00:00
"""
Tries to create dashboard with given parameters
:param dashboard_data: JSON dashboard data
:param account_id: Id of owner of this dashboard
2018-10-22 19:29:20 +00:00
:param name: Name of the dashboard
2018-09-19 23:42:37 +00:00
:type name: JSON
:type account_id: int
2018-10-22 19:29:20 +00:00
:type name: string
2018-09-19 23:42:37 +00:00
:returns: True if dashboard is successfully created
:rtype: Boolean
"""
2018-10-22 19:29:20 +00:00
dashboard = Dashboard(account_id, dashboard_data, name)
2018-09-19 23:42:37 +00:00
dashboard.save()
return dashboard
2018-09-19 23:42:37 +00:00
def get_dashboard(dashboard_id):
"""
Tries to fetch dashboard with given id
:param dashboard_id: Id of requested dashboard
:type name: int
:returns: Dashboard object
:rtype: Dashboard
"""
return Dashboard.get(id=dashboard_id)
2018-10-15 17:51:51 +00:00
def patch_dashboard(account_id, dashboard_id,
2018-10-22 19:29:20 +00:00
dashboard_data=None, active=None, name=None):
2018-09-19 23:42:37 +00:00
"""
Tries to update dashboard with given parameters
:param dashboard_data: JSON dashboard data
:param dashboard_id: Id of the dashboard
:type name: JSON
:type dashboard_id: int
"""
2018-10-22 19:29:20 +00:00
dashboard = Dashboard.get(id=dashboard_id)
2018-10-15 17:51:51 +00:00
if dashboard_data is not None:
dashboard.dashboard_data = dashboard_data
2018-10-22 19:29:20 +00:00
if name is not None:
dashboard.name = name
dashboard.save()
2018-10-15 17:51:51 +00:00
if active:
set_active_dashboard(account_id, dashboard_id)
dashboard.active = True
return dashboard
2018-09-19 23:42:37 +00:00
2018-10-10 20:19:13 +00:00
def delete_dashboard(dashboard_id):
"""
Tries to delete dashboard with given id
:param dashboard_id: Id of requested dashboard
:type name: int
"""
dashboard = Dashboard.get(id=dashboard_id)
dashboard.delete()
2018-10-15 17:51:51 +00:00
def set_active_dashboard(account_id, dashboard_id):
"""
Tries to set given dashboard as active
:param dashboard_id: Id of requested dashboard
:type name: int
:param dashboard_id: Id of owner account
:type name: int
"""
Dashboard.deactivate_all_for_user(account_id)
dashboard = Dashboard.get(id=dashboard_id)
dashboard.active = True
dashboard.save()
def get_active_dashboard(account_id):
"""
Tries to fetch active dashboard owned by account with given id
:param account_id: Id of owner account
:type name: int
:returns: active Dashboard object
:rtype: Dashboard
"""
return Dashboard.get(account_id=account_id, active=True)
def get_dashboards(account_id, active):
2018-09-19 23:42:37 +00:00
"""
Tries to fetch dashboards owned by account with given id
:param account_id: Id of owner account
:type name: int
2018-10-15 17:51:51 +00:00
:param active: Whether to filter active only
:type name: bool
2018-09-19 23:42:37 +00:00
:returns: Dashboard list
:rtype: List of Dashboard
"""
2018-10-15 17:51:51 +00:00
return Dashboard.get_many_filtered(account_id=account_id, active=active)
2018-10-22 21:54:31 +00:00
2018-11-03 14:47:38 +00:00
def create_widget(dashboard_id, device_id, name, height, width, x, y,
2018-10-22 21:54:31 +00:00
chart_type, filters):
"""
Tries to create a dashboard widget
"""
2018-11-03 14:47:38 +00:00
widget = DashboardWidget(dashboard_id, device_id,
name, height, width, x, y,
2018-10-22 21:54:31 +00:00
chart_type, filters)
widget.save()
2018-10-24 10:12:45 +00:00
return widget
2018-10-22 21:54:31 +00:00
def delete_widget(widget_id):
"""
Tries to delete widget with given id
:param widget_id: Id of requested widget
:type name: int
"""
widget = DashboardWidget.get(id=widget_id)
widget.delete()
def get_widgets(dashboard_id):
"""
Tries to fetch widgets of a dashboard with dashboard_id
:param dashboard_id: Id of owner dashboard
:type name: int
:returns: Widget list
:rtype: List of Widgets
"""
return DashboardWidget.get_many_for_dashboard(dashboard_id)
def get_widget(widget_id):
"""
Tries to fetch widget with given id
:param widget_id: Id of requested dashboard
:type name: int
:returns: Widget object
:rtype: Widget
"""
return DashboardWidget.get(id=widget_id)
2018-11-03 14:47:38 +00:00
def patch_widget(widget_id, device_id=None, name=None, height=None, width=None,
2018-10-22 21:54:31 +00:00
x=None, y=None, chart_type=None, filters=None):
"""
Tries to update widget with given parameters
"""
widget = DashboardWidget.get(id=widget_id)
if device_id is not None:
widget.device_id = device_id
if height is not None:
widget.height = height
2018-11-03 14:47:38 +00:00
if name is not None:
widget.name = name
2018-10-22 21:54:31 +00:00
if width is not None:
widget.width = width
if x is not None:
widget.x = x
if y is not None:
widget.y = y
if chart_type is not None:
widget.chart_type = chart_type
if filters is not None:
widget.filters = filters
widget.save()
return widget