Added a bit of style to edit.html
Added buy capability to view with dialog Added additional properties to Item (url and imageurl); NEEDS TESTING Made delete items work properly
This commit is contained in:
parent
3ba6099976
commit
05c40d0148
9 changed files with 222 additions and 82 deletions
65
app/views.py
65
app/views.py
|
|
@ -1,4 +1,4 @@
|
|||
from flask import url_for, redirect, render_template
|
||||
from flask import url_for, redirect, render_template, abort
|
||||
from app import app, db
|
||||
from app.forms import (
|
||||
NewWishlist,
|
||||
|
|
@ -7,6 +7,8 @@ from app.forms import (
|
|||
ResetWishlistUrls,
|
||||
NewItem,
|
||||
DeleteItem,
|
||||
CheckItem,
|
||||
parseHiddenIndex,
|
||||
)
|
||||
from app.models import Wishlist, Item
|
||||
from uuid import UUID, uuid4 as uuid
|
||||
|
|
@ -21,7 +23,7 @@ def index():
|
|||
def new():
|
||||
form = NewWishlist()
|
||||
if form.validate_on_submit():
|
||||
wishlist = Wishlist(form.title.data, form.description.data)
|
||||
wishlist = Wishlist(str(form.title.data), str(form.description.data))
|
||||
db.session.add(wishlist)
|
||||
db.session.commit()
|
||||
|
||||
|
|
@ -44,7 +46,7 @@ def edit(id: str):
|
|||
form_it_delete = DeleteItem()
|
||||
|
||||
# Each submit needs a different page fot it to work on the same page.
|
||||
if form_wl_delete.validate_on_submit() and form_wl_delete.wl_del_submit.data:
|
||||
if form_wl_delete.validate_on_submit() and form_wl_delete.wl_del_submit.data:
|
||||
for i in wishlist.items:
|
||||
db.session.delete(i)
|
||||
db.session.delete(wishlist)
|
||||
|
|
@ -52,12 +54,15 @@ def edit(id: str):
|
|||
|
||||
return redirect(url_for("index"))
|
||||
elif form_wl_editinfo.validate_on_submit() and form_wl_editinfo.wl_edit_submit.data:
|
||||
wishlist.title = form_wl_editinfo.title.data
|
||||
wishlist.description = form_wl_editinfo.description.data
|
||||
wishlist.title = str(form_wl_editinfo.title.data)
|
||||
wishlist.description = str(form_wl_editinfo.description.data)
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for("edit", id=id))
|
||||
elif form_wl_reseturls.validate_on_submit() and form_wl_reseturls.wl_reset_submit.data:
|
||||
elif (
|
||||
form_wl_reseturls.validate_on_submit()
|
||||
and form_wl_reseturls.wl_reset_submit.data
|
||||
):
|
||||
wishlist.editId = uuid()
|
||||
wishlist.viewId = uuid()
|
||||
db.session.commit()
|
||||
|
|
@ -65,27 +70,36 @@ def edit(id: str):
|
|||
return redirect(url_for("edit", id=wishlist.editId))
|
||||
elif form_it_new.validate_on_submit() and form_it_new.it_new_submit.data:
|
||||
f = form_it_new
|
||||
price = f.price.data if f.price.data != None else 0
|
||||
|
||||
item = Item(
|
||||
f.title.data,
|
||||
f.description.data,
|
||||
f.price.data
|
||||
str(
|
||||
f.title.data,
|
||||
),
|
||||
str(
|
||||
f.description.data,
|
||||
),
|
||||
price,
|
||||
str(
|
||||
f.url.data,
|
||||
),
|
||||
str(
|
||||
f.image.data,
|
||||
),
|
||||
)
|
||||
wishlist.items.append(item)
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for("edit", id=id))
|
||||
elif form_it_delete.validate_on_submit() and form_it_delete.it_del_submit.data:
|
||||
print(form_it_delete.index.data)
|
||||
data = form_it_delete.index.data
|
||||
i = int(data) if data != None else 0
|
||||
if i + 1 > len(wishlist.items):
|
||||
return "Invalid item to delete", 400
|
||||
|
||||
wishlist.items.remove(wishlist.items[i])
|
||||
index = parseHiddenIndex(form_it_delete.index, wishlist.items)
|
||||
if index == None:
|
||||
return abort(400)
|
||||
|
||||
wishlist.items.remove(wishlist.items[index])
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for("edit", id=id))
|
||||
|
||||
|
||||
return render_template(
|
||||
"edit.html",
|
||||
|
|
@ -98,11 +112,22 @@ def edit(id: str):
|
|||
)
|
||||
|
||||
|
||||
@app.route("/view/<id>")
|
||||
@app.route("/view/<id>", methods=["GET", "POST"])
|
||||
def view(id: str):
|
||||
wishlist = db.one_or_404(
|
||||
wishlist: Wishlist = db.one_or_404(
|
||||
db.select(Wishlist).filter_by(viewId=UUID(id)),
|
||||
description="Failed to get wishlist. Are you sure this is the correct url?",
|
||||
)
|
||||
checkform = CheckItem()
|
||||
checkform.num
|
||||
if checkform.validate_on_submit():
|
||||
index = parseHiddenIndex(checkform.num, wishlist.items)
|
||||
if index == None:
|
||||
return abort(400)
|
||||
|
||||
return render_template("view.html", wishlist=wishlist)
|
||||
wishlist.items[index].bought = True
|
||||
db.session.commit()
|
||||
|
||||
return redirect(url_for("view", id=id))
|
||||
|
||||
return render_template("view.html", wishlist=wishlist, form=checkform)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue