diff --git a/__pycache__/main.cpython-312.pyc b/__pycache__/main.cpython-312.pyc index bb30c7f..9276f36 100644 Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ diff --git a/main.py b/main.py index 7228e50..0637477 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ #!/usr/bin/python import psycopg2 from config import config -import json, datetime, copy +import json, datetime, copy, csv def lst2pgarr(alist): return '{' + ','.join(alist) + '}' @@ -100,20 +100,18 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info", with psycopg2.connect(**database_config) as conn: with conn.cursor() as cur: cur.execute(f"SELECT item_name, logistics_info_id FROM {site_name}_items WHERE barcode=%s;", (barcode, )) - row = cur.fetchone() - print(row) + item = cur.fetchone() with conn.cursor() as cur: - cur.execute(f"SELECT location_data, quantity_on_hand, primary_location FROM {site_name}_logistics_info WHERE id=%s;", (row[1],)) + cur.execute(f"SELECT location_data, quantity_on_hand, primary_location, barcode FROM {site_name}_logistics_info WHERE id=%s;", (item[1],)) logistics_info = cur.fetchone() - print(logistics_info) new_trans = copy.deepcopy(transaction_payload) new_trans["timestamp"] = datetime.datetime.now() - new_trans["logistics_info_id"] = row[1] + new_trans["logistics_info_id"] = item[1] new_trans["barcode"] = barcode new_trans["user_id"] = user_id - new_trans["name"] = row[0] + new_trans["name"] = item[0] new_trans["transaction_type"] = transaction_type new_trans["description"] = description new_trans["quantity"] = qty @@ -131,11 +129,34 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info", print(error) conn.rollback() return False + if not location: mover = logistics_info[2] else: mover = location + + location_items = None + location_id = None + try: + with conn.cursor() as cur: + cur.execute(f"SELECT id, items FROM {site_name}_locations WHERE name=%s;", (mover, )) + location = cur.fetchone() + if location: + location_id = location[0] + location_items = location[1] + except (Exception, psycopg2.DatabaseError) as error: + print(error) + conn.rollback() + return False + + if logistics_info[3] in location_items.keys(): + location_items[logistics_info[3]] = location_items[logistics_info[3]] + qty + else: + location_items[logistics_info[3]] = qty + + print(location_items) + if mover in logistics_info[0].keys(): logistics_info[0][mover] = logistics_info[0][mover] + qty else: @@ -143,10 +164,12 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info", qty = logistics_info[1] + qty + set_location_data = f"UPDATE {site_name}_locations SET items = %s WHERE id = %s;" set_quantity_on_hand = f"UPDATE {site_name}_logistics_info SET quantity_on_hand = %s, location_data = %s WHERE id = %s;" try: with conn.cursor() as cur: cur.execute(set_quantity_on_hand, (qty, json.dumps(logistics_info[0]), new_trans["logistics_info_id"])) + cur.execute(set_location_data, (json.dumps(location_items), location_id)) except (Exception, psycopg2.DatabaseError) as error: print(error) conn.rollback() @@ -156,6 +179,8 @@ def add_transaction(site_name, barcode, qty, user_id, transaction_type = "info", def add_food_item(site_name: str, barcode: str, name: str, qty: float, payload: dict): + # TODO: I need to validate the name so that it doesnt have characters against the SQL database schema such as ' + defaults = config(filename=f"sites/{site_name}/site.ini", section="defaults") payload["logistics_info"]["primary_location"] = defaults["default_primary_location"] payload["logistics_info"]["auto_issue_location"] = defaults["default_auto_issue_location"] @@ -225,6 +250,9 @@ def delete_site(site_name): drop_table(f'sites/{site_name}/sql/drop/locations.sql') def create_site(site_name): + + site_config = config(f"sites/{site_name}/site.ini", 'defaults') + create_table(f'sites/{site_name}/sql/create/logins.sql') create_table(f'sites/{site_name}/sql/create/groups.sql') create_table(f'sites/{site_name}/sql/create/linked_items.sql') @@ -237,6 +265,33 @@ def create_site(site_name): create_table(f'sites/{site_name}/sql/create/zones.sql') create_table(f'sites/{site_name}/sql/create/locations.sql') + sql = f"INSERT INTO {site_name}_zones(name) VALUES (%s) RETURNING id;" + sqltwo = f"INSERT INTO {site_name}_locations(name, zone_id, items) VALUES (%s, %s, %s);" + + database_config = config() + with psycopg2.connect(**database_config) as conn: + zone_id = None + try: + with conn.cursor() as cur: + cur.execute(sql, (site_config["default_zone"], )) + rows = cur.fetchone() + if rows: + zone_id = rows[0] + except (Exception, psycopg2.DatabaseError) as error: + print(error) + conn.rollback() + return False + + try: + with conn.cursor() as cur: + cur.execute(sqltwo, (site_config["default_primary_location"], zone_id, json.dumps({}))) + except (Exception, psycopg2.DatabaseError) as error: + print(error) + conn.rollback() + return False + + + conn.commit() @@ -281,16 +336,36 @@ payload_food_item = { } } +def parse_csv(path_to_csv): + + payload = copy.deepcopy(payload_food_item) + + + with open(path_to_csv, "r+", encoding="utf-8") as file: + reader = csv.reader(file) + for line in reader: + if line[0] != "id": + payload["item_info"]["packaging"] = line[10] + payload["item_info"]["uom"] = line[13] + payload["item_info"]["cost"] = line[15] + if line[17] != "None": + payload["item_info"]["safety_stock"] = line[17] + qty = float(line[30]) + add_food_item(site_name="test", barcode=line[1], name=line[2], qty=qty, payload=payload) + + if __name__ == "__main__": - #print(add_item(site_name="main", barcode="1235", name="testone")) + #print(add_readitem(site_name="main", barcode="1235", name="testone")) database_config = config() - sql = "SELECT * FROM main_logistics_info WHERE id=2;" + sql = "SELECT items FROM test_locations WHERE id=1;" with psycopg2.connect(**database_config) as conn: with conn.cursor() as cur: cur.execute(sql) - rows = cur.fetchone() - print(rows) - print(type(rows[5])) \ No newline at end of file + items = cur.fetchone()[0] + for k, v in items.items(): + print(f"{k}: {v}") + + #parse_csv(r"C:\\Users\\jadow\Downloads\\2024-09-30-Pantry.csv") \ No newline at end of file diff --git a/sites/test/site.ini b/sites/test/site.ini deleted file mode 100644 index 8cf4684..0000000 --- a/sites/test/site.ini +++ /dev/null @@ -1,9 +0,0 @@ -[site] -site_name=test -site_owner=test -email= - -[defaults] -default_zone=default -default_primary_location=all -default_auto_issue_location=all diff --git a/sites/test/sql/create/brands.sql b/sites/test/sql/create/brands.sql deleted file mode 100644 index 6cd9f9c..0000000 --- a/sites/test/sql/create/brands.sql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_brands ( - id SERIAL PRIMARY KEY, - name VARCHAR(255) -); \ No newline at end of file diff --git a/sites/test/sql/create/food_info.sql b/sites/test/sql/create/food_info.sql deleted file mode 100644 index bedbe47..0000000 --- a/sites/test/sql/create/food_info.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_food_info ( - id SERIAL PRIMARY KEY, - food_groups TEXT [], - ingrediants TEXT [], - nutrients JSONB, - expires BOOLEAN -); \ No newline at end of file diff --git a/sites/test/sql/create/groups.sql b/sites/test/sql/create/groups.sql deleted file mode 100644 index b97fdab..0000000 --- a/sites/test/sql/create/groups.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_groups( - id SERIAL PRIMARY KEY, - name VARCHAR(255) NOT NULL, - description TEXT, - included_items INTEGER [], - group_type VARCHAR(255), - UNIQUE (name) -); \ No newline at end of file diff --git a/sites/test/sql/create/item.sql b/sites/test/sql/create/item.sql deleted file mode 100644 index 5f30ebb..0000000 --- a/sites/test/sql/create/item.sql +++ /dev/null @@ -1,27 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_items( - id SERIAL PRIMARY KEY, - barcode VARCHAR(255) NOT NULL, - item_name VARCHAR(255) NOT NULL, - brand INTEGER, - tags TEXT [], - links TEXT [], - item_info_id INTEGER NOT NULL, - logistics_info_id INTEGER NOT NULL, - food_info_id INTEGER, - row_type VARCHAR(255) NOT NULL, - item_type VARCHAR(255) NOT NULL, - search_string TEXT NOT NULL, - UNIQUE(barcode, item_info_id), - CONSTRAINT fk_item_info - FOREIGN KEY(item_info_id) - REFERENCES test_item_info(id), - CONSTRAINT fk_food_info - FOREIGN KEY(food_info_id) - REFERENCES test_food_info(id), - CONSTRAINT fk_brand - FOREIGN KEY(brand) - REFERENCES test_brands(id), - CONSTRAINT fk_logistics_info - FOREIGN KEY(logistics_info_id) - REFERENCES test_logistics_info(id) -); diff --git a/sites/test/sql/create/item_info.sql b/sites/test/sql/create/item_info.sql deleted file mode 100644 index 317106c..0000000 --- a/sites/test/sql/create/item_info.sql +++ /dev/null @@ -1,15 +0,0 @@ -CREATE TABLE IF NOt EXISTS test_item_info ( - id SERIAL PRIMARY KEY, - barcode VARCHAR(255) NOT NULL, - linked_items INTEGER [], - shopping_lists INTEGER [], - recipes INTEGER [], - groups INTEGER [], - packaging VARCHAR(255), - uom VARCHAR(255), - cost FLOAT8, - safety_stock FLOAT8, - lead_time_days FLOAT8, - ai_pick BOOLEAN, - UNIQUE(barcode) -); \ No newline at end of file diff --git a/sites/test/sql/create/linked_items.sql b/sites/test/sql/create/linked_items.sql deleted file mode 100644 index 16a24e2..0000000 --- a/sites/test/sql/create/linked_items.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_itemlinks ( - id SERIAL PRIMARY KEY, - barcode VARCHAR(255) NOt NULL, - link INTEGER NOT NULL, - data JSONB NOT NULL, - conv_factor FLOAT8 NOt NULL, - UNIQUE(barcode) -); \ No newline at end of file diff --git a/sites/test/sql/create/locations.sql b/sites/test/sql/create/locations.sql deleted file mode 100644 index 32c6a98..0000000 --- a/sites/test/sql/create/locations.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_locations( - id SERIAL PRIMARY KEY, - name VARCHAR(32) NOT NULL, - zone_id INTEGER NOT NULL, - items JSONB, - UNIQUE(name), - CONSTRAINT fk_zone - FOREIGN KEY(zone_id) - REFERENCES test_zones(id) -); \ No newline at end of file diff --git a/sites/test/sql/create/logins.sql b/sites/test/sql/create/logins.sql deleted file mode 100644 index f69b01b..0000000 --- a/sites/test/sql/create/logins.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE TABLE IF NOT EXISTS logins( - id SERIAL PRIMARY KEY, - username VARCHAR(255), - password VARCHAR(255), - favorites JSONB, - unseen_pantry_items INTEGER [], - unseen_groups INTEGER [], - unseen_shopping_lists INTEGER [], - unseen_recipes INTEGER [], - seen_pantry_items INTEGER [], - seen_groups INTEGER[], - seen_shopping_lists INTEGER [], - seen_recipes INTEGER [], - flags JSONB -); - diff --git a/sites/test/sql/create/logistics_info.sql b/sites/test/sql/create/logistics_info.sql deleted file mode 100644 index ed6202f..0000000 --- a/sites/test/sql/create/logistics_info.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_logistics_info( - id SERIAL PRIMARY KEY, - barcode VARCHAR(255) NOT NULL, - primary_location VARCHAR(16), - auto_issue_location VARCHAR(16), - dynamic_locations JSONB, - location_data JSONB, - quantity_on_hand FLOAT8 NOT NULL, - UNIQUE(barcode) -); \ No newline at end of file diff --git a/sites/test/sql/create/transactions.sql b/sites/test/sql/create/transactions.sql deleted file mode 100644 index c781b14..0000000 --- a/sites/test/sql/create/transactions.sql +++ /dev/null @@ -1,15 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_Transactions ( - id SERIAL PRIMARY KEY, - timestamp TIMESTAMP, - logistics_info_id INTEGER NOT NULL, - barcode VARCHAR(255) NOT NULL, - name VARCHAR(255), - transaction_type VARCHAR(255) NOT NULL, - quantity FLOAT8 NOT NULL, - description TEXT, - user_id INTEGER NOT NULL, - data JSONB, - CONSTRAINT fk_logistics_info - FOREIGN KEY(logistics_info_id) - REFERENCES test_logistics_info(id) -); \ No newline at end of file diff --git a/sites/test/sql/create/zones.sql b/sites/test/sql/create/zones.sql deleted file mode 100644 index 0ed0a9e..0000000 --- a/sites/test/sql/create/zones.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE TABLE IF NOT EXISTS test_zones( - id SERIAL PRIMARY KEY, - name VARCHAR(32) NOT NULL, - UNIQUE(name) -); diff --git a/sites/test/sql/drop/brands.sql b/sites/test/sql/drop/brands.sql deleted file mode 100644 index f407a8c..0000000 --- a/sites/test/sql/drop/brands.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_brands CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/food_info.sql b/sites/test/sql/drop/food_info.sql deleted file mode 100644 index a4c3056..0000000 --- a/sites/test/sql/drop/food_info.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_food_info CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/groups.sql b/sites/test/sql/drop/groups.sql deleted file mode 100644 index 6b62f3b..0000000 --- a/sites/test/sql/drop/groups.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_groups CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/item_info.sql b/sites/test/sql/drop/item_info.sql deleted file mode 100644 index c84b0d1..0000000 --- a/sites/test/sql/drop/item_info.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_item_info CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/items.sql b/sites/test/sql/drop/items.sql deleted file mode 100644 index 097b9a7..0000000 --- a/sites/test/sql/drop/items.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_items CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/linked_items.sql b/sites/test/sql/drop/linked_items.sql deleted file mode 100644 index bcc9093..0000000 --- a/sites/test/sql/drop/linked_items.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_itemlinks CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/locations.sql b/sites/test/sql/drop/locations.sql deleted file mode 100644 index 4a1f42e..0000000 --- a/sites/test/sql/drop/locations.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_locations CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/logistics_info.sql b/sites/test/sql/drop/logistics_info.sql deleted file mode 100644 index cb5abcd..0000000 --- a/sites/test/sql/drop/logistics_info.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_logistics_info CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/transactions.sql b/sites/test/sql/drop/transactions.sql deleted file mode 100644 index 1356e47..0000000 --- a/sites/test/sql/drop/transactions.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_transactions CASCADE; \ No newline at end of file diff --git a/sites/test/sql/drop/zones.sql b/sites/test/sql/drop/zones.sql deleted file mode 100644 index c84c67b..0000000 --- a/sites/test/sql/drop/zones.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE test_zones CASCADE; \ No newline at end of file