2018-12-13 21:45:33 +01:00
|
|
|
import pprint
|
2018-12-29 00:27:19 +01:00
|
|
|
from abc import ABCMeta
|
2018-12-13 21:45:33 +01:00
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.api import *
|
2019-01-02 21:20:44 +01:00
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
|
2018-12-10 22:13:28 +01:00
|
|
|
class CRUDController(metaclass=ABCMeta):
|
2018-11-01 19:50:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
cache = {}
|
|
|
|
caching = True
|
2019-01-02 21:20:44 +01:00
|
|
|
name = None
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2018-11-01 21:25:42 +01:00
|
|
|
def __init__(self, cbpi):
|
|
|
|
self.cbpi = cbpi
|
2018-11-01 19:50:04 +01:00
|
|
|
self.cache = {}
|
|
|
|
|
|
|
|
async def init(self):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
2019-01-02 21:20:44 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
if self.caching is True:
|
|
|
|
self.cache = await self.model.get_all()
|
|
|
|
|
|
|
|
async def get_all(self, force_db_update=False):
|
2018-11-18 15:40:10 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param force_db_update:
|
|
|
|
:return:
|
|
|
|
'''
|
2018-11-01 19:50:04 +01:00
|
|
|
if self.caching is False or force_db_update:
|
|
|
|
self.cache = await self.model.get_all()
|
|
|
|
|
|
|
|
return self.cache
|
|
|
|
|
|
|
|
async def get_one(self, id):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param id:
|
|
|
|
:return:
|
|
|
|
'''
|
2019-01-02 21:20:44 +01:00
|
|
|
if id not in self.cache:
|
|
|
|
raise CBPiException("%s with id %s not found" % (self.name,id))
|
2018-11-01 19:50:04 +01:00
|
|
|
return self.cache.get(id)
|
|
|
|
|
|
|
|
async def _pre_add_callback(self, data):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
'''
|
2018-11-01 19:50:04 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
async def _post_add_callback(self, m):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param m:
|
|
|
|
:return:
|
|
|
|
'''
|
2018-11-01 19:50:04 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
async def add(self, **data):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
'''
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
await self._pre_add_callback(data)
|
2019-01-02 21:20:44 +01:00
|
|
|
print("INSSERT ADD", data)
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
m = await self.model.insert(**data)
|
2019-01-02 21:20:44 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
self.cache[m.id] = m
|
2018-12-13 21:45:33 +01:00
|
|
|
await self._post_add_callback(m)
|
|
|
|
|
|
|
|
await self.cbpi.bus.fire(topic="actor/%s/added" % m.id, actor=m)
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
return m
|
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
async def _pre_update_callback(self, m):
|
2018-11-01 19:50:04 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
async def _post_update_callback(self, m):
|
|
|
|
pass
|
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
async def update(self, id, data):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param id:
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
'''
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
id = int(id)
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
if id not in self.cache:
|
|
|
|
raise CBPiException("%s with id %s not found" % (self.name,id))
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
data["id"] = id
|
2018-11-16 20:35:59 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
try:
|
2018-11-16 20:35:59 +01:00
|
|
|
### DELETE INSTANCE BEFORE UPDATE
|
2018-11-01 19:50:04 +01:00
|
|
|
del data["instance"]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if self.caching is True:
|
2018-11-16 20:35:59 +01:00
|
|
|
await self._pre_update_callback(self.cache[id])
|
|
|
|
self.cache[id].__dict__.update(**data)
|
|
|
|
m = await self.model.update(**self.cache[id].__dict__)
|
|
|
|
await self._post_update_callback(m)
|
|
|
|
else:
|
|
|
|
m = await self.model.update(**data)
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
return m
|
|
|
|
|
2018-11-16 20:35:59 +01:00
|
|
|
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
async def _pre_delete_callback(self, m):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param m:
|
|
|
|
:return:
|
|
|
|
'''
|
2018-11-01 19:50:04 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
async def _post_delete_callback(self, id):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param id:
|
|
|
|
:return:
|
|
|
|
'''
|
2018-11-01 19:50:04 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
async def delete(self, id):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:param id:
|
|
|
|
:return:
|
|
|
|
'''
|
2018-12-13 21:45:33 +01:00
|
|
|
|
|
|
|
if id not in self.cache:
|
2019-01-02 21:20:44 +01:00
|
|
|
raise CBPiException("%s with id %s not found" % (self.name,id))
|
2018-12-13 21:45:33 +01:00
|
|
|
|
2019-01-02 21:20:44 +01:00
|
|
|
await self._pre_delete_callback(id)
|
2018-11-01 19:50:04 +01:00
|
|
|
m = await self.model.delete(id)
|
|
|
|
await self._post_delete_callback(id)
|
|
|
|
try:
|
|
|
|
if self.caching is True:
|
2018-12-13 21:45:33 +01:00
|
|
|
del self.cache[int(id)]
|
2018-11-01 19:50:04 +01:00
|
|
|
except Exception as e:
|
|
|
|
pass
|
2019-01-02 00:48:36 +01:00
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
await self.cbpi.bus.fire(topic="actor/%s/deleted" % id, id=id)
|
2018-11-01 19:50:04 +01:00
|
|
|
|
|
|
|
async def delete_all(self):
|
2018-11-29 21:59:08 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
2018-11-01 19:50:04 +01:00
|
|
|
self.model.delete_all()
|
|
|
|
if self.caching is True:
|
|
|
|
self.cache = {}
|
2018-11-01 21:25:42 +01:00
|
|
|
#self.cbpi.push_ws("DELETE_ALL_%s" % self.key, None)
|