Implement record locking for native gui

This commit is contained in:
thooge committed 2026-11-04 17:21:39 +01:00
1 parent 1a6674c819
commit 669c15109a
16 files changed
+162 -66

No files matched your search

+1 -1
View File
@@ -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)
+3 -3
View File
@@ -3,17 +3,17 @@
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkAdjustment" id="adj_val1">
<property name="upper">100</property>
<property name="upper">100000</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkAdjustment" id="adj_val2">
<property name="upper">100</property>
<property name="upper">100000</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
<object class="GtkAdjustment" id="adj_val3">
<property name="upper">100</property>
<property name="upper">100000</property>
<property name="step-increment">1</property>
<property name="page-increment">10</property>
</object>
+8 -4
View File
@@ -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
+6 -2
View File
@@ -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'])
+6 -1
View File
@@ -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'])
+4 -1
View File
@@ -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'])
+11 -3
View File
@@ -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'])
+4 -24
View File
@@ -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'])
+10 -3
View File
@@ -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'])
+30 -12
View File
@@ -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'])
+7 -2
View File
@@ -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'])
+8 -3
View File
@@ -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'])
+6 -2
View File
@@ -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
+8 -3
View File
@@ -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'])
+37
View File
@@ -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