From 54ef59c8e52378f945ac60eeeb1ed38cf5945473 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 23 Jun 2024 01:59:16 +0000 Subject: [PATCH] fix issue with price when not valid float --- make_post_sell/templates/product_new.j2 | 2 +- make_post_sell/tests/test_functional.py | 1 + make_post_sell/views/product.py | 65 +++++++++++++++---------- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/make_post_sell/templates/product_new.j2 b/make_post_sell/templates/product_new.j2 index e68e00b..9c05459 100644 --- a/make_post_sell/templates/product_new.j2 +++ b/make_post_sell/templates/product_new.j2 @@ -14,7 +14,7 @@
-
+ diff --git a/make_post_sell/tests/test_functional.py b/make_post_sell/tests/test_functional.py index e9a7558..9081149 100644 --- a/make_post_sell/tests/test_functional.py +++ b/make_post_sell/tests/test_functional.py @@ -125,6 +125,7 @@ class AuthenticatedFunctionalTests(FunctionalTests): "description": "russell's product", "price": "3.50", "is_sellable": "on", + "submit": True, } self.coupon1_params = { diff --git a/make_post_sell/views/product.py b/make_post_sell/views/product.py index 0c383b8..5c38521 100644 --- a/make_post_sell/views/product.py +++ b/make_post_sell/views/product.py @@ -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,