Files
YMS/yms/lookup.py
T

345 lines
9.5 KiB
Python

# SPDX-License-Identifier: WTFPL
from enum import IntEnum
"""
Dropdowns, Database enums and Table numbers
dropdowns contents can change
enums are static
TBD
- language
- dateformat
- dimensions (measurement)
"""
class Dropdown(IntEnum):
VESSEL_TYPE = 1
COMPANY_TYPE = 2
STORAGE_TYPE = 3
EQUIPMENT_CATEGORY = 4
PROVISION_CATEGORY = 5
PROVISION_UNIT = 6
FUSE_TYPE = 7
UNIT = 8
BOX_TYPE = 9
FUSE_BLOCK = 10
CABLE_TYPE = 11
CABLE_TECHNAME = 12
INVENTORY_CATEGORY = 13
BBN_VESSELDATA = 14
MEASUREMENT_CATEGORY = 15
DropdownDeps = {
Dropdown.VESSEL_TYPE: (
('vessel', 'vtype')
),
Dropdown.COMPANY_TYPE: (
('company', 'comptype'),
('company', 'comptype2')
),
Dropdown.STORAGE_TYPE: (
('storage', 'stype')
),
Dropdown.EQUIPMENT_CATEGORY: (
('equipment', 'ecat')
),
Dropdown.PROVISION_CATEGORY: (
('provisions', 'provcat')
),
Dropdown.PROVISION_UNIT: (
('provisions', 'unit')
),
Dropdown.FUSE_TYPE: (
('fuse', 'ftype')
),
Dropdown.UNIT: (
('measurement', 'unit')
),
Dropdown.BOX_TYPE: (
('box', 'boxtype')
),
Dropdown.FUSE_BLOCK: (
('fuse', 'location')
),
Dropdown.CABLE_TYPE: (
('cable', 'cabletype')
),
Dropdown.CABLE_TECHNAME: (
('cable', 'techname')
),
Dropdown.INVENTORY_CATEGORY: (
('inventory', 'invcat')
),
Dropdown.BBN_VESSELDATA: (
#unused
),
Dropdown.MEASUREMENT_CATEGORY: (
('measurement', 'mcat')
)
}
class Table(IntEnum):
VESSEL = (1, "base")
USERS = (2, "base")
COMPANIES = (3, "companies")
STORAGE = (4, "storage")
BOXES = (5, "boxes")
EQUIPMENT = (6, "equipment")
INVENTORY = (7, "inventory")
PROJECTS = (8, "projects")
CABLES = (9, "cables")
FUSES = (10, "fuses")
TAGS = (11, "tags")
def __new__(cls, value, module):
obj = int.__new__(cls, value)
obj._value_ = value
obj.module = module
return obj
class Lookup:
def __init__(self, db):
self.db = db
self._enums = {}
self._dropdowns = {}
self._revision_ts = {} # tracking list revisions
def load(self):
self._load_enums()
self._load_dropdowns()
def _load_enums(self):
"""
enums and sets
translation via gettext from db schema
"""
db_enums = {
'cable.cablecond': ('cable', 'cablecond'),
'checkitem.checkresult': ('checkitem', 'checkresult'),
'checklist.clstate': ('checklist', 'clstate'),
'company.flags': ('company', 'flags'),
'docref.reftype': ('docref', 'reftype'),
'document.doctype': ('document', 'doctype'),
'document.roleaccess': ('document', 'roleaccess'),
'dropdown.flags': ('dropdown', 'flags'),
'equipment.econd': ('equipment', 'econd'),
'equipment.flags': ('equipment', 'flags'),
'inventory.conttype': ('inventory', 'conttype'),
'inventory.invcond': ('inventory', 'invcond'),
'inventory.flags': ('inventory', 'flags'),
'maintenance.maintstate': ('maintenance', 'maintstate'),
'maintenance.plan_precision': ('maintenance', 'plan_precision'),
'measurement.accuracy': ('measurement', 'accuracy'),
'note.notetype': ('note', 'notetype'),
'project.projstate': ('project', 'projstate'),
'signalk.datatype': ('signalk', 'datatype'),
'signalk.objtype': ('signalk', 'objtype'),
'storage.capaunit': ('storage', 'capaunit'),
'tagref.objtype': ('tagref', 'objtype'),
'task.priority': ('task', 'priority'),
'task.taskstate': ('task', 'taskstate'),
'user.role': ('user', 'role'),
'vessel.htype': ('vessel', 'htype')
}
for key, (table, column) in db_enums.items():
self._enums[key] = self.db.load_enum(table, column)
def _load_dropdowns(self):
self._dropdowns.clear()
sql = "SELECT ddval FROM dropdown WHERE ddid=0 AND ddval>0"
# Step 1: init all lists as empty
self.db.cur.execute(sql)
for ddval in self.db.cur.fetchall():
self._dropdowns[ddval[0]] = {}
# Step 2: fill values where defined
sql = (
"SELECT ddid, ddval, ddtext "
"FROM dropdown "
"WHERE ddid>0 "
"ORDER BY ddid, sort, ddval"
)
self.db.cur.execute(sql)
for ddid, ddval, ddtext in self.db.cur.fetchall():
if ddid not in self._dropdowns:
# should really not happen!
print("unknown dropdown list id found!")
self._dropdowns[ddid] = {}
self._dropdowns[ddid][ddval] = ddtext
def reload_dropdown(self, ddid):
self._dropdowns[ddid] = {}
sql = (
"SELECT ddval, ddtext "
"FROM dropdown "
"WHERE ddid=%s "
"ORDER BY sort, ddval"
)
self.db.cur.execute(sql)
for ddval, ddtext in self.db.cur.fetchall():
self._dropdowns[ddid][ddval] = ddtext
def enum(self, name):
return self._enums[name]
def dropdown(self, ddid):
return self._dropdowns[ddid]
def iter_dropdowns(self):
yield from self._dropdowns.keys()
def user(self):
self.db.cur.execute("SELECT userid, displayname FROM user ORDER BY displayname")
return dict(self.db.cur.fetchall())
def vessel(self):
vessellist = {}
self.db.dcur.execute("SELECT vid, vesselname, model FROM vessel ORDER BY vid")
for row in self.db.dcur.fetchall():
vessellist[row['vid']] = (
row['vesselname'] + " - " + row['model'] if row['model']
else row['vesselname']
)
return vessellist
def company(self, comptype):
sql = (
"SELECT compid, compname "
"FROM company "
"WHERE comptype=%(type)s OR comptype2=%(type)s "
"ORDER BY compname"
)
self.db.cur.execute(sql, {"type": comptype})
return dict(self.db.cur.fetchall())
def storage(self, vid):
storagelist = {}
sql = (
"SELECT sid, sname, CONCAT(x, ',', y, ',', z) AS position, remarks "
"FROM storage "
"WHERE vid=%s "
"ORDER BY sname"
)
self.db.cur.execute(sql, (vid,))
for row in self.db.cur.fetchall():
if row[3]:
storagelist[row[0]] = "{} ({}) - {}".format(row[1], row[2], row[3])
else:
storagelist[row[0]] = "{} ({})".format(row[1], row[2])
return storagelist
def box(self, vid):
boxlist = {}
sql = (
"SELECT b.boxid, b.label, b.content "
"FROM box AS b LEFT JOIN storage AS s USING (sid) "
"WHERE s.vid=%s OR s.vid IS NULL "
"ORDER BY b.label"
)
self.db.cur.execute(sql, (vid,))
for row in self.db.cur.fetchall():
if row[2]:
boxlist[row[0]] = "{} - {}".format(row[1], row[2])
else:
boxlist[row[0]] = row[1]
return boxlist
def equipment(self, vid):
sql = (
"SELECT eid, ename "
"FROM equipment "
"WHERE vid=%s "
"ORDER BY ename"
)
self.db.cur.execute(sql, (vid,))
return dict(self.db.cur.fetchall())
def inventory(self, vid):
sql = (
"SELECT invid, invname "
"FROM inventory "
"WHERE vid=%s "
"ORDER BY invname"
)
self.db.cur.execute(sql, (vid,))
return dict(self.db.cur.fetchall())
def cable(self, vid):
sql = (
"SELECT cableid, cablename "
"FROM cable "
"WHERE vid=%s "
"ORDER BY cablename"
)
self.db.cur.execute(sql, (vid,))
return dict(self.db.cur.fetchall())
def fuse(self, vid):
sql = (
"SELECT fuseid, CONCAT('F', fnumber, ' - ', description) "
"FROM fuse "
"WHERE vid=%s "
"ORDER BY fnumber"
)
self.db.cur.execute(sql, (vid,))
return dict(self.db.cur.fetchall())
def project(self, vid):
sql = (
"SELECT projid, projname "
"FROM project "
"WHERE vid=%s "
"ORDER BY projname"
)
self.db.cur.execute(sql, (vid,))
return dict(self.db.cur.fetchall())
def refresh(self):
# TODO ugly hack please improve
self._load_dropdowns()
class ListDefinition:
pass
"""
EQUIPMENT_LIST = ListDefinition(
name="equipment",
query=
"SELECT
e.eid,
e.ename,
c.name AS manufacturer,
e.model,
e.ecat,
e.weight
FROM equipment e
LEFT JOIN company c
ON c.cid = e.manufacturer"
,
columns=[
Column("ename", _("Name")),
Column("manufacturer", _("Manufacturer")),
Column(
"model",
_("Model")
),
Column(
"ecat",
_("Category"),
formatter=dropdown(Dropdown.EQUIPMENT_CATEGORY)
),
Column(
"weight",
_("Weight"),
formatter=weight()
),
]
)
"""