2019-01-02 21:20:44 +01:00
|
|
|
import logging
|
2021-01-09 15:20:56 +01:00
|
|
|
import json
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.controller.crud_controller import CRUDController
|
|
|
|
from cbpi.database.model import DashboardModel, DashboardContentModel
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DashboardController(CRUDController):
|
|
|
|
|
|
|
|
model = DashboardModel
|
|
|
|
name = "Dashboard"
|
|
|
|
|
|
|
|
def __init__(self, cbpi):
|
2019-08-16 21:36:55 +02:00
|
|
|
self.caching = False
|
2019-01-02 21:20:44 +01:00
|
|
|
super(DashboardController, self).__init__(cbpi)
|
|
|
|
self.cbpi = cbpi
|
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
self.cbpi.register(self)
|
|
|
|
|
2019-01-07 22:05:52 +01:00
|
|
|
def get_state(self):
|
|
|
|
return dict(items=self.cache)
|
|
|
|
|
2019-01-02 21:20:44 +01:00
|
|
|
async def get_content(self, dashboard_id):
|
2021-01-09 15:20:56 +01:00
|
|
|
with open('./config/dashboard/cbpi_dashboard_%s.json' % dashboard_id) as json_file:
|
|
|
|
data = json.load(json_file)
|
|
|
|
return data
|
2019-01-02 21:20:44 +01:00
|
|
|
|
2021-01-09 15:20:56 +01:00
|
|
|
async def add_content(self, dashboard_id, data):
|
|
|
|
with open('./config/dashboard/cbpi_dashboard_%s.json' % dashboard_id, 'w') as outfile:
|
|
|
|
json.dump(data, outfile, indent=4, sort_keys=True)
|
|
|
|
print(data)
|
|
|
|
return {"status": "OK"}
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
async def delete_content(self, content_id):
|
|
|
|
await DashboardContentModel.delete(content_id)
|
|
|
|
|
2021-01-09 15:20:56 +01:00
|
|
|
|
2019-08-16 21:36:55 +02:00
|
|
|
|
|
|
|
async def delete_dashboard(self, dashboard_id):
|
|
|
|
await DashboardContentModel.delete_by_dashboard_id(dashboard_id)
|
|
|
|
await self.model.delete(dashboard_id)
|