Native gui usable for data editing. Lots of fixes and improvenemts

This commit is contained in:
thooge committed 2026-11-11 18:58:02 +01:00
1 parent a6abdaa614
commit 09387280fc
52 files changed
+1579 -294

No files matched your search

+4 -2
View File
@@ -7,8 +7,10 @@ class Box(DbRecord):
table = "box"
pk_field = "boxid"
fields = ('boxid', 'sid', 'boxtype', 'parent', 'label', 'color', 'content',
'wx', 'wy', 'wz', 'weight', 'weight_net', 'price', 'remarks')
pk_is_auto = True
fields = ('boxid', 'sid', 'boxtype', 'parent', 'label', 'color',
'content', 'wx', 'wy', 'wz', 'weight', 'weight_net', 'price',
'remarks')
def __init__(self, db, boxid=None):
super().__init__(db, boxid)
+4 -3
View File
@@ -7,9 +7,10 @@ class Cable(DbRecord):
table = "cable"
pk_field = "cableid"
fields = ('vid', 'cablename', 'cabletype', 'techname', 'length',
'conductors', 'diameter', 'xsection', 'weight', 'color', 'supplier',
'manufacturer', 'cablecond', 'remarks')
pk_is_auto = True
fields = ('cableid', 'vid', 'cablename', 'cabletype', 'techname',
'length', 'conductors', 'diameter', 'xsection', 'weight', 'color',
'supplier', 'manufacturer', 'cablecond', 'remarks')
def __init__(self, db, cableid=None):
super().__init__(db, cableid)
+4 -3
View File
@@ -13,9 +13,10 @@ class Company(DbRecord):
table = "company"
pk_field = "compid"
fields = ('compid', 'compname', 'comptype', 'comptype2', 'shortname',
'street', 'zip', 'city', 'country', 'contact', 'phone', 'email',
'web', 'customerno', 'contractno', 'remarks', 'flags' )
pk_is_auto = True
fields = ('compid', 'compname', 'comptype', 'comptype2',
'shortname', 'street', 'zip', 'city', 'country', 'contact', 'phone',
'email', 'web', 'customerno', 'contractno', 'remarks', 'flags')
def __init__(self, db, compid=None):
super().__init__(db, compid)
+34 -14
View File
@@ -3,20 +3,24 @@
"""
No commits here: Has to be done elsewhere to support transactions!
Fields has to be a tuple to have fixed order which simplifies debugging
TODO primary keys can be multiple fields, stored as tuple
do not include pk auto fields in inserts and updates!
"""
from datetime import datetime
class DbRecord:
def __init__(self, db, record_id=None):
if not hasattr(self, "table"):
raise TypeError("DbRecord subclass must define 'table'")
if not hasattr(self, "pk_field"):
raise TypeError("DbRecord subclass must define 'pk_field'")
if not hasattr(self, "fields"):
raise TypeError("DbRecord subclass must define 'fields'")
REQUIRED = ('table', 'pk_field', 'pk_is_auto', 'fields')
def __init_subclass__(cls, **kwargs):
for variable in DbRecord.REQUIRED:
if variable not in cls.__dict__:
raise TypeError(f"{cls.__name__} subclass must define '{variable}'")
def __init__(self, db, record_id=None):
self.db = db
self.cur = db.cur
self.dcur = db.dcur
@@ -25,6 +29,7 @@ class DbRecord:
self._title = _("DbRecord({})") # for pretty print
self.pk_value = None
if record_id is not None:
self.pk_value = int(record_id)
self.load()
@@ -81,7 +86,14 @@ class DbRecord:
def _update(self):
sqlfield = []
values = []
for field in self.fields:
if self.pk_is_auto:
if type(self.pk_field) == tuple:
flist = set(self.fields) - set(self.pk_field)
else:
flist = set(self.fields) - {self.pk_field}
else:
flist = self.fields
for field in flist:
newdata = getattr(self, field)
if self.data[field] != newdata:
sqlfield.append("{}=%s".format(field))
@@ -94,7 +106,7 @@ class DbRecord:
)
values.append(self.pk_value)
try:
self.dcur.execute(sql, values)
self.cur.execute(sql, values)
except Exception as e:
print(e)
print(sql)
@@ -106,28 +118,36 @@ class DbRecord:
return True
def _insert(self):
if self.pk_is_auto:
if type(self.pk_field) == tuple:
flist = set(self.fields) - set(self.pk_field)
else:
flist = set(self.fields) - {self.pk_field}
else:
flist = self.fields
sql = (
"INSERT INTO {} ({}) "
"VALUES ({})"
).format(
self.table,
','.join(self.fields),
','.join(["%s"] * len(self.fields))
','.join(flist),
','.join(["%s"] * len(flist))
)
values = []
for field in self.fields:
for field in flist:
newdata = getattr(self, field)
values.append(newdata)
self.data[field] = newdata
try:
self.dcur.execute(sql, values)
self.cur.execute(sql, values)
except Exception as e:
print(e)
print(sql)
print(values)
self.db.error = e
return False
self.pk_value = self.dcur.lastrowid
self.pk_value = self.cur.lastrowid
setattr(self, self.pk_field, self.pk_value)
return True
def delete(self, record_id=None):
+2 -1
View File
@@ -6,7 +6,8 @@ class Document(DbRecord):
table = "document"
pk_field = "docid"
fields = ('doctype', 'mimetype', 'filename', 'extension',
pk_is_auto = True
fields = ('docid', 'doctype', 'mimetype', 'filename', 'extension',
'hash', 'docsize', 'doctime', 'docdate', 'title', 'pages', 'remarks',
'roleaccess')
+4 -3
View File
@@ -7,9 +7,10 @@ class Equipment(DbRecord):
table = "equipment"
pk_field = "eid"
fields = ('eid', 'vid', 'ename', 'shortname', 'model', 'ecat', 'serial',
'supplier', 'manufacturer', 'purchdate', 'price', 'weight', 'remarks',
'econd', 'flags')
pk_is_auto = True
fields = ('eid', 'vid', 'ename', 'shortname', 'model', 'ecat',
'serial', 'supplier', 'manufacturer', 'purchdate', 'price', 'weight',
'remarks', 'econd', 'flags')
def __init__(self, db, eid=None):
super().__init__(db, eid)
+3 -2
View File
@@ -7,8 +7,9 @@ class Fuse(DbRecord):
table = "fuse"
pk_field = "fuseid"
fields = ('fuseid', 'vid', 'fnumber', 'ftype', 'current', 'cableid',
'eid', 'location', 'description', 'color', 'remarks')
pk_is_auto = True
fields = ('fuseid', 'vid', 'fnumber', 'ftype', 'current',
'cableid', 'eid', 'location', 'description', 'color', 'remarks')
def __init__(self, db, fuseid=None):
super().__init__(db, fuseid)
+3 -2
View File
@@ -7,8 +7,9 @@ class Inventory(DbRecord):
table = "inventory"
pk_field = "invid"
fields = ('invid', 'invname', 'icat', 'conttype', 'sid', 'boxid', 'eid',
'number', 'weight', 'price', 'purchdate', 'invcond', 'remarks',
pk_is_auto = True
fields = ('invid', 'invname', 'icat', 'conttype', 'sid', 'boxid',
'eid', 'number', 'weight', 'price', 'purchdate', 'invcond', 'remarks',
'flags')
def __init__(self, db, invid=None):
+4 -3
View File
@@ -6,9 +6,10 @@ class Maintenance(DbRecord):
table = "maintenance"
pk_field = "maintid"
fields = ('maintid', 'vid', 'eid', 'series', 'activities', 'plan_date',
'plan_precision', 'completion_date', 'interval_hours', 'last_hours',
'remarks', 'maintstate')
pk_is_auto = True
fields = ('maintid', 'vid', 'eid', 'series', 'activities',
'plan_date', 'plan_precision', 'completion_date', 'interval_hours',
'last_hours', 'remarks', 'maintstate')
def __init__(self, db, maintid=None):
super().__init__(db, maintid)
+3 -2
View File
@@ -6,8 +6,9 @@ class Measurement(DbRecord):
table = "measurement"
pk_field = "mid"
fields = ('mid', 'mname', 'vid', 'eid', 'nval', 'val1', 'val2', 'val3',
'unit', 'accuracy', 'mdate', 'mcat', 'note')
pk_is_auto = True
fields = ('mid', 'mname', 'vid', 'eid', 'nval', 'val1', 'val2',
'val3', 'unit', 'accuracy', 'mdate', 'mcat', 'note')
def __init__(self, db, mid=None):
super().__init__(db, mid)
+3 -2
View File
@@ -7,8 +7,9 @@ class Project(DbRecord):
table = "project"
pk_field = "projid"
fields = ('projname', 'vid', 'responsible', 'startdate', 'duration',
'costs_plan', 'costs_final', 'remarks', 'projstate')
pk_is_auto = True
fields = ('projid', 'projname', 'vid', 'responsible', 'startdate',
'duration', 'costs_plan', 'costs_final', 'remarks', 'projstate')
def __init__(self, db, projid=None):
super().__init__(db, projid)
+4 -3
View File
@@ -6,9 +6,10 @@ class Provision(DbRecord):
table = "provisions"
pk_field = "provid"
fields = ('provid', 'provname', 'provcat', 'sid', 'boxid', 'number',
'target', 'weight', 'weight_net', 'unit', 'calories', 'storedate',
'shelflife', 'price', 'remarks')
pk_is_auto = True
fields = ('provid', 'provname', 'provcat', 'sid', 'boxid',
'number','target', 'weight', 'weight_net', 'unit', 'calories',
'storedate', 'shelflife', 'price', 'remarks')
def __init__(self, db, provid=None):
super().__init__(db, provid)
+2 -1
View File
@@ -12,7 +12,8 @@ class Storage(DbRecord):
table = "storage"
pk_field = "sid"
fields = ('vid', 'sname', 'stype', 'capacity', 'capaunit',
pk_is_auto = True
fields = ('sid', 'vid', 'sname', 'stype', 'capacity', 'capaunit',
'x', 'y', 'z', 'wx', 'wy', 'wz', 'angle', 'color', 'remarks')
def __init__(self, db, sid=None):
+4 -2
View File
@@ -6,8 +6,10 @@ class Task(DbRecord):
table = "task"
pk_field = "taskid"
fields = ('vid', 'projid', 'taskname', 'priority', 'taskstate', 'state_ts',
'plandate', 'duedate', 'started', 'finished', 'responsible', 'remarks')
pk_is_auto = True
fields = ('taskid', 'vid', 'projid', 'taskname', 'priority',
'taskstate', 'state_ts', 'plandate', 'duedate', 'started', 'finished',
'responsible', 'remarks')
def __init__(self, db, taskid=None):
super().__init__(db, taskid)
+8 -7
View File
@@ -6,18 +6,20 @@ class Vessel(DbRecord):
table = "vessel"
pk_field = "vid"
fields = ('vid', 'vtype', 'htype', 'vesselname', 'model', 'shipyard', 'hull_number',
'loa', 'lwl', 'beam', 'draught', 'draught_min', 'displacement', 'ballast',
'beltpos_aft', 'beltpos_bow', 'shapefile', 'sid_default', 'year_built',
'remarks')
pk_is_auto = True
fields = ('vid', 'vtype', 'htype', 'vesselname', 'model',
'shipyard', 'hull_number', 'loa', 'lwl', 'beam', 'draught',
'draught_min', 'displacement', 'ballast', 'beltpos_aft', 'beltpos_bow',
'shapefile', 'sid_default', 'year_built', 'remarks')
def __init__(self, db, vid=None):
super().__init__(db, vid)
self.title = _("Vessel({})")
def create(self):
self.clear()
sql = (
"SELECT MIN(v1.vid+1) AS newid "
"SELECT IFNULL(MIN(v1.vid+1), 1) AS newid "
"FROM vessel AS v1 LEFT JOIN vessel AS v2 ON v2.vid=v1.vid+1 "
"WHERE v1.vid>=0 AND v2.vid IS NULL"
)
@@ -29,9 +31,8 @@ class Vessel(DbRecord):
else:
# create additional vessel
self.pk_value = row['newid']
self.clear()
self.vid = self.pk_value
self.vesselname = f"Vessel #{self.pk_value}"
self.vesselname = "Vessel #{}".format(self.pk_value)
self.htype = 'mono'
self._insert()
# set list change marker