Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion opentreemap/treemap/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
to_object_name, safe_get_model_class,
get_pk_from_collection_audit_name,
get_name_from_canonical_name,
make_udf_name_from_key)
make_udf_name_from_key, num_format)
from treemap.decorators import classproperty

from treemap.lib.object_caches import (field_permissions,
Expand Down Expand Up @@ -1480,6 +1480,8 @@ def _unit_format(self, value):
units = get_unit_name(get_units(self.instance,
model_name, self.field))
value += (' %s' % units)
elif isinstance(value, float):
return num_format(value)

return value

Expand Down
5 changes: 5 additions & 0 deletions opentreemap/treemap/templates/treemap/field/attrs.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{% load l10n %}
{% load util %}
data-field="{{ field.identifier }}"
data-class="{{ class }}"
{% if field.data_type == 'float' %}
data-value="{{ field.value|num_format|default_if_none:""|unlocalize|force_escape }}"
{% else %}
data-value="{{ field.value|default_if_none:""|unlocalize|force_escape }}"
{% endif %}
data-type="{{ field.data_type }}"
data-units="{{ field.units }}"
data-digits="{{ field.digits }}"
6 changes: 5 additions & 1 deletion opentreemap/treemap/templates/treemap/field/inputs.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
<input name="{{ field.identifier }}"
type="text"
class="{{ class|default_if_none:"" }} form-control"
value="{{ field.value|default_if_none:""|unlocalize }}"
{% if field.data_type == 'float' %}
value="{{ field.value|num_format|default_if_none:""|unlocalize }}"
{% else %}
value="{{ field.value|default_if_none:""|unlocalize }}"
{% endif %}
{{ extra|default:"" }} />
<span class="input-group-addon" {{ unit_extra|default:"" }}>{{ field.units }}</span>
</div>
Expand Down
4 changes: 3 additions & 1 deletion opentreemap/treemap/templatetags/form_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from opentreemap.util import dotted_split

from treemap.util import get_model_for_instance, to_object_name
from treemap.util import get_model_for_instance, to_object_name, num_format
from treemap.json_field import (is_json_field_reference,
get_attr_from_json_field)
from treemap.units import (get_digits_if_formattable, get_units_if_convertible,
Expand Down Expand Up @@ -430,6 +430,8 @@ def _field_value(model, field_name, data_type):
display_vals = [choice['display_value'] for choice in choices
if choice['value'] == field_value]
display_val = display_vals[0] if display_vals else field_value
elif data_type == 'float':
display_val = num_format(field_value)
else:
display_val = unicode(field_value)

Expand Down
7 changes: 6 additions & 1 deletion opentreemap/treemap/templatetags/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from treemap.models import MapFeature, Tree, TreePhoto, MapFeaturePhoto, Audit
from treemap.udf import UserDefinedCollectionValue
from treemap.util import (get_filterable_audit_models, to_model_name,
safe_get_model_class)
safe_get_model_class, num_format as util_num_format)
from treemap.units import Convertible


Expand Down Expand Up @@ -199,3 +199,8 @@ def udf_name(udf_identifier):
raise ValueError('Unrecognized identifier %(id)s' % udf_identifier)

return udf_identifier.split("udf:", 1)[1]


@register.filter
def num_format(num):
return util_num_format(num)
7 changes: 7 additions & 0 deletions opentreemap/treemap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,10 @@ def make_udf_name_from_key(key):

def make_udf_lookup_from_key(key):
return 'udfs__{}'.format(key)


def num_format(num):
if isinstance(num, float):
# Allow for up to 10 digits of precision, but strip trailing '0' or '.'
return '{0:.10f}'.format(num).rstrip('0').rstrip('.')
return num