diff --git a/__pycache__/api.cpython-312.pyc b/__pycache__/api.cpython-312.pyc index 495adc9..65cb803 100644 Binary files a/__pycache__/api.cpython-312.pyc and b/__pycache__/api.cpython-312.pyc differ diff --git a/__pycache__/main.cpython-312.pyc b/__pycache__/main.cpython-312.pyc index 0830de1..ba529c2 100644 Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ diff --git a/__pycache__/webserver.cpython-312.pyc b/__pycache__/webserver.cpython-312.pyc new file mode 100644 index 0000000..0020478 Binary files /dev/null and b/__pycache__/webserver.cpython-312.pyc differ diff --git a/api.py b/api.py index 646cb9d..01dc3a1 100644 --- a/api.py +++ b/api.py @@ -4,12 +4,59 @@ from config import config database_api= Blueprint('database_api', __name__) + +def paginate_with_params(cur, limit, offset, params): + sql = f"SELECT * FROM main_items LEFT JOIN main_logistics_info ON main_items.logistics_info_id = main_logistics_info.id" + count = f"SELECT COUNT(*) FROM main_items LEFT JOIN main_logistics_info ON main_items.logistics_info_id = main_logistics_info.id" + # WHERE search_string LIKE '%{search_string}%' + strings = [] + count_strings = [] + if params['search_string'] != "": + s = params['search_string'] + strings.append(f" search_string LIKE '%{s}%'") + count_strings.append(f" search_string LIKE '%{s}%'") + + if params['view'] == 1: + s = params['view'] + strings.append(f" main_logistics_info.quantity_on_hand <> 0.00") + count_strings.append(f" main_logistics_info.quantity_on_hand <> 0.00") + + + # LIMIT {limit} OFFSET {offset};" + + if len(strings) > 0: + sql = f"{sql} WHERE{" AND".join(strings)}" + + if len(count_strings) > 0: + count = f"{count} WHERE{" AND".join(count_strings)}" + + sql = f"{sql} ORDER BY main_logistics_info.quantity_on_hand LIMIT {limit} OFFSET {offset};" + count = f"{count};" + print(count) + print(sql) + cur.execute(sql) + pantry_inventory = cur.fetchall() + cur.execute(count) + count = cur.fetchone()[0] + return pantry_inventory, count + +def paginate_default(cur, limit, offset): + sql = f"SELECT * FROM main_items LEFT JOIN main_logistics_info ON main_items.logistics_info_id = main_logistics_info.id LIMIT %s OFFSET %s;" + cur.execute(sql, (limit, offset)) + pantry_inventory = cur.fetchall() + cur.execute("SELECT COUNT(*) FROM main_items;") + count = cur.fetchone()[0] + return pantry_inventory, count + + @database_api.route("/getItems") def pagninate_items(): print("hello") page = int(request.args.get('page', 1)) limit = int(request.args.get('limit', 10)) search_string = str(request.args.get('search_text', "")) + sort_order = int(request.args.get('sort_order', 1)) + view = int(request.args.get('view', 0)) offset = (page - 1) * limit @@ -20,26 +67,22 @@ def pagninate_items(): with psycopg2.connect(**database_config) as conn: try: with conn.cursor() as cur: - if search_string != "": - sql = f"SELECT * FROM main_items LEFT JOIN main_logistics_info ON main_items.logistics_info_id = main_logistics_info.id WHERE search_string LIKE '%{search_string}%' LIMIT {limit} OFFSET {offset};" - cur.execute(sql) - pantry_inventory = cur.fetchall() - cur.execute(f"SELECT COUNT(*) FROM main_items WHERE search_string LIKE '%{search_string}%';") - count = cur.fetchone()[0] - else: - sql = f"SELECT * FROM main_items LEFT JOIN main_logistics_info ON main_items.logistics_info_id = main_logistics_info.id LIMIT %s OFFSET %s;" - cur.execute(sql, (limit, offset)) - pantry_inventory = cur.fetchall() - cur.execute("SELECT COUNT(*) FROM main_items;") - count = cur.fetchone()[0] - - print(sql) - print(count, math.ceil(count/limit)) - + pantry_inventory, count = paginate_with_params( + cur, limit, offset, + {'search_string': search_string, + 'view': view} + ) except (Exception, psycopg2.DatabaseError) as error: print(error) - pantry_inventory = sorted(pantry_inventory, key=lambda x: x[2]) + if sort_order == 0: + pantry_inventory = sorted(pantry_inventory, key=lambda x: x[1]) + + if sort_order == 1: + pantry_inventory = sorted(pantry_inventory, key=lambda x: x[2]) + + if sort_order == 2: + pantry_inventory = sorted(pantry_inventory, key=lambda x: x[18]) return jsonify({'items': pantry_inventory, "end": math.ceil(count/limit)}) @@ -63,4 +106,17 @@ def get_item(): except (Exception, psycopg2.DatabaseError) as error: print(error) - return render_template(f"item_page/index.html", item=item) \ No newline at end of file + return render_template(f"item_page/index.html", item=item) + +@database_api.route("/addGroup") +def addGroup(): + name = str(request.args.get('name', "")) + description = str(request.args.get('description', "")) + group_type = str(request.args.get('type', "")) + + state = "FAILED" + + if name or description or group_type == "": + print("this is empty") + + return jsonify({'state': state}) \ No newline at end of file diff --git a/main.py b/main.py index 094085f..dc996f3 100644 --- a/main.py +++ b/main.py @@ -310,6 +310,11 @@ def delete_site(site_name): drop_table(f'sites/{site_name}/sql/drop/logistics_info.sql') drop_table(f'sites/{site_name}/sql/drop/zones.sql') drop_table(f'sites/{site_name}/sql/drop/locations.sql') + drop_table(f'sites/{site_name}/sql/drop/vendors.sql') + drop_table(f'sites/{site_name}/sql/drop/receipt_items.sql') + drop_table(f'sites/{site_name}/sql/drop/receipts.sql') + drop_table(f'sites/{site_name}/sql/drop/recipes.sql') + drop_table(f'sites/{site_name}/sql/drop/shopping_lists.sql') def create_site(site_name): @@ -326,6 +331,12 @@ def create_site(site_name): create_table(f'sites/{site_name}/sql/create/item.sql') create_table(f'sites/{site_name}/sql/create/zones.sql') create_table(f'sites/{site_name}/sql/create/locations.sql') + create_table(f'sites/{site_name}/sql/create/vendors.sql') + create_table(f'sites/{site_name}/sql/create/receipt_items.sql') + create_table(f'sites/{site_name}/sql/create/receipts.sql') + create_table(f'sites/{site_name}/sql/create/recipes.sql') + create_table(f'sites/{site_name}/sql/create/shopping_lists.sql') + sql = f"INSERT INTO {site_name}_zones(name) VALUES (%s) RETURNING id;" sqltwo = f"INSERT INTO {site_name}_locations(uuid, name, zone_id, items) VALUES (%s, %s, %s, %s);" @@ -411,7 +422,8 @@ def parse_csv(path_to_csv): if line[0] != "id": payload["item_info"]["packaging"] = line[10] payload["item_info"]["uom"] = line[13] - payload["item_info"]["cost"] = line[15] + if line[15] != "": + payload["item_info"]["cost"] = line[15] if line[17] != "None": payload["item_info"]["safety_stock"] = line[17] qty = float(line[30]) @@ -432,4 +444,4 @@ if __name__ == "__main__": for k, v in items.items(): print(f"{k}: {v}") """ - parse_csv(r"C:\\Users\\jadow\Downloads\\2024-10-02-Pantry.csv") \ No newline at end of file + parse_csv(r"C:\\Users\\jadow\\Documents\\code\\postgresql python\\postgresql-python\\2024-10-02-Pantry.csv") \ No newline at end of file diff --git a/manage.py b/manage.py index a1d9867..9f84298 100644 --- a/manage.py +++ b/manage.py @@ -6,6 +6,22 @@ Manage.py is where the databases and configuration is set up. Its a CLI for quic MyPantry App. """ +def rename_unique_sql(site_name): + files = os.walk(f"sites/{site_name}/sql/unique") + + sql_files = [] + for file in files: + sql_files = file[2] + + for file_name in sql_files: + words = None + with open(f"sites/{site_name}/sql/unique/{file_name}", "r") as file: + words = file.read() + words = words.replace("%sitename%", site_name) + + with open(f"sites/{site_name}/sql/unique/{file_name}", "w") as file: + file.write(words) + def rename_drop_sql(site_name): files = os.walk(f"sites/{site_name}/sql/drop") @@ -60,6 +76,7 @@ def create(): shutil.copytree(f"sites/default/sql", f"sites/{site_name}/sql") rename_create_sql(site_name) rename_drop_sql(site_name) + rename_unique_sql(site_name) with open(f"sites/{site_name}/site.ini", "w+") as config: config.write(f"[site]\n") diff --git a/sites/default/sql/create/item.sql b/sites/default/sql/create/item.sql index 33109d8..2d6081d 100644 --- a/sites/default/sql/create/item.sql +++ b/sites/default/sql/create/item.sql @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS %sitename%_items( barcode VARCHAR(255) NOT NULL, item_name VARCHAR(255) NOT NULL, brand INTEGER, + description TEXT; tags TEXT [], links JSONB, item_info_id INTEGER NOT NULL, diff --git a/sites/default/sql/create/receipt_items.sql b/sites/default/sql/create/receipt_items.sql index 0b20778..6cbfb64 100644 --- a/sites/default/sql/create/receipt_items.sql +++ b/sites/default/sql/create/receipt_items.sql @@ -6,4 +6,4 @@ CREATE TABLE IF NOT EXISTS %sitename%_receipt_items ( qty FLOAT8 NOT NULL, data JSONB, status VARCHAR (64) -) \ No newline at end of file +); \ No newline at end of file diff --git a/sites/default/sql/create/vendors.sql b/sites/default/sql/create/vendors.sql index ea7b7ee..d3512a7 100644 --- a/sites/default/sql/create/vendors.sql +++ b/sites/default/sql/create/vendors.sql @@ -5,4 +5,4 @@ CREATE TABLE IF NOT EXISTS %sitename%_vendors ( creation_date TIMESTAMP NOT NULL, created_by TIMESTAMP NOT NULL, phone_number VARCHAR(32) -) \ No newline at end of file +); \ No newline at end of file diff --git a/sites/default/sql/drop/receipt_items.sql b/sites/default/sql/drop/receipt_items.sql new file mode 100644 index 0000000..3dea741 --- /dev/null +++ b/sites/default/sql/drop/receipt_items.sql @@ -0,0 +1 @@ +DROP TABLE %sitename%_receipt_items CASCADE; \ No newline at end of file diff --git a/sites/default/sql/drop/receipts.sql b/sites/default/sql/drop/receipts.sql new file mode 100644 index 0000000..c9b266a --- /dev/null +++ b/sites/default/sql/drop/receipts.sql @@ -0,0 +1 @@ +DROP TABLE %sitename%_receipts CASCADE; \ No newline at end of file diff --git a/sites/default/sql/drop/recipes.sql b/sites/default/sql/drop/recipes.sql new file mode 100644 index 0000000..a672a7f --- /dev/null +++ b/sites/default/sql/drop/recipes.sql @@ -0,0 +1 @@ +DROP TABLE %sitename%_recipes CASCADE; \ No newline at end of file diff --git a/sites/default/sql/drop/shopping_lists.sql b/sites/default/sql/drop/shopping_lists.sql new file mode 100644 index 0000000..3591aa4 --- /dev/null +++ b/sites/default/sql/drop/shopping_lists.sql @@ -0,0 +1 @@ +DROP TABLE %sitename%_shopping_lists CASCADE; \ No newline at end of file diff --git a/sites/default/sql/drop/vendors.sql b/sites/default/sql/drop/vendors.sql new file mode 100644 index 0000000..8a68124 --- /dev/null +++ b/sites/default/sql/drop/vendors.sql @@ -0,0 +1 @@ +DROP TABLE %sitename%_vendors CASCADE; \ No newline at end of file diff --git a/sites/default/sql/unique/select_item_all.sql b/sites/default/sql/unique/select_item_all.sql index bdc2894..8967b1c 100644 --- a/sites/default/sql/unique/select_item_all.sql +++ b/sites/default/sql/unique/select_item_all.sql @@ -9,36 +9,37 @@ WHERE %sitename%_items.id=%s; 01 - barcode 02 - item_name 03 - brand (id) -04 - tags -05 - links -06 - item_info_id -07 - logistics_info_id -08 - food_info_id -09 - row_type -10 - item_type -11 - search_string -12 - logistics_info_id -13 - barcode -14 - primary_location -15 - auto_issue_location -16 - dynamic_locations -17 - location_data -18 - quantity_on_hand -19 - item_info_id -20 - barcode -21 - linked_items -22 - shopping_lists -23 - recipes -24 - groups -25 - packaging -26 - uom -27 - cost -28 - safety_stock -29 - lead_time_days -30 - ai_pick -31 - food_info_id -32 - food_groups -33 - ingrediants -34 - nutrients -35 - expires +04 - description +05 - tags +06 - links +07 - item_info_id +08 - logistics_info_id +09 - food_info_id +10 - row_type +11 - item_type +12 - search_string +13 - logistics_info_id +14 - barcode +15 - primary_location +16 - auto_issue_location +17 - dynamic_locations +18 - location_data +19 - quantity_on_hand +20 - item_info_id +21 - barcode +22 - linked_items +23 - shopping_lists +24 - recipes +25 - groups +26 - packaging +27 - uom +28 - cost +29 - safety_stock +30 - lead_time_days +31 - ai_pick +32 - food_info_id +33 - food_groups +34 - ingrediants +35 - nutrients +36 - expires */ \ No newline at end of file diff --git a/sites/main/site.ini b/sites/main/site.ini index d08dec8..2d7e2e6 100644 --- a/sites/main/site.ini +++ b/sites/main/site.ini @@ -1,6 +1,6 @@ [site] site_name=main -site_owner=jadowyne +site_owner= email= [defaults] diff --git a/sites/main/sql/create/item.sql b/sites/main/sql/create/item.sql index 5f1c027..d736b6b 100644 --- a/sites/main/sql/create/item.sql +++ b/sites/main/sql/create/item.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS main_items( item_name VARCHAR(255) NOT NULL, brand INTEGER, tags TEXT [], - links TEXT [], + links JSONB, item_info_id INTEGER NOT NULL, logistics_info_id INTEGER NOT NULL, food_info_id INTEGER, diff --git a/sites/main/sql/create/receipt_items.sql b/sites/main/sql/create/receipt_items.sql new file mode 100644 index 0000000..6ab19d5 --- /dev/null +++ b/sites/main/sql/create/receipt_items.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS main_receipt_items ( + id SERIAL PRIMARY KEY, + type VARCHAR(255) NOT NULL, + barcode VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + qty FLOAT8 NOT NULL, + data JSONB, + status VARCHAR (64) +); \ No newline at end of file diff --git a/sites/main/sql/create/receipts.sql b/sites/main/sql/create/receipts.sql new file mode 100644 index 0000000..f0859d2 --- /dev/null +++ b/sites/main/sql/create/receipts.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS main_receipts ( + id SERIAL PRIMARY KEY, + receipt_id INTEGER NOT NULL, + receipt_status VARCHAR (64) NOT NULL, + date_submitted TIMESTAMP NOT NULL, + submitted_by INTEGER NOT NULL, + vendor_id INTEGER, + files JSONB, + UNIQUE(receipt_id) +); \ No newline at end of file diff --git a/sites/main/sql/create/recipes.sql b/sites/main/sql/create/recipes.sql new file mode 100644 index 0000000..a4a267e --- /dev/null +++ b/sites/main/sql/create/recipes.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS main_recipes ( + id SERIAL PRIMARY KEY, + name VARCHAR, + author INTEGER, + description TEXT, + creation_date TIMESTAMP, + custom_items JSONB, + pantry_items JSONB, + group_items JSONB, + instructions TEXT [], + picture_path TEXT +); \ No newline at end of file diff --git a/sites/main/sql/create/shopping_lists.sql b/sites/main/sql/create/shopping_lists.sql new file mode 100644 index 0000000..ec12db3 --- /dev/null +++ b/sites/main/sql/create/shopping_lists.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS main_shopping_lists ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + description TEXT, + pantry_items JSONB, + custom_items JSONB, + recipes JSONB, + groups JSONB, + author INTEGER, + creation_date TIMESTAMP, + type VARCHAR(64), + UNIQUE(name) +); \ No newline at end of file diff --git a/sites/main/sql/create/vendors.sql b/sites/main/sql/create/vendors.sql new file mode 100644 index 0000000..50d498c --- /dev/null +++ b/sites/main/sql/create/vendors.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS main_vendors ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + address VARCHAR(255), + creation_date TIMESTAMP NOT NULL, + created_by TIMESTAMP NOT NULL, + phone_number VARCHAR(32) +); \ No newline at end of file diff --git a/sites/main/sql/drop/receipt_items.sql b/sites/main/sql/drop/receipt_items.sql new file mode 100644 index 0000000..8c7d640 --- /dev/null +++ b/sites/main/sql/drop/receipt_items.sql @@ -0,0 +1 @@ +DROP TABLE main_receipt_items CASCADE; \ No newline at end of file diff --git a/sites/main/sql/drop/receipts.sql b/sites/main/sql/drop/receipts.sql new file mode 100644 index 0000000..48af77b --- /dev/null +++ b/sites/main/sql/drop/receipts.sql @@ -0,0 +1 @@ +DROP TABLE main_receipts CASCADE; \ No newline at end of file diff --git a/sites/main/sql/drop/recipes.sql b/sites/main/sql/drop/recipes.sql new file mode 100644 index 0000000..c680d90 --- /dev/null +++ b/sites/main/sql/drop/recipes.sql @@ -0,0 +1 @@ +DROP TABLE main_recipes CASCADE; \ No newline at end of file diff --git a/sites/main/sql/drop/shopping_lists.sql b/sites/main/sql/drop/shopping_lists.sql new file mode 100644 index 0000000..db181d9 --- /dev/null +++ b/sites/main/sql/drop/shopping_lists.sql @@ -0,0 +1 @@ +DROP TABLE main_shopping_lists CASCADE; \ No newline at end of file diff --git a/sites/main/sql/drop/vendors.sql b/sites/main/sql/drop/vendors.sql new file mode 100644 index 0000000..e20f2a5 --- /dev/null +++ b/sites/main/sql/drop/vendors.sql @@ -0,0 +1 @@ +DROP TABLE main_vendors CASCADE; \ No newline at end of file diff --git a/sites/main/sql/unique/select_item_all.sql b/sites/main/sql/unique/select_item_all.sql index 42b69ea..b5ca580 100644 --- a/sites/main/sql/unique/select_item_all.sql +++ b/sites/main/sql/unique/select_item_all.sql @@ -2,4 +2,43 @@ SELECT * FROM main_items LEFT JOIN main_logistics_info ON main_items.logistics_info_id = main_logistics_info.id LEFT JOIN main_item_info ON main_items.item_info_id = main_item_info.id LEFT JOIN main_food_info ON main_items.food_info_id = main_food_info.id -WHERE main_items.id=%s; \ No newline at end of file +WHERE main_items.id=%s; + +/* +00 - item_id +01 - barcode +02 - item_name +03 - brand (id) +04 - tags +05 - links +06 - item_info_id +07 - logistics_info_id +08 - food_info_id +09 - row_type +10 - item_type +11 - search_string +12 - logistics_info_id +13 - barcode +14 - primary_location +15 - auto_issue_location +16 - dynamic_locations +17 - location_data +18 - quantity_on_hand +19 - item_info_id +20 - barcode +21 - linked_items +22 - shopping_lists +23 - recipes +24 - groups +25 - packaging +26 - uom +27 - cost +28 - safety_stock +29 - lead_time_days +30 - ai_pick +31 - food_info_id +32 - food_groups +33 - ingrediants +34 - nutrients +35 - expires +*/ \ No newline at end of file diff --git a/templates/home.html b/templates/home.html index 8269b11..0aaf1d4 100644 --- a/templates/home.html +++ b/templates/home.html @@ -4,78 +4,177 @@ My Pantry + + + + + + + + -
+
search
- tune + tune
-
-
+
+ +
+
+

Change Views

+
+
+ +
+
+ +
+
+
+ +
+
+

Sort Items By

+
+
+ +
+
+ +
+
+ +
+
+
+ +
-

Item Limit

+

Set Items Per Page

-
+
-
+
-
+
-
+
+
+ +
+
+ +
-
-
+
+
+
+
+ + + +
+
+
+
+
+
+

Welcome to you personalized workshop, this is where you can add/create/modify + alot of the items and functions in your site. These modals will walk you through what basic info you will need + in order to further expand on your site.

+
+
+
+ Add Group +
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+ + + + + + \ No newline at end of file diff --git a/webserver.py b/webserver.py index 3cc61cd..b12ddea 100644 --- a/webserver.py +++ b/webserver.py @@ -4,6 +4,9 @@ app = Flask(__name__) app.register_blueprint(api.database_api) +@app.route("/workshop") +def workshop(): + return render_template("workshop.html") @app.route("/") def home():