partsdb

electronic parts inventory
git clone https://git.e1e0.net/partsdb.git
Log | Files | Refs | README | LICENSE

commit 9520c607508901bc55e9c3b116e0b17cb9ae8d9e
parent 4bbb7c35ba690f2ed44eaed04db06cded5be147b
Author: Paco Esteban <paco@e1e0.net>
Date:   Thu, 29 Dec 2022 09:47:29 +0100

add html export by storage

Diffstat:
Mpartsdb/database.py | 14++++++++++++++
Mpartsdb/exports/templates/index.html | 24++++++++++++++++++------
Apartsdb/exports/templates/storage.html | 41+++++++++++++++++++++++++++++++++++++++++
Mpartsdb/exports/templates/style.css | 14++++++++++++++
Mpartsdb/helpers.py | 10++++++++--
Mpartsdb/partsdb.py | 12++++++++----
6 files changed, 103 insertions(+), 12 deletions(-)

diff --git a/partsdb/database.py b/partsdb/database.py @@ -24,6 +24,15 @@ LIST_PARTS_CATEGORY_QUERY = """ WHERE c.name = ? OR c.id = ? GROUP BY p.id """ +LIST_PARTS_STORAGE_QUERY = """ + SELECT p.id, p.pn, p.mpn, p.manufacturer, p.description, + p.footprint, p.quantity, p.part_type, s.name as sname + FROM parts as p, storages as s + INNER JOIN categories + ON p.storage_id = s.id + WHERE s.name = ? OR s.id = ? + GROUP BY p.id +""" INSERT_PART_QUERY = """ INSERT INTO parts ( @@ -109,6 +118,11 @@ class PartsDB: c.execute(LIST_PARTS_CATEGORY_QUERY, (category, category)) return c.fetchall() + def list_parts_by_storage(self, storage): + c = self.conn.cursor() + c.execute(LIST_PARTS_STORAGE_QUERY, (storage, storage)) + return c.fetchall() + def new_part(self, part): if part[9] is not None: part[9] = sqlite3.Binary(part[9]) diff --git a/partsdb/exports/templates/index.html b/partsdb/exports/templates/index.html @@ -12,11 +12,23 @@ </head> <body> <h1>PartsDB</h1> - <p>Categories:</p> - <ul> - {% for c in cat %} - <li><a href="cat_list_{{ c.id }}.html">{{ c.name }}</a></li> - {% endfor %} - </ul> + <div class="list-container"> + <div class="category-list"> + <p>By category:</p> + <ul> + {% for c in cat %} + <li><a href="cat_list_{{ c.id }}.html">{{ c.name }}</a></li> + {% endfor %} + </ul> + </div> + <div class="storage-list"> + <p>By storage:</p> + <ul> + {% for s in sto %} + <li><a href="storage_{{ s.id }}.html">{{ s.name }}</a></li> + {% endfor %} + </ul> + </div> + </div> </body> </html> diff --git a/partsdb/exports/templates/storage.html b/partsdb/exports/templates/storage.html @@ -0,0 +1,41 @@ +<!doctype html> + +<html lang="en"> +<head> + <meta charset="utf-8"> + + <title>partsdb >> "{{ storage.name }}" index</title> + <meta name="description" content="partsdb storage index"> + <meta name="author" content="e1e0"> + + <link rel="stylesheet" href="style.css?v=1.0"> +</head> +<body> + <p><a href="index.html">Index</a></p> + <p>{{ storage.name }}</p> + <table> + <tr> + <th>ID</th> + <th>PN</th> + <th>MPN</th> + <th>Manufacturer</th> + <th>Desc</th> + <th>Footprint</th> + <th>Type</th> + <th>Qty</th> + </tr> + {% for p in parts %} + <tr> + <td>{{ p.id }}</td> + <td><a href="part_{{ p.id }}.html">{{ p.pn }}</a></td> + <td><a href="part_{{ p.id }}.html">{{ p.mpn }}</a></td> + <td>{{ p.manufacturer }}</td> + <td>{{ p.description }}</td> + <td>{{ p.footprint }}</td> + <td>{{ p.part_type }}</td> + <td>{{ p.quantity }}</td> + </tr> + {% endfor %} + </table> +</body> +</html> diff --git a/partsdb/exports/templates/style.css b/partsdb/exports/templates/style.css @@ -18,3 +18,17 @@ h1 { .field { font-weight: bold; } + +.category-list { + grid-area: catList; +} + +.storage-list { + grid-area: storageList; +} + +.list-container { + display: grid; + column-gap: 50px; + grid-template-areas: 'catList storageList'; +} diff --git a/partsdb/helpers.py b/partsdb/helpers.py @@ -136,10 +136,10 @@ def open_file(content, extension): time.sleep(2) -def html_main_index(dest_folder, categories, env): +def html_main_index(dest_folder, categories, storages, env): tpl = env.get_template("index.html") with open(f"{dest_folder}/index.html", "w") as f: - f.write(tpl.render(cat=categories)) + f.write(tpl.render(cat=categories, sto=storages)) def html_category_index(dest_folder, category, parts, env): @@ -148,6 +148,12 @@ def html_category_index(dest_folder, category, parts, env): f.write(tpl.render(category=category, parts=parts)) +def html_storage_index(dest_folder, storage, parts, env): + tpl = env.get_template("storage.html") + with open(f"{dest_folder}/storage_{storage['id']}.html", "w") as f: + f.write(tpl.render(storage=storage, parts=parts)) + + def html_part(dest_folder, part, part_history, env): tpl = env.get_template("part.html") with open(f"{dest_folder}/part_{part['id']}.html", "w") as f: diff --git a/partsdb/partsdb.py b/partsdb/partsdb.py @@ -183,10 +183,14 @@ def export_db(dest_folder): autoescape=select_autoescape(["html", "xml"]), ) categories = db.get_categories() - helpers.html_main_index(dest_folder, categories, env) - for c in categories: - parts = db.list_parts_by_category(c["name"]) - helpers.html_category_index(dest_folder, c, parts, env) + storages = db.get_storages() + helpers.html_main_index(dest_folder, categories, storages, env) + for s in categories: + parts = db.list_parts_by_category(s["name"]) + helpers.html_category_index(dest_folder, s, parts, env) + for s in storages: + parts = db.list_parts_by_storage(s["name"]) + helpers.html_storage_index(dest_folder, s, parts, env) parts = db.list_parts() for p in parts: part = db.get_part(p["id"])