output human readable size of project on product.

modified:   make_post_sell/templates/product.j2
	modified:   make_post_sell/views/product.py
This commit is contained in:
Russell Ballestrini 2022-07-28 11:38:36 -04:00
parent 8bba55f5d5
commit b00897acf4
2 changed files with 20 additions and 5 deletions

View file

@ -16,7 +16,6 @@
-->
{%- endblock append_to_head_tag_section -%}
{%- block call_to_action -%}
{% if request.user.can_edit_shop(request.shop) %}
<a href="/p/{{ product.id }}/edit" class="product-edit-button mps-button mps-button-small">&#9998; Edit</a>
@ -35,7 +34,6 @@
<a href="{{ product.shop.absolute_about_url(request) }}" rel="nofollow">{{ product.shop.name }}</a>
</h3>
{% if "thumbnail1" in product.extensions %}
<img src="{{ request.app["bucket.secure_uploads.get_endpoint"] }}/{{ product.s3_path }}/thumbnail1?ts={{ product.updated_timestamp }}" class="product-main" />
<br/>
@ -47,8 +45,7 @@
<a href="{{ get_endpoint }}" target="_blank"><img src="{{ get_endpoint }}?ts={{ product.updated_timestamp }}" class="product-thumbnail" /></a>
{% endif %}
{% endfor %}
</section>
<section class="product-right">
@ -122,9 +119,10 @@
{% elif product.has_product_file %}
<b>File Type</b>
<br/>
{{ product.get_content_type("product") }}
{{ product.get_content_type("product") }} {{ product_size }}
<br/>
Please make sure you have an application to open this file type.
{% endif %}
</section>

View file

@ -74,8 +74,25 @@ def product(request):
ExpiresIn=900
)
response = request.secure_uploads_client.head_object(
Bucket=request.app["bucket.secure_uploads"],
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)
return {
"product": product,
"product_size": product_size,
"signed_get_object_url": signed_get_object_url,
}