diff --git a/make_post_sell/models/product.py b/make_post_sell/models/product.py index a895107..773de89 100644 --- a/make_post_sell/models/product.py +++ b/make_post_sell/models/product.py @@ -58,6 +58,14 @@ DEFAULT_BUNDLE_METADATA = unicode( ) +def sizeof_fmt(num, suffix="B"): + for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: + if abs(num) < 1024.0: + return f"{num:3.1f}{unit}{suffix}" + num /= 1024.0 + return f"{num:.1f}Yi{suffix}" + + class Product(RBase, Base): """This class represents a product.""" @@ -278,6 +286,14 @@ class Product(RBase, Base): self.json_file_bytes = json.dumps(new_dict) self.total_file_bytes = sum(self.file_bytes.values()) + @property + def human_total_file_bytes(self): + return sizeof_fmt(self.total_file_bytes) + + @property + def human_product_file_bytes(self): + return sizeof_fmt(self.file_bytes["product"]) + @property def extensions(self): return self.file_metadata.get("extensions", {}) diff --git a/make_post_sell/templates/shop_products.j2 b/make_post_sell/templates/shop_products.j2 index 47c804a..a7c6352 100644 --- a/make_post_sell/templates/shop_products.j2 +++ b/make_post_sell/templates/shop_products.j2 @@ -10,7 +10,7 @@ tr { text-align: left;}
| Sellable | Price | Visibility | Bundle | Ready | Title + | |
|---|---|---|---|---|---|---|
| Sellable | Price | Visibility | Bundle | Ready | Title | Size | {{ product.is_bundle }} | {{ product.is_ready }} | {{ product.title }} | +{{ product.human_total_file_bytes }} | {% endfor %} diff --git a/make_post_sell/views/content.py b/make_post_sell/views/content.py index 34a7091..2d16c2d 100644 --- a/make_post_sell/views/content.py +++ b/make_post_sell/views/content.py @@ -10,7 +10,6 @@ from pyramid.httpexceptions import HTTPFound def content(request): product = request.product - product_size = "" if not request.is_saas_domain and request.domain != request.shop.domain_name: # The content uuid in the URI matchdict is mismatched with request shop. @@ -65,29 +64,9 @@ def content(request): ExpiresIn=900 ) - #t = request.secure_uploads_client.get_object_torrent( - # Bucket=bucket_name, - # Key=product.s3_key, - #) - + product_size = "" if product.has_product_file: - # get the human readable size of s3 object for product. - - response = request.secure_uploads_client.head_object( - Bucket=bucket_name, - Key=product.s3_key, - ) - - product_content_length = response["ContentLength"] - - def sizeof_fmt(num, suffix="B"): - for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: - if abs(num) < 1024.0: - return f"{num:3.1f}{unit}{suffix}" - num /= 1024.0 - return f"{num:.1f}Yi{suffix}" - - product_size = sizeof_fmt(product_content_length) + product_size = product.human_product_file_bytes return { "product": product, diff --git a/make_post_sell/views/product.py b/make_post_sell/views/product.py index db12a62..1d50958 100644 --- a/make_post_sell/views/product.py +++ b/make_post_sell/views/product.py @@ -22,7 +22,6 @@ def checkbox_to_bool(checkbox): def product(request): product = request.product - product_size = "" if not request.is_saas_domain and request.domain != request.shop.domain_name: # The product uuid in the URI matchdict is mismatched with request shop. @@ -77,24 +76,9 @@ def product(request): ExpiresIn=900 ) + product_size = "" if product.has_product_file: - # get the human readable size of s3 object for product. - - response = request.secure_uploads_client.head_object( - Bucket=bucket_name, - Key=product.s3_key, - ) - - product_content_length = response["ContentLength"] - - def sizeof_fmt(num, suffix="B"): - for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: - if abs(num) < 1024.0: - return f"{num:3.1f}{unit}{suffix}" - num /= 1024.0 - return f"{num:.1f}Yi{suffix}" - - product_size = sizeof_fmt(product_content_length) + product_size = product.human_product_file_bytes return { "product": product,