Revision 3e87075a

b/snf-astakos-app/astakos/im/forms.py
45 45
from django.db import transaction
46 46
from django.core import validators
47 47

  
48
from synnefo.util import units
48 49
from synnefo_branding.utils import render_to_string
49 50
from synnefo.lib import join_urls
50 51
from astakos.im.models import AstakosUser, EmailChange, Invitation, Resource, \
......
883 884
                        raise forms.ValidationError("Invalid resource %s" %
884 885
                                                    resource.name)
885 886
                    d = model_to_dict(resource)
886
                    if uplimit:
887
                        d.update(dict(resource=prefix, uplimit=long(uplimit)))
888
                    else:
889
                        d.update(dict(resource=prefix, uplimit=None))
887
                    try:
888
                        uplimit = long(uplimit)
889
                    except ValueError:
890
                        m = "Limit should be an integer"
891
                        raise forms.ValidationError(m)
892
                    display = units.show(uplimit, resource.unit)
893
                    d.update(dict(resource=prefix, uplimit=uplimit,
894
                                  display_uplimit=display))
890 895
                    append(d)
891 896

  
892 897
        ordered_keys = presentation.RESOURCES['resources_order']
b/snf-astakos-app/astakos/im/models.py
69 69

  
70 70
from snf_django.lib.db.fields import intDecimalField
71 71
from synnefo.util.text import uenc, udec
72
from synnefo.util import units
72 73
from astakos.im import presentation
73 74

  
74 75
logger = logging.getLogger(__name__)
......
1484 1485
        unique_together = ("resource", "project_application")
1485 1486

  
1486 1487
    def display_member_capacity(self):
1487
        if self.member_capacity:
1488
            if self.resource.unit:
1489
                return ProjectResourceGrant.display_filesize(
1490
                    self.member_capacity)
1491
            else:
1492
                if math.isinf(self.member_capacity):
1493
                    return 'Unlimited'
1494
                else:
1495
                    return self.member_capacity
1496
        else:
1497
            return 'Unlimited'
1488
        return units.show(self.member_capacity, self.resource.unit)
1498 1489

  
1499 1490
    def __str__(self):
1500 1491
        return 'Max %s per user: %s' % (self.resource.pluralized_display_name,
1501 1492
                                        self.display_member_capacity())
1502 1493

  
1503
    @classmethod
1504
    def display_filesize(cls, value):
1505
        try:
1506
            value = float(value)
1507
        except:
1508
            return
1509
        else:
1510
            if math.isinf(value):
1511
                return 'Unlimited'
1512
            if value > 1:
1513
                unit_list = zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
1514
                                [0, 0, 0, 0, 0, 0])
1515
                exponent = min(int(math.log(value, 1024)), len(unit_list) - 1)
1516
                quotient = float(value) / 1024**exponent
1517
                unit, value_decimals = unit_list[exponent]
1518
                format_string = '{0:.%sf} {1}' % (value_decimals)
1519
                return format_string.format(quotient, unit)
1520
            if value == 0:
1521
                return '0 bytes'
1522
            if value == 1:
1523
                return '1 byte'
1524
            else:
1525
                return '0'
1526

  
1527 1494

  
1528 1495
def _distinct(f, l):
1529 1496
    d = {}
b/snf-astakos-app/astakos/im/templates/im/projects/projectapplication_form_summary.html
53 53
                        {{rp.pluralized_display_name}} per user
54 54
           			</dt>
55 55
			 		<dd>
56
			 			
57
           			{% if rp.uplimit %}
58
           				 {% if rp.unit %}
59
           				 	{{ rp.uplimit|sizeof_fmt }}
60
           				 {% else %}
61
           				 	{{ rp.uplimit }}
62
           				 {% endif %}
63
           			{% else %}
64
           				Unlimited
65
           			{% endif %}
56
                                          {{ rp.display_uplimit }}
66 57
           			</dd>
67 58
           		{% empty %}
68 59
           			No resources
b/snf-astakos-app/astakos/im/templatetags/filters.py
178 178
#     return value.replace('http://','')[:-1]
179 179

  
180 180

  
181
from math import log
182
unit_list = zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'], [0, 0, 0, 0, 0, 0])
183

  
184

  
185
@register.filter
186
def sizeof_fmt(num):
187
    """Human friendly file size"""
188
    return ProjectResourceGrant.display_filesize(num)
189

  
190

  
191 181
@register.filter
192 182
def truncatename(v, max=18, append="..."):
193 183
    length = len(v)

Also available in: Unified diff