Commit 65a183c1 authored by Marko Kuder's avatar Marko Kuder
Browse files

improvements and fixes for resource size output

- 0 bytes would previously output for zero size (rather avoid this)
- "bytes" was not translated, rather output "{num} B"
- downloadall button could cause error when zip_res would not exist
parent 1e86a1c4
......@@ -16,6 +16,7 @@ from itertools import dropwhile
import itertools
import datetime
import random
import numbers
import types
import urllib2
from urllib2 import HTTPError, URLError
......@@ -2760,6 +2761,24 @@ def as_dict(d):
def extract_year(resource_dict):
return parse_date(resource_dict.get('date'))['year']
def human_size(num):
if num < 1024.0:
return '{} B'.format(num)
for unit in ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB"]:
if abs(num) < 1024.0:
return '{:.1f} {}'.format(num, unit)
num /= 1024.0
return '{:.1f} YB'
def get_resource_size_formatted(resource_dict):
if resource_dict and resource_dict.get('archiver') and resource_dict['archiver'].get('size'):
size = resource_dict['archiver'].get('size')
if not isinstance(size, numbers.Number) or size <= 0:
return None
return human_size(size)
else:
return None
def report_match_organization_name(name, dct):
return filter(lambda d: d['organization_name'] == name, dct)
......
......@@ -261,13 +261,13 @@
<div class="mr-15">
{% if h.is_plugin_enabled('downloadall') %}
{% set zip_res = h.downloadall__pop_zip_resource(c.pkg_dict) %}
{% set resource_size = h.get_resource_size_formatted(zip_res) %}
{% if zip_res %}
<a class="button w-230 resource-url-analytics resource-type-{{ zip_res.resource_type }}" href="{{ zip_res.url }}">
{% else %}
<a class="button w-230" disabled="disabled" title="{{ _('This download is not currently available') }}" href="#">
{% endif %}
<span class="icon-download"></span><strong>{{ _('Download all') }}{% if zip_res.get('archiver') and not h.is_string(zip_res['archiver'])
and zip_res['archiver'].get('size') %} ({{ zip_res['archiver']['size']|filesizeformat }}){% endif %}</strong>
<span class="icon-download"></span><strong>{{ _('Download all') }}{% if resource_size %} ({{ resource_size }}){% endif %}</strong>
</a>
{% endif %}
</div>
......@@ -315,6 +315,7 @@
{% set is_location_data = h.is_location_data(c.pkg_dict) %}
{% set is_harvest_metadata = res.get('gemini',False) or res.get('is_harvest_metadata',False) %} {# i.e. the gemini metadata links, shown as resources #}
{% set archiver_format = res.get('archiver', {})['format'] or '' %}
{% set resource_size = h.get_resource_size_formatted(res) %}
{% set is_download = not is_location_data and res.get('resource_type') != 'api' and (res.format or '').upper() not in non_download_formats and archiver_format not in non_download_formats %}
{% set is_html = not is_download and (res.format and res.format.upper() == 'HTML' or archiver_format.upper() == 'HTML') %}
<span>
......@@ -323,8 +324,8 @@
<strong>
{% if is_download and not is_harvest_metadata %}
Prenos
{% if res.get('archiver') and res['archiver'].get('size') %}
({{ res['archiver']['size']|filesizeformat }})
{% if resource_size %}
({{ resource_size }})
{% endif %}
{% else %}
Povezava
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment