commit 0dc43f4e0b7574e0f63b4648379b61efe8b6684f
parent 04f0866bea02ea8c4ace6d860ff8b092dae2d432
Author: Paco Esteban <paco@e1e0.net>
Date: Mon, 9 Oct 2023 11:57:46 +0200
easy way to update image and datasheet + ignore some fields from update
Diffstat:
2 files changed, 73 insertions(+), 9 deletions(-)
diff --git a/partsdb/database.py b/partsdb/database.py
@@ -68,6 +68,22 @@ UPDATE_PART_QUERY = """
WHERE
id = ?
"""
+UPDATE_DATASHEET_QUERY = """
+ UPDATE parts
+ SET
+ datasheet = ?,
+ update_date = datetime('now','localtime')
+ WHERE
+ id = ?
+"""
+UPDATE_IMAGE_QUERY = """
+ UPDATE parts
+ SET
+ image = ?,
+ update_date = datetime('now','localtime')
+ WHERE
+ id = ?
+"""
GET_PART_QUERY = """
SELECT p.*, c.name as cat, s.name as storage
FROM parts as p, categories as c, storages as s
@@ -218,6 +234,16 @@ class PartsDB:
)
c.execute(UPDATE_PART_QUERY, update_tuple)
+ def update_datasheet(self, part_id, datasheet):
+ with self.conn:
+ c = self.conn.cursor()
+ c.execute(UPDATE_DATASHEET_QUERY, (sqlite3.Binary(datasheet), part_id))
+
+ def update_image(self, part_id, image):
+ with self.conn:
+ c = self.conn.cursor()
+ c.execute(UPDATE_IMAGE_QUERY, (sqlite3.Binary(image), part_id))
+
def get_image(self, part_id):
c = self.conn.cursor()
c.execute(GET_IMAGE_QUERY, (part_id,))
diff --git a/partsdb/partsdb.py b/partsdb/partsdb.py
@@ -173,16 +173,34 @@ def delete_part(part_id):
db.delete_part(part_id)
-def edit_part(part_id):
+def update_part(part_id):
# load part into a tmp yaml
- part = db.get_part(part_id)
+ part = dict(db.get_part(part_id))
+
+ # ignore some fields.
+ # Binaries We use other functions for this.
+ # also the id fields. We have the text representation of those
+ # insert date should not be modifiable. In fact is ingored on the db update query
+ # and update_date is auto generated
+ ignore_list = [
+ "datasheet",
+ "image",
+ "storage_id",
+ "category_id",
+ "insert_date",
+ "update_date",
+ ]
+ for i in ignore_list:
+ del part[i]
+
part_tmp_file = f"/tmp/partsdb_edit_part{part_id}.yaml"
with open(part_tmp_file, mode="w+") as fp:
- yaml.dump(dict(part), fp)
+ yaml.dump(part, fp)
# call vim to edit, load the resulting yaml and update db
with open(part_tmp_file, mode="r") as fp:
- subprocess.call(["nvim", fp.name])
+ editor = os.environ.get("EDITOR", "vim")
+ subprocess.call([editor, fp.name])
try:
edited_part = yaml.safe_load(fp)
db.update_part(edited_part)
@@ -193,6 +211,16 @@ def edit_part(part_id):
os.remove(part_tmp_file)
+def update_datasheet(part_id, datasheet_file):
+ with open(datasheet_file, mode="rb") as f:
+ db.update_datasheet(part_id, f.read())
+
+
+def update_image(part_id, image_file):
+ with open(image_file, mode="rb") as f:
+ db.update_image(part_id, f.read())
+
+
def adjust_stock(part_id, stock_mod, comment):
db.new_part_history_event(part_id, stock_mod, comment)
db.update_part_qty(part_id, stock_mod)
@@ -273,9 +301,14 @@ def main():
# delete
ap_delete = asp.add_parser("delete", help="Delete a part")
ap_delete.add_argument("part_id", help="Part Id", type=int)
- # edit
- ap_edit = asp.add_parser("edit", help="Edit a part")
- ap_edit.add_argument("part_id", help="Part Id", type=int)
+ # update
+ ap_update = asp.add_parser(
+ "update",
+ help="update a part. Will launch $EDITOR with a YAML dump of the part",
+ )
+ ap_update.add_argument("-d", dest="datasheet", help="Datasheet file to update")
+ ap_update.add_argument("-i", dest="image", help="Image file to update")
+ ap_update.add_argument("part_id", help="Part Id", type=int)
# stock
ap_stock = asp.add_parser("stock", help="Modifies a part stock")
ap_stock.add_argument("part_id", help="Part Id", type=int)
@@ -310,8 +343,13 @@ def main():
open_image(args.part_id)
elif args.command == "delete":
delete_part(args.part_id)
- elif args.command == "edit":
- edit_part(args.part_id)
+ elif args.command == "update":
+ if args.datasheet:
+ update_datasheet(args.part_id, args.datasheet)
+ elif args.image:
+ update_image(args.part_id, args.image)
+ else:
+ update_part(args.part_id)
elif args.command == "stock":
adjust_stock(args.part_id, args.stock_mod, args.comment)
elif args.command == "export":