Merge branch 'develop' into 'master'

Display error message when uploading no files picked.

Closes #15

See merge request engineering/make-post-sell/make_post_sell!17
This commit is contained in:
Russell Ballestrini 2024-06-23 14:50:01 +00:00
commit 98895188cc
2 changed files with 38 additions and 13 deletions

View file

@ -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(
(
'<span class="avoid-wrap">You successfully uploaded</span> <span class="avoid-wrap">a new {} file!</span>'.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"))

View file

@ -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]