Native gui first steps: storage, box and companies are editable
This commit is contained in:
1 parent
1eea4058bf
commit
06a9834688
47 files changed
+3119
-883
No files matched your search
+209
-49
@@ -1,12 +1,16 @@
|
||||
# SPDX-License-Identifier: WTFPL
|
||||
|
||||
|
||||
|
||||
import os
|
||||
from gi.repository import GLib, Gtk, Gdk
|
||||
from dateutil.parser import parse
|
||||
|
||||
from .view import View
|
||||
from yms import gtkutils, combobox, treeview
|
||||
from yms.lookup import Dropdown
|
||||
from yms.model import inventory
|
||||
from yms.model import equipment
|
||||
|
||||
class InventoryView(View):
|
||||
|
||||
@@ -15,22 +19,14 @@ 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
|
||||
# - or via box which is assigned to a storage
|
||||
self.lst_inventory = Gtk.ListStore(int, str, str, int, str, str)
|
||||
sql = (
|
||||
"SELECT i.invid, i.invname, s.sname AS container, i.number, i.invcond, i.remarks "
|
||||
"FROM inventory AS i JOIN storage AS s ON (i.conttype='storage' AND i.sid=s.sid) "
|
||||
"WHERE s.vid=%s "
|
||||
"UNION SELECT i.invid, i.invname, b.label AS container, i.number, i.invcond, i.remarks "
|
||||
"FROM inventory AS i JOIN box AS b ON (i.conttype='box' AND i.boxid=b.boxid) JOIN storage AS s ON (b.sid=s.sid) "
|
||||
"WHERE s.vid=%s"
|
||||
)
|
||||
self.db.cur.execute(sql, (context.vessel.vid, context.vessel.vid))
|
||||
for row in self.db.cur.fetchall():
|
||||
self.lst_inventory.append(row)
|
||||
self.load_list()
|
||||
|
||||
treeview.add_column(self.tree['inventory'], _('ID'), 0)
|
||||
treeview.add_column(self.tree['inventory'], _('Name'), 1)
|
||||
@@ -40,42 +36,60 @@ class InventoryView(View):
|
||||
treeview.add_column(self.tree['inventory'], _('Remarks'), 5)
|
||||
self.tree['inventory'].set_model(self.lst_inventory)
|
||||
|
||||
# Container type
|
||||
self.dd_conttype = self.context.lookup.enum('inventory.conttype')
|
||||
combobox.load_from_dict(self.combo['conttype'], self.dd_conttype)
|
||||
# Inventory category
|
||||
self.dd_category = self.context.lookup.dropdown(Dropdown.INVENTORY_CATEGORY)
|
||||
combobox.load_from_dict(self.combo['category'], {-1: _("─── none ───")} | self.dd_category)
|
||||
# Storage
|
||||
self.dd_storage = self.context.lookup.storage(context.vessel.vid)
|
||||
combobox.load_from_dict(self.combo['storage'], {-1: _("─── none ───")} | self.dd_storage)
|
||||
# Box
|
||||
self.dd_box = self.context.lookup.box(context.vessel.vid)
|
||||
combobox.load_from_dict(self.combo['box'], {-1: _("─── none ───")} | self.dd_box)
|
||||
# Equipment
|
||||
self.dd_equipment = self.context.lookup.equipment(context.vessel.vid)
|
||||
combobox.load_from_dict(self.combo['equipment'], {-1: _("─── none ───")} | self.dd_equipment)
|
||||
# Condition
|
||||
self.dd_condition = self.context.lookup.enum('inventory.invcond')
|
||||
combobox.load_from_dict(self.combo['condition'], {"-1": _("─── none ───")} | self.dd_condition)
|
||||
|
||||
# buttons on list page
|
||||
def on_but_new_clicked(self, widget):
|
||||
self.topwidget.set_visible_child(self.box['inv_edit'])
|
||||
def load_list(self):
|
||||
self.lst_inventory.clear()
|
||||
sql = (
|
||||
"SELECT i.invid, i.invname, s.sname AS container, i.number, i.invcond, i.remarks "
|
||||
"FROM inventory AS i JOIN storage AS s ON (i.conttype='storage' AND i.sid=s.sid) "
|
||||
"WHERE s.vid=%s "
|
||||
"UNION SELECT i.invid, i.invname, b.label AS container, i.number, i.invcond, i.remarks "
|
||||
"FROM inventory AS i JOIN box AS b ON (i.conttype='box' AND i.boxid=b.boxid) JOIN storage AS s ON (b.sid=s.sid) "
|
||||
"WHERE s.vid=%s"
|
||||
)
|
||||
self.db.cur.execute(sql, (self.context.vessel.vid, self.context.vessel.vid))
|
||||
for row in self.db.cur.fetchall():
|
||||
self.lst_inventory.append(row)
|
||||
|
||||
def on_but_edit_clicked(self, widget):
|
||||
self.topwidget.set_visible_child(self.box['inv_edit'])
|
||||
def lock_timer(self):
|
||||
self.context.db.lock_refresh(self.token)
|
||||
return True # continue timer
|
||||
|
||||
def on_but_detail_clicked(self, widget):
|
||||
invid = treeview.get_value(self.tree['inventory'], 0)
|
||||
self.inv.load(invid)
|
||||
self.display_record()
|
||||
self.topwidget.set_visible_child(self.box['inv_detail'])
|
||||
self.button['close'].grab_focus()
|
||||
def on_page_activated(self):
|
||||
print("Inventory activated")
|
||||
|
||||
def on_tvw_inventory_row_activated(self, widget, path, column):
|
||||
invid = treeview.get_value(widget, 0)
|
||||
self.inv.load(invid)
|
||||
self.display_record()
|
||||
self.topwidget.set_visible_child(self.box['inv_detail'])
|
||||
|
||||
# buttons on detail page
|
||||
# edit2 uses edit callback
|
||||
def on_but_close_clicked(self, widget):
|
||||
self.topwidget.set_visible_child(self.box['inventory'])
|
||||
|
||||
def on_but_delete_clicked(self, widget):
|
||||
self.topwidget.set_visible_child(self.box['inventory'])
|
||||
|
||||
# buttons on edit page
|
||||
def on_but_save_clicked(self, widget):
|
||||
pass
|
||||
|
||||
def on_but_cancel_clicked(self, widget):
|
||||
self.topwidget.set_visible_child(self.box['inventory'])
|
||||
def clear_record(self):
|
||||
self.inv.clear()
|
||||
self.entry['name'].set_text('')
|
||||
combobox.activate_row(self.combo['conttype'], -1)
|
||||
combobox.activate_row(self.combo['storage'], -1)
|
||||
combobox.activate_row(self.combo['box'], -1)
|
||||
combobox.activate_row(self.combo['equipment'], -1)
|
||||
self.spin['number'].set_value(1)
|
||||
self.spin['weight'].set_value(0)
|
||||
self.spin['price'].set_value(0)
|
||||
self.entry['purchdate'].set_text('')
|
||||
combobox.activate_row(self.combo['condition'], 'unknown')
|
||||
combobox.activate_row(self.combo['category'], -1)
|
||||
self.text['remarks'].get_buffer().set_text('')
|
||||
|
||||
def display_record(self):
|
||||
# TODO nice formatting
|
||||
@@ -93,13 +107,159 @@ class InventoryView(View):
|
||||
self.label['category'].set_text(self.dd_category[self.inv.icat])
|
||||
else:
|
||||
self.label['category'].set_text('')
|
||||
self.label['flags'].set_text(self.inv.flags)
|
||||
self.label['remarks'].set_text(self.inv.remarks or '')
|
||||
|
||||
def edit_record(self):
|
||||
self.entry['user_login'].set_text(user.login)
|
||||
self.entry['user_displayname'].set_text(user.displayname)
|
||||
combobox.activate_row(self.combo['user_role'], user.role)
|
||||
combobox.activate_row(self.combo['user_lang'], user.language)
|
||||
self.check['user_locked'].set_active('locked' in user.flags)
|
||||
self.check['user_deleted'].set_active('deleted' in user.flags)
|
||||
self.entry['user_pass'].set_text('')
|
||||
self.entry['name'].set_text(self.inv.invname)
|
||||
combobox.activate_row(self.combo['conttype'], self.inv.conttype)
|
||||
combobox.activate_row(self.combo['storage'], self.inv.sid)
|
||||
combobox.activate_row(self.combo['box'], self.inv.boxid)
|
||||
combobox.activate_row(self.combo['equipment'], self.inv.eid)
|
||||
self.spin['number'].set_value(self.inv.number)
|
||||
self.spin['weight'].set_value(self.inv.weight or 0)
|
||||
self.spin['price'].set_value(self.inv.price or 0)
|
||||
self.entry['purchdate'].set_text(self.inv.purchdate or '')
|
||||
combobox.activate_row(self.combo['condition'], self.inv.invcond)
|
||||
combobox.activate_row(self.combo['category'], self.inv.icat)
|
||||
flags = self.inv.flags.split(',')
|
||||
for flag in self.context.lookup.enum('inventory.flags'):
|
||||
self.check[flag].set_active(flag in flags)
|
||||
self.text['remarks'].get_buffer().set_text(self.inv.remarks or '')
|
||||
|
||||
def save_record(self):
|
||||
# fields must have been validated beforehand
|
||||
self.inv.invname = self.entry['name'].get_text()
|
||||
self.inv.conttype = combobox.get_list_value(self.combo['conttype'])
|
||||
self.inv.sid = combobox.get_list_value(self.combo['storage'])
|
||||
if self.inv.sid < 0:
|
||||
self.inv.sid = None
|
||||
self.inv.boxid = combobox.get_list_value(self.combo['box'])
|
||||
if self.inv.boxid < 0:
|
||||
self.inv.boxid = None
|
||||
self.inv.eid = combobox.get_list_value(self.combo['equipment'])
|
||||
if self.inv.eid < 0:
|
||||
self.inv.eid = None
|
||||
self.inv.number = self.spin['number'].get_value()
|
||||
self.inv.weight = self.spin['weight'].get_value()
|
||||
self.inv.price = self.spin['price'].get_value()
|
||||
try:
|
||||
# TODO respect configured date format
|
||||
self.inv.purchdate = parse(self.entry['purchdate'].get_text(), dayfirst=True)
|
||||
except ValueError:
|
||||
self.inv.purchdate = None
|
||||
self.inv.invcond = combobox.get_list_value(self.combo['condition'])
|
||||
self.inv.icat = combobox.get_list_value(self.combo['category'])
|
||||
# TODO this could be a generic function operating on a list
|
||||
# of check controls
|
||||
flags = []
|
||||
if self.check['ordered'].get_active(): flags.append('ordered')
|
||||
if self.check['removed'].get_active(): flags.append('removed')
|
||||
if self.check['deleted'].get_active(): flags.append('deleted')
|
||||
self.inv.flags = ','.join(flags)
|
||||
self.inv.remarks = gtkutils.get_text_contents(self.text['remarks'])
|
||||
|
||||
# buttons on list page
|
||||
def on_but_new_clicked(self, widget):
|
||||
self.newrecord = True
|
||||
self.label['title_edit'].set_text(_("Add new Inventory"))
|
||||
self.clear_record()
|
||||
self.topwidget.set_visible_child(self.box['inv_edit'])
|
||||
|
||||
def on_but_edit_clicked(self, widget):
|
||||
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"))
|
||||
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)
|
||||
self.inv.load(invid)
|
||||
print(self.inv)
|
||||
self.display_record()
|
||||
self.topwidget.set_visible_child(self.box['inv_detail'])
|
||||
self.button['close'].grab_focus()
|
||||
|
||||
def on_tvw_inventory_row_activated(self, widget, path, column):
|
||||
invid = treeview.get_value(widget, 0)
|
||||
self.inv.load(invid)
|
||||
self.display_record()
|
||||
self.topwidget.set_visible_child(self.box['inv_detail'])
|
||||
|
||||
# buttons on detail page
|
||||
# edit2 uses edit callback
|
||||
def on_but_close_clicked(self, widget):
|
||||
self.topwidget.set_visible_child(self.box['inventory'])
|
||||
|
||||
def on_but_convert_clicked(self, widget):
|
||||
# convert equipment to inventory
|
||||
answer = gtkutils.questionbox(
|
||||
_("Convert inventory to equipment?\n"
|
||||
"Some properties will be lost!\n\n"
|
||||
"Are you sure?"), title="YMS"
|
||||
)
|
||||
if answer == False:
|
||||
return
|
||||
# TODO / WIP
|
||||
"""
|
||||
lost fields: icat number conttype sid boxid
|
||||
"""
|
||||
equip = equipment.Equipment(self.context.db)
|
||||
equip.ename = self.inv.name
|
||||
equip.shortname = ''
|
||||
equip.manufacturer = None
|
||||
equip.model = ''
|
||||
equip.serial = ''
|
||||
equip.weight = self.inv.weight
|
||||
equip.price = self.inv.price
|
||||
equip.purchdate = self.inv.purchdate
|
||||
equip.supplier = None
|
||||
equip.ecat = None
|
||||
equip.econd = self.inv.invcond
|
||||
equip.remarks = self.inv.remarks
|
||||
equip.flags = self.inv.flags
|
||||
print(equip)
|
||||
|
||||
def on_but_delete_clicked(self, widget):
|
||||
answer = gtkutils.questionbox(
|
||||
_("Delete inventory!\n"
|
||||
"Are you sure?"), title="YMS"
|
||||
)
|
||||
if answer == False:
|
||||
return
|
||||
self.topwidget.set_visible_child(self.box['inventory'])
|
||||
|
||||
# buttons on edit page
|
||||
def on_but_save_clicked(self, widget):
|
||||
# check mandatory fields
|
||||
if not self.entry['name'].get_text().strip():
|
||||
# TODO error marker
|
||||
return
|
||||
self.save_record()
|
||||
print(self.inv)
|
||||
self.inv.save()
|
||||
self.context.db.conn.commit()
|
||||
|
||||
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.topwidget.set_visible_child(self.box['inventory'])
|
||||
Reference in new issue
Block a user