From 669c15109a77969697b91750ff68723a324e6c5e Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Wed, 4 Nov 2026 17:21:39 +0100 Subject: [PATCH] Implement record locking for native gui --- yms/model/measurement.py | 2 +- yms/ui/measurement.ui | 6 +++--- yms/view/boxes.py | 12 ++++++++---- yms/view/cables.py | 8 ++++++-- yms/view/companies.py | 7 ++++++- yms/view/equipment.py | 5 ++++- yms/view/fuses.py | 14 +++++++++++--- yms/view/inventory.py | 28 ++++----------------------- yms/view/maintenance.py | 13 ++++++++++--- yms/view/measurement.py | 42 ++++++++++++++++++++++++++++------------ yms/view/projects.py | 9 +++++++-- yms/view/provisions.py | 11 ++++++++--- yms/view/storage.py | 8 ++++++-- yms/view/tasks.py | 11 ++++++++--- yms/view/view.py | 37 +++++++++++++++++++++++++++++++++++ ymsgui.py | 15 ++++++++++++-- 16 files changed, 162 insertions(+), 66 deletions(-) diff --git a/yms/model/measurement.py b/yms/model/measurement.py index ca8879e..db555fd 100644 --- a/yms/model/measurement.py +++ b/yms/model/measurement.py @@ -7,7 +7,7 @@ class Measurement(DbRecord): table = "measurement" pk_field = "mid" fields = ('mid', 'mname', 'vid', 'eid', 'nval', 'val1', 'val2', 'val3', - 'unit', 'accuracy', 'mdate', 'note') + 'unit', 'accuracy', 'mdate', 'mcat', 'note') def __init__(self, db, mid=None): super().__init__(db, mid) diff --git a/yms/ui/measurement.ui b/yms/ui/measurement.ui index ccd5b83..464f281 100644 --- a/yms/ui/measurement.ui +++ b/yms/ui/measurement.ui @@ -3,17 +3,17 @@ - 100 + 100000 1 10 - 100 + 100000 1 10 - 100 + 100000 1 10 diff --git a/yms/view/boxes.py b/yms/view/boxes.py index 93e7829..8ecc038 100644 --- a/yms/view/boxes.py +++ b/yms/view/boxes.py @@ -143,11 +143,13 @@ class BoxesView(View): self.topwidget.set_visible_child(self.box['box_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - self.label['title_edit'].set_text(_("Edit Box")) boxid = treeview.get_value(self.tree['box'], 0) if not boxid: return + self.newrecord = False + self.label['title_edit'].set_text(_("Edit Box")) + if not self.record_lock('box', boxid): + return self.sbox.load(boxid) self.edit_record() self.topwidget.set_visible_child(self.box['box_edit']) @@ -191,7 +193,9 @@ class BoxesView(View): return self.save_record() print(self.sbox) - self.sbox.save() + if not self.sbox.save(): + pass + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False @@ -218,5 +222,5 @@ class BoxesView(View): return False def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['box']) - self.newrecord = False diff --git a/yms/view/cables.py b/yms/view/cables.py index 676aa73..f22291c 100644 --- a/yms/view/cables.py +++ b/yms/view/cables.py @@ -162,11 +162,13 @@ class CablesView(View): self.topwidget.set_visible_child(self.box['cable_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - self.label['title_edit'].set_text(_("Edit Cable")) cableid = treeview.get_value(self.tree['cables'], 0) if not cableid: return + self.newrecord = False + self.label['title_edit'].set_text(_("Edit Cable")) + if not self.record_lock('cable', cableid): + return self.cable.load(cableid) self.edit_record() self.topwidget.set_visible_child(self.box['cable_edit']) @@ -215,6 +217,7 @@ class CablesView(View): # TODO error in infobox print(self.context.db.last_error()) return + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False @@ -222,5 +225,6 @@ class CablesView(View): self.topwidget.set_visible_child(self.box['cable_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['cables']) diff --git a/yms/view/companies.py b/yms/view/companies.py index 29c4b88..197716f 100644 --- a/yms/view/companies.py +++ b/yms/view/companies.py @@ -143,6 +143,8 @@ class CompaniesView(View): compid = treeview.get_value(self.tree['companies'], 0) if not compid: return + if not self.record_lock('company', compid): + return self.comp.load(compid) self.edit_record() self.label['title_edit'].set_text(_("Edit Company")) @@ -187,7 +189,9 @@ class CompaniesView(View): return self.save_record() print(self.comp) - self.comp.save() + if not self.comp.save(): + pass + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False @@ -195,4 +199,5 @@ class CompaniesView(View): self.topwidget.set_visible_child(self.box['comp_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['companies']) diff --git a/yms/view/equipment.py b/yms/view/equipment.py index c42a2af..fa0d034 100644 --- a/yms/view/equipment.py +++ b/yms/view/equipment.py @@ -103,7 +103,6 @@ class EquipmentView(View): self.entry['name'].grab_focus() def display_record(self): - print(self.equip) self.label['name'].set_text(self.equip.ename) self.label['shortname'].set_text(self.equip.shortname or '') if self.equip.manufacturer: @@ -184,6 +183,8 @@ class EquipmentView(View): if eid is None: return self.label['title_edit'].set_text(_("Edit Equipment")) + if not self.record_lock('equipment', eid): + return self.equip.load(eid) self.edit_record() self.topwidget.set_visible_child(self.box['equip_edit']) @@ -239,6 +240,7 @@ class EquipmentView(View): if not self.equip.save(): # TODO error handling pass + self.record_unlock() if self.newrecord: self.add_row() #self.load_list() @@ -249,5 +251,6 @@ class EquipmentView(View): self.topwidget.set_visible_child(self.box['equip_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['equipment']) diff --git a/yms/view/fuses.py b/yms/view/fuses.py index e09c252..626a477 100644 --- a/yms/view/fuses.py +++ b/yms/view/fuses.py @@ -160,11 +160,13 @@ class FusesView(View): self.topwidget.set_visible_child(self.box['fuse_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - fuseid = treeview.get_value(self.tree['fuses'], 0) if fuseid is None: return self.label['title_edit'].set_text(_("Edit Fuse")) + self.newrecord = False + fuseid = treeview.get_value(self.tree['fuses'], 0) + if not self.record_lock('fuse', fuseid): + return self.fuse.load(fuseid) self.edit_record() self.topwidget.set_visible_child(self.box['fuse_edit']) @@ -211,10 +213,16 @@ class FusesView(View): return self.save_record() print(self.fuse) - self.fuse.save() + if not self.fuse.save(): + pass + self.record_unlock() + if self.newrecord: + self.load_list() + self.newrecord = False self.display_record() self.topwidget.set_visible_child(self.box['fuse_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['fuses']) diff --git a/yms/view/inventory.py b/yms/view/inventory.py index 22e8396..90280d4 100644 --- a/yms/view/inventory.py +++ b/yms/view/inventory.py @@ -18,8 +18,6 @@ class InventoryView(View): self.inv = inventory.Inventory(context.db) # current record self.newrecord = False - self.token = None # record locking - self.timer_id = None # Inventory gets vessel assignment via # - storage direct @@ -73,10 +71,6 @@ class InventoryView(View): # restore previous selected row selection.select_iter(treeiter) - def lock_timer(self): - self.context.db.lock_refresh(self.token) - return True # continue timer - def on_page_activated(self): print("Inventory activated") @@ -180,20 +174,13 @@ class InventoryView(View): invid = treeview.get_value(self.tree['inventory'], 0) if invid is None: return - self.token = self.context.db.lock_acquire('inventory', invid, self.user.user_id) - if self.token is None: - gtkutils.messagebox( - _("Record is locked!\n" - "Edit prohibited."), title="YMS" - ) - return - self.timer_id = GLib.timeout_add(120000, self.lock_timer) # 2 minutes lock refresh timer self.newrecord = False self.label['title_edit'].set_text(_("Edit Inventory")) + if not self.record_lock('inventory', invid): + return self.inv.load(invid) self.edit_record() self.topwidget.set_visible_child(self.box['inv_edit']) - # TODO start timer to refresh record lock def on_but_detail_clicked(self, widget): invid = treeview.get_value(self.tree['inventory'], 0) @@ -262,22 +249,15 @@ class InventoryView(View): print(self.inv) if not self.inv.save(): pass + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False else: pass - # TODO aktuelle Listenzeile aktualisieren! self.display_record() self.topwidget.set_visible_child(self.box['inv_detail']) def on_but_cancel_clicked(self, widget): - if not self.newrecord: - if self.timer_id is not None: - # remove locking timer - GLib.source_remove(self.timer_id) - self.timer_id = None - if not self.token is None: - if not self.context.db.lock_release(self.token): - print("Error while releasing lock!") + self.record_unlock() self.topwidget.set_visible_child(self.box['inventory']) diff --git a/yms/view/maintenance.py b/yms/view/maintenance.py index 12cd166..2d31982 100644 --- a/yms/view/maintenance.py +++ b/yms/view/maintenance.py @@ -133,11 +133,13 @@ class MaintenanceView(View): self.topwidget.set_visible_child(self.box['maint_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - self.label['title_edit'].set_text(_("Edit Maintenance")) maintid = treeview.get_value(self.tree['maintenance'], 0) if not maintid: return + self.newrecord = False + self.label['title_edit'].set_text(_("Edit Maintenance")) + if not self.record_lock('maintenance', maintid): + return self.maint.load(maintid) self.edit_record() self.topwidget.set_visible_child(self.box['maint_edit']) @@ -178,13 +180,17 @@ class MaintenanceView(View): if not self.entry['activities'].get_text().strip(): # TODO error marker return - self.save_record() + if not self.save_record(): + pass + if not self.record_lock('maintenance', self.maint.maintid): + return print(self.maint) if not self.maint.save(): print("Save error") # TODO error in infobox print(self.context.db.last_error()) return + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False @@ -192,4 +198,5 @@ class MaintenanceView(View): self.topwidget.set_visible_child(self.box['maint_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['maintenance']) diff --git a/yms/view/measurement.py b/yms/view/measurement.py index 05ddf7b..6370f96 100644 --- a/yms/view/measurement.py +++ b/yms/view/measurement.py @@ -2,6 +2,7 @@ import os from gi.repository import Gtk +from dateutil.parser import parse from .view import View from yms import gtkutils, combobox, treeview @@ -51,6 +52,9 @@ class MeasurementView(View): treeview.add_column(self.tree['measurement'], _('Note'), 5) self.tree['measurement'].set_model(self.lst_measurement) + self.entry['date'].set_placeholder_text(context.get_datefmt_placeholder()) + DatePicker(self.entry['date'], self.button['date'], context.get_datefmt()) + def load_list(self): selected_id = treeview.get_value(self.tree['measurement']) self.lst_measurement.clear() @@ -64,12 +68,13 @@ class MeasurementView(View): self.db.dcur.execute(sql, (self.context.vessel.vid,)) selection = self.tree['measurement'].get_selection() for row in self.db.dcur.fetchall(): + unit = self.dd_unit[row['unit']] if row['nval'] == 1: - value = "{} {}".format(row['val1'], row['unit']) + value = "{} {}".format(row['val1'], unit) elif row['nval'] == 2: - value = "{}x{} {}".format(row['val1'], row['val2'], row['unit']) + value = "{}x{} {}".format(row['val1'], row['val2'], unit) elif row['nval'] == 3: - value = "{}x{}x{} {}".format(row['val1'], row['val2'], row['val3'], row['unit']) + value = "{}x{}x{} {}".format(row['val1'], row['val2'], row['val3'], unit) else: #invalid value = "#invalid" @@ -82,7 +87,7 @@ class MeasurementView(View): row['note'] or '' ) treeiter = self.lst_measurement.append(data) - if row[0] == selected_id: + if row['mid'] == selected_id: # restore previous selected row selection.select_iter(treeiter) @@ -120,7 +125,7 @@ class MeasurementView(View): self.label['unit'].set_text(self.dd_unit.get(self.measure.unit, '')) self.label['accuracy'].set_text(self.dd_accuracy[self.measure.accuracy]) self.label['date'].set_text(self.format_date(self.measure.mdate)) - self.label['category'].set_text(self.dd_category[self.measure.mcat]) + self.label['category'].set_text(self.dd_category.get(self.measure.mcat, '')) self.label['note'].set_text(self.measure.note or '') def edit_record(self): @@ -140,10 +145,10 @@ class MeasurementView(View): self.spin['val3'].set_value(self.measure.val3) combobox.activate_row(self.combo['unit'], self.measure.unit or 1) combobox.activate_row(self.combo['accuracy'], self.measure.accuracy) - self.entry['date'].set_text(self.measure.mdate) + self.entry['date'].set_text(self.format_date(self.measure.mdate)) combobox.activate_row(self.combo['equipment'], self.measure.eid) combobox.activate_row(self.combo['category'], self.measure.mcat) - self.text['note'].get_buffer().set_text(self.task.note or '') + self.text['note'].get_buffer().set_text(self.measure.note or '') self.entry['name'].grab_focus() self.label['edit_val2'].set_visible(self.measure.nval > 1) @@ -153,15 +158,24 @@ class MeasurementView(View): def save_record(self): # fields must have been validated beforehand + self.measure.vid = self.context.vessel.vid self.measure.mname = self.entry['name'].get_text() self.measure.nval = combobox.get_list_value(self.combo['dimensions']) self.measure.val1 = self.spin['val1'].get_value_as_int() self.measure.val2 = self.spin['val2'].get_value_as_int() self.measure.val3 = self.spin['val3'].get_value_as_int() + self.measure.unit = combobox.get_list_value(self.combo['unit']) self.measure.accuracy = combobox.get_list_value(self.combo['accuracy']) - self.measure.mdate = self.entry['date'].get_text() + try: + self.measure.mdate = parse(self.entry['date'].get_text(), dayfirst=True) + except ValueError: + self.measure.mdate = None self.measure.eid = combobox.get_list_value(self.combo['equipment']) + if self.measure.eid == -1: + self.measure.eid = None self.measure.mcat = combobox.get_list_value(self.combo['category']) + if self.measure.mcat == -1: + self.measure.mcat = None self.measure.note = gtkutils.get_text_contents(self.text['note']) # buttons on list page @@ -172,11 +186,13 @@ class MeasurementView(View): self.topwidget.set_visible_child(self.box['measure_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - self.label['title_edit'].set_text(_("Edit Measurement")) mid = treeview.get_value(self.tree['measurement'], 0) if not mid: return + self.newrecord = False + self.label['title_edit'].set_text(_("Edit Measurement")) + if not self.record_lock('measurement', mid): + return self.measure.load(mid) self.edit_record() self.topwidget.set_visible_child(self.box['measure_edit']) @@ -233,13 +249,15 @@ class MeasurementView(View): self.infobar.set_revealed(False) self.save_record() print(self.measure) - self.measure.save() + if not self.measure.save(): + pass + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False - self.display_record() self.topwidget.set_visible_child(self.box['measure_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['measurement']) diff --git a/yms/view/projects.py b/yms/view/projects.py index a4ab99b..af15814 100644 --- a/yms/view/projects.py +++ b/yms/view/projects.py @@ -2,6 +2,7 @@ import os from gi.repository import Gtk +from dateutil.parser import parse from gettext import pgettext from .view import View @@ -120,11 +121,13 @@ class ProjectsView(View): self.topwidget.set_visible_child(self.box['proj_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - self.label['title_edit'].set_text(_("Edit Project")) projid = treeview.get_value(self.tree['projects'], 0) if not projid: return + self.newrecord = False + self.label['title_edit'].set_text(_("Edit Project")) + if not self.record_lock('project', projid): + return self.project.load(projid) self.edit_record() self.topwidget.set_visible_child(self.box['proj_edit']) @@ -172,6 +175,7 @@ class ProjectsView(View): # TODO error in infobox print(self.context.db.last_error()) return + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False @@ -179,4 +183,5 @@ class ProjectsView(View): self.topwidget.set_visible_child(self.box['proj_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['projects']) diff --git a/yms/view/provisions.py b/yms/view/provisions.py index 703070b..51133c1 100644 --- a/yms/view/provisions.py +++ b/yms/view/provisions.py @@ -167,11 +167,13 @@ class ProvisionsView(View): self.topwidget.set_visible_child(self.box['prov_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - self.label['title_edit'].set_text(_("Edit Provision")) provid = treeview.get_value(self.tree['provisions'], 0) if not provid: return + self.newrecord = False + self.label['title_edit'].set_text(_("Edit Provision")) + if not self.record_lock('provision', provid): + return self.prov.load(provid) self.edit_record() self.topwidget.set_visible_child(self.box['prov_edit']) @@ -230,7 +232,9 @@ class ProvisionsView(View): return self.save_record() print(self.prov) - self.prov.save() + if not self.prov.save(): + pass + self.record_unlock() if self.newrecord: self.load_list() else: @@ -241,4 +245,5 @@ class ProvisionsView(View): self.topwidget.set_visible_child(self.box['prov_detail']) def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['provisions']) diff --git a/yms/view/storage.py b/yms/view/storage.py index 4b0013b..ba653ae 100644 --- a/yms/view/storage.py +++ b/yms/view/storage.py @@ -142,6 +142,8 @@ class StorageView(View): return self.newrecord = False self.label['title_edit'].set_text(_("Edit Storage")) + if not self.record_lock('storage', sid): + return self.storage.load(sid) self.edit_record() self.topwidget.set_visible_child(self.box['stor_edit']) @@ -185,7 +187,9 @@ class StorageView(View): return self.save_record() print(self.storage) - self.storage.save() + if not self.storage.save(): + pass + self.record_unlock() if self.newrecord: self.load_list() self.context.publish("storage_added", (self.storage.pk_value, self.storage.sname)) @@ -212,5 +216,5 @@ class StorageView(View): return False def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['storage']) - self.newrecord = False diff --git a/yms/view/tasks.py b/yms/view/tasks.py index 8e5c702..8423901 100644 --- a/yms/view/tasks.py +++ b/yms/view/tasks.py @@ -149,11 +149,13 @@ class TasksView(View): self.topwidget.set_visible_child(self.box['task_edit']) def on_but_edit_clicked(self, widget): - self.newrecord = False - self.label['title_edit'].set_text(_("Edit Task")) taskid = treeview.get_value(self.tree['tasks'], 0) if not taskid: return + self.newrecord = False + self.label['title_edit'].set_text(_("Edit Task")) + if not self.record_lock('task', taskid): + return self.task.load(taskid) self.edit_record() self.topwidget.set_visible_child(self.box['task_edit']) @@ -195,7 +197,9 @@ class TasksView(View): return self.save_record() print(self.task) - self.task.save() + if not self.task.save(): + pass + self.record_unlock() if self.newrecord: self.load_list() self.newrecord = False @@ -218,4 +222,5 @@ class TasksView(View): pass def on_but_cancel_clicked(self, widget): + self.record_unlock() self.topwidget.set_visible_child(self.box['tasks']) diff --git a/yms/view/view.py b/yms/view/view.py index 4f3b81d..662290e 100644 --- a/yms/view/view.py +++ b/yms/view/view.py @@ -21,6 +21,9 @@ class View: self.user = context.user self._title = title + self._lock_token = None # record locking + self._lock_timerid = None + locale.bindtextdomain(context.textdomain, context.localedir) locale.textdomain(context.textdomain) @@ -36,6 +39,40 @@ class View: gtkutils.init_named_controls(self, self.builder) + def record_lock(self, table, pk_value): + self._lock_token = self.context.db.lock_acquire(table, pk_value, self.user.user_id) + if self._lock_token is None: + gtkutils.messagebox( + _("Record #%(id)d in table '%(table)s' is locked!\n" + "Edit prohibited.") % {'id': pk_value, 'table': table}, + title="YMS" + ) + return False + # 2 minutes lock refresh timer + self._lock_timerid = GLib.timeout_add(120000, self.on_lock_timer) + return True + + def record_unlock(self): + if self._lock_timerid is not None: + # remove locking timer + GLib.source_remove(self._lock_timerid) + self._lock_timerid = None + if not self._lock_token is None: + # remove lock from database + if not self.context.db.lock_release(self._lock_token): + print(_("Error while releasing record lock!")) + return False + self._lock_token = None + return True + + def on_lock_timer(self): + if self._lock_token is not None: + self.context.db.lock_refresh(self._lock_token) + return True # continue timer + # no valid token, better stop our timer + self._lock_timerid = None + return False + def format_int(self, ival, separator=True, suffix=None, default=''): if ival is None: return default diff --git a/ymsgui.py b/ymsgui.py index 5ce5645..f6e4f5e 100755 --- a/ymsgui.py +++ b/ymsgui.py @@ -225,6 +225,12 @@ class Frontend: def run(self): self.window.show_all() Gtk.main() + # cleanup code for all modules + for module in self.modules.values(): + method = getattr(module, "record_unlock", None) + if callable(method): + # remove all pending record locks + method() def db_change_poller(self): # dynamic combobox data refresh @@ -339,8 +345,13 @@ if __name__ == "__main__": errno, errtxt = context.db.last_error() gtkutils.messagebox( _("Database connection cannot be established.\n\n" - "Server: {}\nDatabase: {}\n" - "Error: {} ({})").format(context.cfg['host'], context.cfg['db'], errtxt, errno), + "Server: %(host)s\nDatabase: %(db)s\n" + "Error: %(errtxt)s (%(errno)d)") % { + 'host': context.cfg['host'], + 'db': context.cfg['db'], + 'errtxt': errtxt, + 'errno': errno + }, title="YMS") # Another fine product of the sirius cybernetics corporation