commit d3283490a5691aa37d7b747b70847152b018bcc4
parent a092e4de9ba70ccd11109995d299920c66a3e6f3
Author: Paco Esteban <paco@e1e0.net>
Date: Sun, 14 Mar 2021 10:45:48 +0100
implement get part
Diffstat:
3 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/database.py b/database.py
@@ -40,7 +40,16 @@ INSERT_PART_QUERY = """
)
VALUES (?,?,?,?,?,?,?,?,?,?,?)
"""
-GET_PART_QUERY = "SELECT * FROM parts 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
+ INNER JOIN categories
+ ON p.category_id = c.id
+ INNER JOIN storages
+ ON p.storage_id = s.id
+ WHERE p.id = ?
+ GROUP BY p.id
+"""
SEARCH_PARTS_QUERY = """
SELECT p.id, p.pn, p.manufacturer, p.description,
p.footprint, p.quantity, p.part_type, c.name as cname
diff --git a/helpers.py b/helpers.py
@@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
+import tempfile
+
def print_parts_list(parts, short=False):
if short:
print(f"{'ID':4} | "
@@ -53,3 +55,29 @@ def print_parts_list(parts, short=False):
f"{p['footprint'][0:5]:6} | "
f"{p['quantity']:4} |"
)
+
+
+def print_part(p, get_files=False):
+ print(f"PN: {p['pn']}\tManufacturer: {p['manufacturer']}")
+ print(f"Category: {p['cat']}\tType: {p['part_type']}"
+ f"\tFootprint: {p['footprint']}")
+ print(f"Storage: {p['storage']}")
+ print(f"Created: {p['insert_date']}"
+ f"\tUpdated: {p['update_date']}\n")
+ print(f"Description:\n{p['description']}\n")
+
+ if get_files:
+ # extract the files from the blobs and store them on /tmp
+ tmpdir = tempfile.gettempdir()
+ basefile = f"{tmpdir}/{p['id']}"
+ if p['datasheet'] is not None:
+ with open(f'{basefile}.pdf', 'wb') as file:
+ file.write(p['datasheet'])
+ print(f'Datasheet: {basefile}.pdf')
+ if p['image'] is not None:
+ with open(f'{basefile}.jpg', 'wb') as file:
+ file.write(p['image'])
+ print(f'Image: {basefile}.jpg\n')
+
+ print(f"Quantity: {p['quantity']}")
+ # here should go the historical data.
diff --git a/myChips.py b/myChips.py
@@ -114,9 +114,9 @@ def search_part(search_term):
print_parts_list(parts)
-def get_part(part_id):
+def get_part(part_id, files):
part = db.get_part(part_id)
- print(part[1])
+ print_part(part, files)
def delete_part(part_id):
@@ -162,6 +162,9 @@ if __name__ == '__main__':
# get
ap_get = asp.add_parser("get", help="Get all details for a part")
ap_get.add_argument("part_id", help="Part Id", type=int)
+ ap_get.add_argument("-f", dest='files',
+ action='store_true',
+ help="Extract Files (datasheet, ...")
# delete
ap_delete = asp.add_parser("delete", help="Delete a part")
ap_delete.add_argument("part_id", help="Part Id", type=int)
@@ -186,7 +189,7 @@ if __name__ == '__main__':
elif args.command == 'search':
search_part(args.search_term)
elif args.command == 'get':
- get_part(args.part_id)
+ get_part(args.part_id, args.files)
elif args.command == 'delete':
delete_part(args.part_id)
elif args.command == 'stock':