Merge branch 'ticket-13' into 'master'

fix issue with price when not valid float

Closes #13

See merge request engineering/make-post-sell/make_post_sell!14
This commit is contained in:
Russell Ballestrini 2024-06-23 01:59:16 +00:00
commit d9c1bd2c35
3 changed files with 40 additions and 28 deletions

View file

@ -14,7 +14,7 @@
<br/>
<form method="post" action="/p/new" onsubmit="submit.disabled = true; return true;">
<form method="post" action="/p/new" onsubmit="this.querySelector('input[type=submit]').style.display = 'none'; return true;">
<input type="hidden" name="shop_id" value="{{ request.shop.id }}" />

View file

@ -125,6 +125,7 @@ class AuthenticatedFunctionalTests(FunctionalTests):
"description": "russell's product",
"price": "3.50",
"is_sellable": "on",
"submit": True,
}
self.coupon1_params = {

View file

@ -108,37 +108,48 @@ def product_new(request):
is_physical = bool(request.params.get("is_physical", "false") == "true")
if title and description:
# prevent trying to create a bundle and also physical product.
if is_bundle and is_physical:
msg = (
"The bundle feature only works with digital goods. Please uncheck the bundle option or mark the product as digital.",
"error",
)
if "submit" in request.params:
# check for required fields.
if not title or not description or not price:
msg = ("You must fill out all fields.", "error")
request.session.flash(msg)
else:
product = Product(title, description)
product.shop = request.shop
product.is_bundle = is_bundle
product.is_sellable = is_sellable
product.is_physical = is_physical
if product.error_message:
request.session.flash((product.error_message, "error"))
else:
if is_sellable and price:
product_price = product.set_price(price)
request.dbsession.add(product_price)
request.dbsession.add(product)
request.dbsession.flush()
msg = ("Great, next you may upload files.", "success")
# validate price.
try:
price = float(price)
price_error = False
except ValueError:
price_error = True
msg = ("Please enter a valid number for price.", "error")
request.session.flash(msg)
return HTTPFound(f"/p/{product.id}/edit")
elif title or description or price:
msg = ("You must fill out all fields.", "error")
request.session.flash(msg)
if not price_error:
# prevent trying to create a bundle and also physical product.
if is_bundle and is_physical:
msg = (
"The bundle feature only works with digital goods. Please uncheck the bundle option or mark the product as digital.",
"error",
)
request.session.flash(msg)
else:
product = Product(title, description)
product.shop = request.shop
product.is_bundle = is_bundle
product.is_sellable = is_sellable
product.is_physical = is_physical
if product.error_message:
request.session.flash((product.error_message, "error"))
else:
if is_sellable and price:
product_price = product.set_price(price)
request.dbsession.add(product_price)
request.dbsession.add(product)
request.dbsession.flush()
msg = ("Great, next you may upload files.", "success")
request.session.flash(msg)
return HTTPFound(f"/p/{product.id}/edit")
return {
"title": title,