From eebb5a9f153afb11b232790a51fb7726edbd8092 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 23 Jun 2024 14:50:01 +0000 Subject: [PATCH] Display error message when uploading no files picked. --- make_post_sell/views/product.py | 27 +++++++++++++++++++-------- make_post_sell/views/shop.py | 24 +++++++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/make_post_sell/views/product.py b/make_post_sell/views/product.py index 33e2758..9c2856b 100644 --- a/make_post_sell/views/product.py +++ b/make_post_sell/views/product.py @@ -16,7 +16,7 @@ from ..lib.currency import dollars_to_cents def checkbox_to_bool(checkbox): - return True if checkbox == "on" else False + return checkbox == "on" @view_config(route_name="product", renderer="product.j2") @@ -207,9 +207,7 @@ def product_edit(request): if uploaded_file_key: request.session.flash( ( - 'You successfully uploaded a new {} file!'.format( - uploaded_file_key - ), + f"You successfully uploaded a new {uploaded_file_key} file!", "success", ) ) @@ -250,6 +248,19 @@ def product_edit(request): request.session.flash(("You updated the product's price.", "success")) if s3_webhook_key and s3_webhook_bucket and s3_webhook_etag: + # Check if the file exists and has a non-zero size + try: + response = request.secure_uploads_client.head_object( + Bucket=s3_webhook_bucket, + Key=s3_webhook_key, + ) + file_size = response.get("ContentLength", 0) + 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")) + return HTTPFound(request.route_url("product_edit", product_id=product.id)) + # Store the uploaded user's file_metadata in our database. file_key = product.store_file_metadata(s3_webhook_key) @@ -423,7 +434,7 @@ def product_bundle_edit(request): product = request.product - if product.is_bundle == False: + if not product.is_bundle: request.session.flash( ("You may only add products to a Product bundle.", "error") ) @@ -435,7 +446,7 @@ def product_bundle_edit(request): request.dbsession, UUID_ALL_PATTERN.findall(bundle_data) ) for product_to_bundle in products: - if product.add_product_to_bundle(product_to_bundle) == False: + if not product.add_product_to_bundle(product_to_bundle): request.session.flash((product.error_message, "error")) continue request.session.flash(("You added a product to this bundle!", "success")) @@ -453,12 +464,12 @@ def product_bundle_remove_product(request): bundled_product_id = request.matchdict.get("bundled_product_id") bundled_product = get_product_by_id(request.dbsession, bundled_product_id) - if product.is_bundle == False: + if not product.is_bundle: request.session.flash( ("You may only remove products from a Product bundle.", "error") ) - if product.remove_product_from_bundle(bundled_product) == False: + if not product.remove_product_from_bundle(bundled_product): request.session.flash((product.error_message, "error")) else: request.session.flash(("You removed a product from this bundle!", "success")) diff --git a/make_post_sell/views/shop.py b/make_post_sell/views/shop.py index 6c90f38..56487d3 100644 --- a/make_post_sell/views/shop.py +++ b/make_post_sell/views/shop.py @@ -3,6 +3,7 @@ import mimetypes from pyramid.view import view_config from . import ( + get_referer_or_home, user_required, shop_owner_required, shop_editor_required, @@ -38,11 +39,11 @@ from pyramid.httpexceptions import HTTPFound def checkbox_to_bool(checkbox): - return True if checkbox == "on" else False + return checkbox == "on" def bool_to_checkbox(boolean): - return "on" if boolean == True else "off" + return "on" if boolean else "off" def get_shop_from_matchdict(request, prefetched_shop=None): @@ -156,16 +157,16 @@ def shop_new(request): description = request.params.get("description", "") if name and description and billing_address and phone_number: - if is_shop_name_valid(name) == False: + if not is_shop_name_valid(name): msg = ( "Invalid shop name, only use alpha numeric, spaces, dashes, or periods.", "error", ) request.session.flash(msg) - elif is_shop_name_available(request.dbsession, name) == False: + elif not is_shop_name_available(request.dbsession, name): msg = ("That shop name is already in use. Please pick another.", "error") request.session.flash(msg) - elif is_phone_number_valid(phone_number) == False: + elif not is_phone_number_valid(phone_number): msg = ("Invalid phone number. We accept: -.() and numbers.", "error") request.session.flash(msg) @@ -489,6 +490,19 @@ def shop_settings(request): # TODO: Dry out this block, it's a copy pasta from views/product.py if s3_webhook_key and s3_webhook_bucket and s3_webhook_etag: + # Check if the file exists and has a non-zero size + try: + response = request.secure_uploads_client.head_object( + Bucket=s3_webhook_bucket, + Key=s3_webhook_key, + ) + file_size = response.get('ContentLength', 0) + 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")) + return HTTPFound(request.route_url('shop_settings', shop_id=shop.id)) + # get file_from S3 service posted params. file_key = s3_webhook_key.split("/")[-1].split(".")[0]