From 477d6c2dd015a45f73cda26ad48008ae875f4ca6 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Tue, 25 Jun 2024 22:12:27 +0000 Subject: [PATCH] fixes ticket #25 bundle products are working and safer now! --- make_post_sell/models/product.py | 11 ++++++++++- make_post_sell/views/product.py | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/make_post_sell/models/product.py b/make_post_sell/models/product.py index 07fc2ec..72830ef 100644 --- a/make_post_sell/models/product.py +++ b/make_post_sell/models/product.py @@ -403,8 +403,17 @@ class Product(RBase, Base): def add_product_to_bundle(self, product): if self.is_bundle == False: self.error_message = "This product is not a bundle." - if self.shop_id != product.shop_id: + elif self.shop_id != product.shop_id: self.error_message = "The product and bundle must share the same shop." + elif product.is_physical or self.is_physical: + self.error_message = ( + f"Refusing to bundle {product.title}, it is a physical item." + ) + elif product in self.products_in_this_bundle: + self.error_message = ( + f"Refusing to bundle {product.title}, it is already in the bundle." + ) + if self.error_message: return False diff --git a/make_post_sell/views/product.py b/make_post_sell/views/product.py index 819e804..23d6cb1 100644 --- a/make_post_sell/views/product.py +++ b/make_post_sell/views/product.py @@ -258,7 +258,9 @@ def product_edit(request): if file_size == 0: raise ValueError("File size is zero") except Exception: - request.session.flash(("File upload failed. Pick a file and try again.", "error")) + request.session.flash( + ("File upload failed. Pick a file and try again.", "error") + ) return HTTPFound(request.route_url("product_edit", product_id=product.id)) # Store the uploaded user's file_metadata in our database. @@ -445,11 +447,25 @@ def product_bundle_edit(request): products = get_products_by_ids( request.dbsession, UUID_ALL_PATTERN.findall(bundle_data) ) + + if products is None: + request.session.flash( + ("We didn't detect any product ids. Try again.", "error") + ) + return HTTPFound(get_referer_or_home(request)) + for product_to_bundle in products: if not product.add_product_to_bundle(product_to_bundle): request.session.flash((product.error_message, "error")) + product.error_message = None continue - request.session.flash(("You added a product to this bundle!", "success")) + + request.session.flash( + ( + f"You added product {product_to_bundle.title} to this bundle!", + "success", + ) + ) return HTTPFound(get_referer_or_home(request))