diff --git a/README.md b/README.md index 1ae903f..f44c8c2 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,20 @@ -# GROCERY LIST - PYTHON FLASK PROJECT - -A simple python CRUD application that allows you to add grocery items to a list. This application uses Flask and SQLite3. -

- Picture -

- -## Live Demo: -https://grocery-list-python-app.herokuapp.com/ - -## Installation: - `pip -r requirements.txt` - - - - +# GROCERY LIST - PYTHON FLASK PROJECT + +A simple python CRUD application that allows you to add grocery items to a list. This application uses Flask and SQLite3. +

+ Picture +

+ +## Live Demo: +https://grocery-list-python-app.herokuapp.com/ + +## Installation: + `pip -r requirements.txt` + + + + diff --git a/grocerylist.db b/grocerylist.db index dac4d9a..85478b8 100644 Binary files a/grocerylist.db and b/grocerylist.db differ diff --git a/myflask.py b/myflask.py index a7b9174..853d083 100644 --- a/myflask.py +++ b/myflask.py @@ -1,133 +1,114 @@ -#!/usr/bin/python3 - -from flask import Flask, jsonify -from flask import session -from flask import render_template -from flask import redirect -from flask import url_for -from flask import request -import sqlite3 - -# module (__name__) as argument -app = Flask(__name__) -app.secret_key ="jfioefefjioawfnieofa" - -# establishes connection to sqlite database -conn = sqlite3.connect('grocerylist.db', check_same_thread=False) -print('Database connected') - -#creates table to hold users -conn.execute('''CREATE TABLE IF NOT EXISTS USER - (ID INTEGER PRIMARY KEY AUTOINCREMENT, - USERNAME TEXT NOT NULL - );''') - -# creates table to hold grocery items -conn.execute('''CREATE TABLE IF NOT EXISTS GROCERIES - (ID INTEGER PRIMARY KEY AUTOINCREMENT, - USERNAME TEXT NOT NULL, - ITEM TEXT NOT NULL, - QUANTITY INTEGER NOT NULL - );''') - -#--------- INDEX PAGE --------------------- -@app.route("/home", methods=["GET", "POST"]) -def index(): - username = session['username'] - - if "username" in session: - #---- GET ALL ITEMS -------- - if request.method == "GET": - cur = conn.cursor() - - # gets all items from database that match current user - cur.execute("SELECT ITEM, QUANTITY FROM GROCERIES WHERE USERNAME=?", (username,)) - data = cur.fetchall() - conn.commit() - - #updates page with results - return render_template('grocerylist.html', name = session["username"], groceries= data) - - # --- ADD ITEM ----------- - elif request.method == "POST": - item = request.form.get('groceryitem') - quantity = request.form.get("quantity") - conn.execute('INSERT INTO GROCERIES (USERNAME, ITEM, QUANTITY) VALUES (?, ?, ?)', (username, item, quantity)) - conn.commit() - - # returns back to list - return redirect(url_for("index")) - - # returns user to login page if not logged in - else: - return redirect(url_for("login")) - -#---------- DELETE ITEM ------------------ -@app.route("/delete", methods=["POST"]) -def deleteItem(): - # gets item from from and deletes it from database - item = request.form.get('item') - cur = conn.cursor() - cur.execute("DELETE FROM GROCERIES WHERE ITEM=?", (item,)) - conn.commit() - return redirect(url_for("index")) - -#--------- Users ------------------------ -@app.route("/users") -def getAllUsers(): - cur = conn.cursor() - - # gets all users and returns in JSON object - cur.execute("SELECT * FROM USER") - data = cur.fetchall() - print(data) - users =[] - for user in data: - item = {'id': user[0], 'username': user[1]} - users.append(item) - return jsonify(users) - -#---------- LOGIN --------------------- -@app.route("/", methods=["POST", "GET"]) -def login(): - - # if the user is in the current session then redirect to home page - if "username" in session: - username = session["username"] - return redirect(url_for("index")) - - # post action to log in - elif request.method == "POST": - #saves user to current session - session["username"] = request.form.get("username") - cur = conn.cursor() - - # searches for user in database - cur.execute("SELECT USERNAME FROM USER WHERE USERNAME=?", (session["username"],)) - data = cur.fetchall() - - # if user is not in database it adds the user - if len(data) == 0: - cur.execute("INSERT INTO USER (USERNAME) VALUES(?)", (session['username'],)) - - # commits changes to database - conn.commit() - - return redirect(url_for("index")) - - # redirects to login page if none of the above are met - else: - return render_template('login.html') - -#---------- LOGOUT --------------------- -@app.route("/logout", methods=["GET"]) -def logout(): - #removes user from current session - session.pop("username", None) - return redirect(url_for("login")) - -print("Operation done successfully") - - -if __name__ == "__main__": - app.run(host="0.0.0.0", port=2224) # runs the application +#!/usr/bin/python3 + +from flask import Flask, jsonify, session, render_template, redirect, url_for, request +import sqlite3 + +app = Flask(__name__) +app.secret_key = "jfioefefjioawfnieofa" + +# establishes connection to sqlite database +conn = sqlite3.connect('grocerylist.db', check_same_thread=False) +print('Database connected') + +# creates table to hold users +conn.execute('''CREATE TABLE IF NOT EXISTS USER + (ID INTEGER PRIMARY KEY AUTOINCREMENT, + USERNAME TEXT NOT NULL + );''') + +# creates table to hold grocery items +conn.execute('''CREATE TABLE IF NOT EXISTS GROCERIES + (ID INTEGER PRIMARY KEY AUTOINCREMENT, + USERNAME TEXT NOT NULL, + ITEM TEXT NOT NULL, + QUANTITY INTEGER NOT NULL + );''') + +#--------- INDEX PAGE --------------------- +@app.route("/home", methods=["GET", "POST"]) +def index(): + if "username" in session: + username = session['username'] + if request.method == "GET": + cur = conn.cursor() + search = request.args.get('search') + if search: + # Filter items by search term (case-insensitive) + cur.execute("SELECT ITEM, QUANTITY FROM GROCERIES WHERE USERNAME=? AND LOWER(ITEM) LIKE ?", (username, f"%{search.lower()}%")) + else: + cur.execute("SELECT ITEM, QUANTITY FROM GROCERIES WHERE USERNAME=?", (username,)) + data = cur.fetchall() + return render_template('grocerylist.html', name=username, groceries=data) + elif request.method == "POST": + item = request.form.get('groceryitem') + quantity = request.form.get("quantity") + if item and quantity: + conn.execute('INSERT INTO GROCERIES (USERNAME, ITEM, QUANTITY) VALUES (?, ?, ?)', (username, item, quantity)) + conn.commit() + return redirect(url_for("index")) + else: + return redirect(url_for("login")) + +#---------- UPDATE ITEM ------------------ +@app.route("/update", methods=["POST"]) +def updateItem(): + old_item = request.form.get('old_item') + new_item = request.form.get('new_item') + new_quantity = request.form.get('new_quantity') + username = session.get('username') + if old_item and new_item and new_quantity and username: + cur = conn.cursor() + cur.execute("UPDATE GROCERIES SET ITEM=?, QUANTITY=? WHERE ITEM=? AND USERNAME=?", (new_item, new_quantity, old_item, username)) + conn.commit() + return redirect(url_for("index")) + +#---------- DELETE ITEM ------------------ +@app.route("/delete", methods=["POST"]) +def deleteItem(): + item = request.form.get('item') + username = session.get('username') + if item and username: + cur = conn.cursor() + cur.execute("DELETE FROM GROCERIES WHERE ITEM=? AND USERNAME=?", (item, username)) + conn.commit() + return redirect(url_for("index")) + +#--------- Users ------------------------ +@app.route("/users") +def getAllUsers(): + cur = conn.cursor() + cur.execute("SELECT * FROM USER") + data = cur.fetchall() + users = [] + for user in data: + item = {'id': user[0], 'username': user[1]} + users.append(item) + return jsonify(users) + +#---------- LOGIN --------------------- +@app.route("/", methods=["POST", "GET"]) +def login(): + if "username" in session: + return redirect(url_for("index")) + elif request.method == "POST": + session["username"] = request.form.get("username") + cur = conn.cursor() + cur.execute("SELECT USERNAME FROM USER WHERE USERNAME=?", (session["username"],)) + data = cur.fetchall() + if len(data) == 0: + cur.execute("INSERT INTO USER (USERNAME) VALUES(?)", (session['username'],)) + conn.commit() + return redirect(url_for("index")) + else: + return render_template('login.html') + +#---------- LOGOUT --------------------- +@app.route("/logout", methods=["GET"]) +def logout(): + session.pop("username", None) + return redirect(url_for("login")) + +print("Operation done successfully") + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=2224) \ No newline at end of file diff --git a/request.py b/request.py index ae151b4..87b88f2 100644 --- a/request.py +++ b/request.py @@ -1,17 +1,17 @@ -import requests - -def main(): - #gets users from api - URL= "http://127.0.0.1:2224/users" - - # Get request to url and stores repsonse - response = requests.get(URL) - - # turns response to python object - users = response.json() - - #prints users in database - print(users) - -if __name__ == "__main__": +import requests + +def main(): + #gets users from api + URL= "http://127.0.0.1:2224/users" + + # Get request to url and stores repsonse + response = requests.get(URL) + + # turns response to python object + users = response.json() + + #prints users in database + print(users) + +if __name__ == "__main__": main() \ No newline at end of file diff --git a/templates/grocerylist.html b/templates/grocerylist.html index 3c8b6fc..22ec850 100644 --- a/templates/grocerylist.html +++ b/templates/grocerylist.html @@ -1,56 +1,135 @@ - - - - - - - - - Grocery List - - - -
-

{{name}}'s Grocery List

-
-
-
- -
- -
- -
-
-
-
- - - {% for x in groceries%} -
-
- {{ x[1] }} - {{ x[0].lower() }} - - -
-
- {% endfor %} - - - Sign Out -
- + + + + + + + {{name}}'s Grocery List + + + + + +
+
+

{{name}}'s Grocery List

+ + +
+
+ + +
+
+ + +
+
+
+ +
+
+ +
+
+ +
+
+
+ +
+ + +
+ {% for x in groceries %} +
+
+
+ {{ x[1] }} - {{ x[0].lower() }} + +
+
+ + +
+
+
+ {% endfor %} +
+ + Sign Out +
+
+ + + + + + \ No newline at end of file diff --git a/templates/login.html b/templates/login.html index 4f36108..19801a5 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,25 +1,25 @@ - - - - - - - - - Grocery List - - - -
-

Login

-
-
- - -
Please enter a username to login
-
- -
-
- + + + + + + + + + Grocery List + + + +
+

Login

+
+
+ + +
Please enter a username to login
+
+ +
+
+ \ No newline at end of file