Started developing the webserver to build the
API against
This commit is contained in:
parent
138555c5b2
commit
c1b13a74c8
396
2024-10-02-Pantry.csv
Normal file
396
2024-10-02-Pantry.csv
Normal file
File diff suppressed because one or more lines are too long
BIN
__pycache__/api.cpython-312.pyc
Normal file
BIN
__pycache__/api.cpython-312.pyc
Normal file
Binary file not shown.
Binary file not shown.
26
api.py
Normal file
26
api.py
Normal file
@ -0,0 +1,26 @@
|
||||
from flask import Blueprint, request, render_template, redirect, session, url_for, send_file, jsonify, Response
|
||||
import psycopg2
|
||||
from config import config
|
||||
|
||||
database_api= Blueprint('database_api', __name__)
|
||||
|
||||
@database_api.route("/getItems")
|
||||
def pagninate_items():
|
||||
print("hello")
|
||||
page = int(request.args.get('page', 1))
|
||||
limit = int(request.args.get('limit', 10))
|
||||
offset = (page - 1) * limit
|
||||
|
||||
pantry_inventory = []
|
||||
|
||||
database_config = config()
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
sql = f"SELECT * FROM main_items LIMIT %s OFFSET %s;"
|
||||
cur.execute(sql, (limit, offset))
|
||||
pantry_inventory = cur.fetchall()
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
print(error)
|
||||
|
||||
return jsonify({'items': pantry_inventory})
|
||||
9
main.py
9
main.py
@ -244,6 +244,7 @@ def add_food_item(site_name: str, barcode: str, name: str, qty: float, payload:
|
||||
|
||||
defaults = config(filename=f"sites/{site_name}/site.ini", section="defaults")
|
||||
uuid = f"{defaults["default_zone"]}@{defaults["default_primary_location"]}"
|
||||
name = name.replace("'", "@&apostraphe&")
|
||||
payload["logistics_info"]["primary_location"] = uuid
|
||||
payload["logistics_info"]["auto_issue_location"] = uuid
|
||||
|
||||
@ -414,14 +415,14 @@ def parse_csv(path_to_csv):
|
||||
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)
|
||||
add_food_item(site_name="main", barcode=line[1], name=line[2], qty=qty, payload=payload)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
#print(add_readitem(site_name="main", barcode="1235", name="testone"))
|
||||
database_config = config()
|
||||
"""database_config = config()
|
||||
sql = "SELECT items FROM test_locations WHERE id=1;"
|
||||
with psycopg2.connect(**database_config) as conn:
|
||||
with conn.cursor() as cur:
|
||||
@ -430,5 +431,5 @@ if __name__ == "__main__":
|
||||
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")
|
||||
"""
|
||||
parse_csv(r"C:\\Users\\jadow\Downloads\\2024-10-02-Pantry.csv")
|
||||
@ -1,6 +1,11 @@
|
||||
import sys, os, shutil
|
||||
import main
|
||||
|
||||
"""
|
||||
Manage.py is where the databases and configuration is set up. Its a CLI for quick serving the databases necessary for
|
||||
MyPantry App.
|
||||
"""
|
||||
|
||||
def rename_drop_sql(site_name):
|
||||
files = os.walk(f"sites/{site_name}/sql/drop")
|
||||
|
||||
@ -42,8 +47,6 @@ def create():
|
||||
if default_zone_name == "":
|
||||
default_zone_name = "default"
|
||||
|
||||
print(f"\n")
|
||||
|
||||
print(f"Now you will set the default location that you wish for things to be received into (primary location) and used from (auto-issue).")
|
||||
default_location_name = input("Set Default Location (all): ").strip()
|
||||
if default_location_name == "":
|
||||
|
||||
81
templates/home.html
Normal file
81
templates/home.html
Normal file
@ -0,0 +1,81 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8" />
|
||||
<title>My Pantry</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@materializecss/materialize@2.0.3-alpha/dist/css/materialize.min.css" />
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/@materializecss/materialize@2.0.3-alpha/dist/js/materialize.min.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col s12">
|
||||
<ul class="collection" id="collection_list">
|
||||
<li class="collection-item">Alvin</li>
|
||||
<li class="collection-item">Alvin</li>
|
||||
<li class="collection-item">Alvin</li>
|
||||
<li class="collection-item">Alvin</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="divider col s12"></div>
|
||||
<div class="col s12 center">
|
||||
<a id="back" class="btn icon-left" href="#!"><i class="material-icons">chevron_left</i></a>
|
||||
<a id="forward" class="btn icon-right"><i class="material-icons">chevron_right</i></a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
let current_page = 1
|
||||
let limit = 25
|
||||
const checked_items = new Array();
|
||||
let instructions = new Array()
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
update_list()
|
||||
});
|
||||
|
||||
function update_list(){
|
||||
const url = new URL('/getItems', window.location.origin);
|
||||
url.searchParams.append('page', current_page);
|
||||
url.searchParams.append('limit', limit);
|
||||
fetch(url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.items.length < limit){
|
||||
document.getElementById('forward').classList.add("disabled")
|
||||
} else {
|
||||
document.getElementById('forward').classList.remove("disabled")
|
||||
}
|
||||
|
||||
const collection = document.getElementById('collection_list');
|
||||
while(collection.firstChild){
|
||||
collection.removeChild(collection.firstChild);
|
||||
}
|
||||
data.items.forEach(item => {
|
||||
const collection_item = document.createElement('li');
|
||||
collection_item.classList.add('collection-item');
|
||||
collection_item.innerHTML = `<div>${item[2]}
|
||||
<a href="#!" class="btn-small right" style="position: relative; bottom: 5px;"><i class="material-icons">send</i></a>
|
||||
<a href="#!" class="btn-small right" style="position: relative; bottom: 5px; right: 5px;"><i class="material-icons">edit</i></a>
|
||||
</div>`;
|
||||
collection.appendChild(collection_item);
|
||||
})
|
||||
})
|
||||
};
|
||||
|
||||
document.getElementById('forward').addEventListener('click', () =>{
|
||||
current_page++
|
||||
update_list();
|
||||
});
|
||||
|
||||
document.getElementById('back').addEventListener('click', () =>{
|
||||
current_page--
|
||||
update_list();
|
||||
});
|
||||
|
||||
</script>
|
||||
</html>
|
||||
|
||||
12
webserver.py
Normal file
12
webserver.py
Normal file
@ -0,0 +1,12 @@
|
||||
from flask import Flask, render_template
|
||||
import api
|
||||
app = Flask(__name__)
|
||||
|
||||
app.register_blueprint(api.database_api)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def home():
|
||||
return render_template("home.html")
|
||||
|
||||
app.run(host="127.0.0.1", debug=True)
|
||||
Loading…
x
Reference in New Issue
Block a user