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

104 lines
2.7 KiB
Python
Raw Normal View History

2018-09-19 23:42:37 +00:00
from .models import Dashboard
# 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()
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)
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)