Files
CPY_DevController/Manager/product_manager.py
T

80 lines
2.1 KiB
Python

from models import Product, Session
import uuid
from config import *
class ProductManager:
def __init__(self, parent=None):
super().__init__()
self.cur_product_id = ""
config = read_config()
if config.get('product_id', '') == "":
try:
products = self.getInfo("all")
if products == []:
print("no product found in database")
else:
config.update(product_id = products[0]["id"])
save_config(config)
except:
pass
def setCurProductId(self, id):
self.cur_product_id = id
config = read_config()
if config.get('product_id', '') == id:
return
config.update(product_id = id)
save_config(config)
def getCurProductId(self):
return self.cur_product_id
def getCurrentProduct(self):
return self.getInfo(self.cur_product_id)
def create(self, info):
data = {
"id": str(uuid.uuid4()),
"info": info
}
ok, res = Session.addByClass(Product, data)
if ok:
return str(res["id"])
return ""
def update(self, id, info):
data = {"info": info}
ok,res = Session.updateById(Product, id, data)
return ok
def delete(self, id):
if isinstance(id, str):
ok,res = Session.deleteById(Product, id)
return ok
else:
ok,res = Session.deleteByIds(Product, id)
return ok
def getInfos(self, ids):
ok,res = Session.queryByIds(Product, ids)
if ok:
return res
else:
return []
def getInfo(self, id = "all"):
if id == "all":
ok,res = Session.queryByAll(Product)
if ok:
return res
else:
return []
else:
ok,res = Session.queryById(Product, id)
if ok:
return res
else:
return None
productManager = ProductManager()