Replaced hard coded values with oslo_utils.units constants

Replaced the hard coded values to oslo constants for clarity reasons.

Change-Id: I2bc3240372c518e210ec7d5e7c8b78cd21b94573
This commit is contained in:
George Peristerakis 2015-02-26 14:35:15 -05:00
parent a3dc4735e2
commit b146406d9a
5 changed files with 27 additions and 22 deletions

View File

@ -20,6 +20,8 @@
Template tags for displaying sizes
"""
from oslo_utils import units
from django import template
from django.utils import formats
from django.utils.translation import ugettext_lazy as _
@ -49,24 +51,19 @@ def filesizeformat(bytes, filesize_number_format):
return ungettext_lazy("%(size)d Byte",
"%(size)d Bytes", 0) % {'size': 0}
if bytes < 1024:
if bytes < units.Ki:
bytes = int(bytes)
return ungettext_lazy("%(size)d Byte",
"%(size)d Bytes", bytes) % {'size': bytes}
if bytes < 1024 * 1024:
return _("%s KB") % \
filesize_number_format(bytes / 1024)
if bytes < 1024 * 1024 * 1024:
return _("%s MB") % \
filesize_number_format(bytes / (1024 * 1024))
if bytes < 1024 * 1024 * 1024 * 1024:
return _("%s GB") % \
filesize_number_format(bytes / (1024 * 1024 * 1024))
if bytes < 1024 * 1024 * 1024 * 1024 * 1024:
return _("%s TB") % \
filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024))
return _("%s PB") % \
filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024))
if bytes < units.Mi:
return _("%s KB") % filesize_number_format(bytes / units.Ki)
if bytes < units.Gi:
return _("%s MB") % filesize_number_format(bytes / units.Mi)
if bytes < units.Ti:
return _("%s GB") % filesize_number_format(bytes / units.Gi)
if bytes < units.Pi:
return _("%s TB") % filesize_number_format(bytes / units.Ti)
return _("%s PB") % filesize_number_format(bytes / units.Pi)
def float_cast_filesizeformat(value, multiplier=1, format=int_format):
@ -80,14 +77,14 @@ def float_cast_filesizeformat(value, multiplier=1, format=int_format):
@register.filter(name='mbformat')
def mbformat(mb):
return float_cast_filesizeformat(mb, 1024 * 1024, int_format)
return float_cast_filesizeformat(mb, units.Mi, int_format)
@register.filter(name='mb_float_format')
def mb_float_format(mb):
return float_cast_filesizeformat(mb, 1024 * 1024, float_format)
return float_cast_filesizeformat(mb, units.Mi, float_format)
@register.filter(name='diskgbformat')
def diskgbformat(gb):
return float_cast_filesizeformat(gb, 1024 * 1024 * 1024, float_format)
return float_cast_filesizeformat(gb, units.Gi, float_format)

View File

@ -14,6 +14,8 @@ import decimal
import math
import re
from oslo_utils import units
from django.conf import settings
from django.contrib.auth import logout # noqa
from django import http
@ -32,7 +34,7 @@ lazy_join = lazy(_lazy_join, unicode)
def bytes_to_gigabytes(bytes):
# Converts the number of bytes to the next highest number of Gigabytes
# For example 5000000 (5 Meg) would return '1'
return int(math.ceil(float(bytes) / 1024 ** 3))
return int(math.ceil(float(bytes) / units.Gi))
def add_logout_reason(request, response, reason):

View File

@ -19,6 +19,8 @@
import json
import logging
from oslo_utils import units
from django import conf
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
@ -93,7 +95,7 @@ class IndexView(tables.DataTableView):
invalid_msg = ('API query is not valid and is ignored: %s=%s'
% (filter_field, filter_string))
try:
filter_string = long(float(filter_string) * (1024 ** 2))
filter_string = long(float(filter_string) * (units.Mi))
if filter_string >= 0:
filters[filter_field] = filter_string
else:

View File

@ -20,6 +20,8 @@ import json
import logging
import operator
from oslo_utils import units
from django.template.defaultfilters import filesizeformat # noqa
from django.utils.text import normalize_newlines # noqa
from django.utils.translation import ugettext_lazy as _
@ -672,7 +674,7 @@ class CustomizeAction(workflows.Action):
log_script_name = upload_file.name
LOG.info('got upload %s' % log_script_name)
if upload_file._size > 16 * 1024: # 16kb
if upload_file._size > 16 * units.Ki: # 16kb
msg = _('File exceeds maximum size (16kb)')
raise forms.ValidationError(msg)
else:

View File

@ -17,6 +17,8 @@
Views for managing volumes.
"""
from oslo_utils import units
from django.conf import settings
from django.core.urlresolvers import reverse
from django.forms import ValidationError # noqa
@ -105,7 +107,7 @@ class CreateForm(forms.SelfHandlingForm):
attrs={'class': 'image-selector'},
data_attrs=('size', 'name'),
transform=lambda x: "%s (%s)" % (
x.name, filesizeformat(x.size * 1024 * 1024 * 1024))),
x.name, filesizeformat(x.size * units.Gi))),
required=False)
type = forms.ChoiceField(
label=_("Type"),