def signup_post(): email = request.form.get('email') firstName = request.form.get('firstName') lastName = request.form.get('lastName') password = request.form.get('password') user = User.query.filter_by(email=email).first() # if this returns a user, then the email already exists in database if user: # if a user is found, we want to redirect back to signup page so user can try again flash('Email address already exists') return redirect(url_for('auth.signup')) # create a new user with the form data. Hash the password so the plaintext version isn't saved. new_user = User( email=email, firstName=firstName, lastName=lastName, password=generate_password_hash(password, method='sha256'), ) firebase_user = { "email": email, "firstName": firstName, "lastName": lastName, } # add the new user to the database db.session.add(new_user) db.session.commit() # add the new user to firebase user = User.query.filter_by(email=email).first() firebase_db.child("users").child(str(user.id)).push(firebase_user) return redirect(url_for('auth.login'))
def shoppinglist_remove(): name = request.form.get("name") for val in firebase_db.child("shoppinglists/" + str(current_user.id) + "/items").get().val(): if firebase_db.child("shoppinglists/" + str(current_user.id) + "/items/" + val).get().val()['name'] == name: firebase_db.child("shoppinglists/" + str(current_user.id) + "/items/" + val).remove() return redirect(url_for('main.shoppinglist'))
def inventory_remove(): name = request.form.get("name") for val in firebase_db.child("inventories/" + str(current_user.id) + "/items").get().val(): if firebase_db.child("inventories/" + str(current_user.id) + "/items/" + val).get().val()['name'] == name: firebase_db.child("inventories/" + str(current_user.id) + "/items/" + val).remove() return redirect(url_for('main.inventory'))
def shoppinglist_addtoinventory(): name = request.form.get("name") quantity = request.form.get("quantity") for val in firebase_db.child("shoppinglists/" + str(current_user.id) + "/items").get().val(): if firebase_db.child("shoppinglists/" + str(current_user.id) + "/items/" + val).get().val()['name'] == name: firebase_db.child("shoppinglists/" + str(current_user.id) + "/items/" + val).remove() if not firebase_db.child("inventories/" + str(current_user.id)).get().val(): firebase_db.child("inventories/" + str(current_user.id)).push( data={"owner": current_user.id}) new_item = { "name": name, "quantity": quantity, "expirationDate": datetime.today().strftime('%Y-%m-%d'), "datePurchased": datetime.today().strftime('%Y-%m-%d'), } firebase_db.child("inventories/" + str(current_user.id) + "/items").push(data=new_item) return redirect(url_for('main.shoppinglist'))
def shoppinglist_edit(): old_name = request.form.get("hidden") new_name = request.form.get("name") for val in firebase_db.child("shoppinglists/" + str(current_user.id) + "/items").get().val(): if firebase_db.child("shoppinglists/" + str(current_user.id) + "/items/" + val).get().val()['name'] == old_name: firebase_db.child("shoppinglists/" + str(current_user.id) + "/items/" + val).update({ "name": new_name, "quantity": request.form.get("quantity"), }) return redirect(url_for('main.shoppinglist'))
def inventory_edit(): old_name = request.form.get("hidden") new_name = request.form.get("name") for val in firebase_db.child("inventories/" + str(current_user.id) + "/items").get().val(): if firebase_db.child("inventories/" + str(current_user.id) + "/items/" + val).get().val()['name'] == old_name: firebase_db.child("inventories/" + str(current_user.id) + "/items/" + val).update({ "name": new_name, "quantity": request.form.get("quantity"), "expirationDate": request.form.get("expirationDate"), "datePurchased": request.form.get("datePurchased"), }) return redirect(url_for('main.inventory'))
def shoppinglist(): items_fb = firebase_db.child("shoppinglists/" + str(current_user.id) + "/items") items = items_fb.get() if request.method == 'GET': # If shoppinglist exists, display the current contents if items.val(): return render_template( 'shoppinglist.html', shoppinglist=items.val(), ) else: return render_template( 'shoppinglist.html', shoppinglist={}, ) else: new_item = { "name": request.form.get("name"), "quantity": request.form.get("quantity"), "expirationDate": request.form.get("expirationDate"), } # Make shoppinglist if it does not exist if not firebase_db.child("shoppinglists/" + str(current_user.id)).get().val(): firebase_db.child("shoppinglists/" + str(current_user.id)).push( data={"owner": current_user.id}) firebase_db.child("shoppinglists/" + str(current_user.id) + "/items").push(data=new_item) return redirect(url_for('main.shoppinglist'))