Convert gen-all-inventories to work with the Django framework.
authorW. Trevor King <wking@drexel.edu>
Sun, 1 Apr 2012 16:51:05 +0000 (12:51 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 1 Apr 2012 16:51:05 +0000 (12:51 -0400)
contrib/gen-all-inventories.py [new file with mode: 0755]
contrib/gen-all-inventories.sh [deleted file]

diff --git a/contrib/gen-all-inventories.py b/contrib/gen-all-inventories.py
new file mode 100755 (executable)
index 0000000..85e7018
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+#
+# Copyright
+
+"""Generate a complete inventory for every storage location.
+
+You may need to export ``DJANGO_SETTINGS_MODULE`` so that the script
+can find the appropriate database.
+"""
+
+import chemdb.models as _models
+import chemdb.doc as _doc
+
+
+def filename(location, extension='.pdf'):
+    loc = location.abbrev.replace(' ', '-')
+    return '{}{}'.format(loc, extension)
+
+
+def generate_inventories():
+    for location in _models.Location.objects.all():
+        chemical_instances = location.chemical_instances.all()
+        dg = _doc.DocGen(chemical_instances=chemical_instances)        
+        pdf = dg.inventory(title='Inventory ({})'.format(location.abbrev))
+        with open(filename(location), 'wb') as f:
+            f.write(pdf)
+
+
+if __name__ == '__main__':
+    generate_inventories()
diff --git a/contrib/gen-all-inventories.sh b/contrib/gen-all-inventories.sh
deleted file mode 100755 (executable)
index d9b5d56..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/bin/bash
-#
-# generate a complete inventory for _every_single_storage_location_ !
-# stored in ./docs/locs/<location>.pdf, with <location> being slightly
-# cleaned up...
-#
-# usage: gen-all-inventories.sh
-
-INVENTORY_DB='./current/inventory.db'
-DOC_DIR='./docs/locs' # no trailing slash!
-
-mkdir -p "$DOC_DIR"
-
-while read LOCATION; do
-    echo "Processing '$LOCATION'"
-    OUTPUT_PDF_PATH=$(./bin/chem_db.py -f "$INVENTORY_DB" --inventory --pdf-title "Inventory ($LOCATION)" --valid-record "r['Location']=='$LOCATION'" --sort-field Name | tail -n1)
-    CLEAN_LOCATION=$(echo "$LOCATION" | sed 's/ /_/g')
-    FINAL_PDF_PATH="${DOC_DIR}/${CLEAN_LOCATION}.pdf"
-    mv "$OUTPUT_PDF_PATH" "$FINAL_PDF_PATH"
-    echo "  created $FINAL_PDF_PATH"
-done < <(./bin/chem_db.py -f "$INVENTORY_DB" --list-locations)