Skip to content

Commit 863708b

Browse files
ianspricecodingjoe
authored andcommitted
Added bidimensional_separator setting/kwarg (#29)
* Added bidimensional_seperator kwarg Added bidimensional_seperator kwarg to MeasurementField. This changes the option label upon rendering. This can be defined two ways- 1. Add a setting "BIDIMENSIONAL_SEPERATOR" to settings.py, which will be default unless kwarg is supplied. 2. Passing kwarg bidimensional_seperator to instantiation of a particular MeasurementField. Example- if kwarg bidimensional_seperator=' per ' you would get a label of 'kg per s'. Otherwise, a default of "/" will be used. Using this project for large-scale engineering applications, I had a need for each level of complexity provided, so hopefully it's useful to others. * Added BIDIMENSIONAL_SEPERATOR setting Added BIDIMENSIONAL_SEPERATOR setting * Corrected spelling of BIDIMENSIONAL_SEPARATOR * Updated test_unicode_labels Updated test_unicode_labels to reflect new bidimensional_separator functionality * Corrected spelling of BIDIMENSIONAL_SEPARATOR * Updated bidimensional_separator assertion * Update forms.py * Update forms.py * Update forms.py * Added bidimensional_seperator kwarg explanation * Update forms.rst * Update forms.rst * Update forms.rst * Added BIDIMENSIONAL_SEPERATOR setting to appconf New conf.py file for Measurement * Updated to use conf.py Now uses conf.py to fetch BIDIMENSIONAL_SEPARATOR * Added django-appconf Added django-appconf>=1.0.2 for handling of app settings * Added django-appconf Added django-appconf>=1.0.2 * Flake tests and import updates * whitespace cleanup for pep257 * whitespace cleanup for pep257
1 parent 96ea1c1 commit 863708b

File tree

8 files changed

+51
-15
lines changed

8 files changed

+51
-15
lines changed

django_measurement/conf.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
"""Settings for django-measurement."""
3+
from __future__ import absolute_import, unicode_literals
4+
5+
from appconf import AppConf
6+
from django.conf import settings
7+
8+
__all__ = ('settings', 'DjangoMeasurementConf')
9+
10+
11+
class DjangoMeasurementConf(AppConf):
12+
13+
"""Settings for django-measurement."""
14+
15+
BIDIMENSIONAL_SEPARATOR = '/'
16+
"""
17+
For measurement classes subclassing a BidimensionalMeasure, this .
18+
"""
19+
20+
class Meta:
21+
prefix = 'measurement'

django_measurement/forms.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from django import forms
77
from django.core.validators import MaxValueValidator, MinValueValidator
88
from measurement.base import BidimensionalMeasure, MeasureBase
9+
from six import string_types
910

10-
from . import utils
11+
from django_measurement import utils
12+
from django_measurement.conf import settings
1113

1214

1315
class MeasurementWidget(forms.MultiWidget):
@@ -47,7 +49,9 @@ def decompress(self, value):
4749

4850
class MeasurementField(forms.MultiValueField):
4951
def __init__(self, measurement, max_value=None, min_value=None,
50-
unit_choices=None, validators=None, *args, **kwargs):
52+
unit_choices=None, validators=None,
53+
bidimensional_separator=settings.MEASUREMENT_BIDIMENSIONAL_SEPARATOR,
54+
*args, **kwargs):
5155

5256
if not issubclass(measurement, (MeasureBase, BidimensionalMeasure)):
5357
raise ValueError(
@@ -57,12 +61,16 @@ def __init__(self, measurement, max_value=None, min_value=None,
5761
self.measurement_class = measurement
5862
if not unit_choices:
5963
if issubclass(measurement, BidimensionalMeasure):
64+
assert isinstance(bidimensional_separator, string_types), \
65+
"Supplied bidimensional_separator for %s must be of string/unicode type;" \
66+
" Instead got type %s" % (measurement, str(type(bidimensional_separator)),)
6067
unit_choices = tuple((
6168
(
6269
'{0}__{1}'.format(primary, reference),
63-
'{0}__{1}'.format(
70+
'{0}{1}{2}'.format(
6471
getattr(measurement.PRIMARY_DIMENSION, 'LABELS', {}).get(
6572
primary, primary),
73+
bidimensional_separator,
6674
getattr(measurement.REFERENCE_DIMENSION, 'LABELS', {}).get(
6775
reference, reference)),
6876
)

docs/topics/forms.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ If unicode symbols are needed in the labels for a MeasurementField, define a LAB
4040
'f':u'°F',
4141
'k':u'°K',
4242
}
43+
44+
For a `MeasurementField` that represents a `BidimensionalMeasure`, you can set the separator either in settings.py (`MEASUREMENT_BIDIMENSIONAL_SEPARATOR is '/'` by default, add setting to override for all BiDimensionalMeasure subclasses) or override for an individual field with the kwarg bidimensional_separator::
45+
46+
speed = MeasurementField(
47+
measurement=Speed,
48+
bidimensional_separator=' per '
49+
)
50+
51+
# Rendered option labels will now be in the format "ft per s", "m per hr", etc

docs/topics/settings.rst

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
Settings
33
========
44

5-
``MEASURE_OVERRIDES``
6-
---------------------
75

8-
If you have your own measures to add,
9-
you can specify a name for your measure and a string class path to allow
10-
for your custom measure to be properly stored and resurrected.::
6+
``MEASUREMENT_BIDIMENSIONAL_SEPARATOR``
7+
---------------------
8+
For any BidimensionalMeasure, what is placed between the primary and reference dimensions on rendered label
119

12-
MEASURE_OVERRIDES = {
13-
'IU': 'path.to.my.class.IUMeasure'
14-
}
10+
MEASUREMENT_BIDIMENSIONAL_SEPARATOR = " per "
1511

16-
You can also override existing measure classes this way.
12+
Defaults to "/". Can be overriden as kwarg `bidimensional_separator` for a given MeasurementField.

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# pip-compile requirements-dev.in
66
#
77
-e .
8+
django-appconf>=1.0.2
89
flake8==2.5.0
910
isort==4.2.2
1011
mccabe==0.3.1 # via flake8

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
django-appconf>=1.0.2
12
measurement>=1.6

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='django-measurement',
12-
version='2.3.0',
12+
version='2.4.0',
1313
url='http://github.com/coddingtonbear/django-measurement/',
1414
description='Convenient fields and classes for handling measurements',
1515
author='Adam Coddington',

tests/test_fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,5 +273,5 @@ def test_unicode_labels(self):
273273
assert ('Ps', 'Ps') in si_form.fields['simple'].fields[1].choices
274274

275275
bi_dim_form = BiDimensionalLabelTestForm()
276-
assert ('c__ms', u'°C__ms') in bi_dim_form.fields['simple'].fields[1].choices
277-
assert ('c__Ps', u'°C__Ps') in bi_dim_form.fields['simple'].fields[1].choices
276+
assert ('c__ms', u'°C/ms') in bi_dim_form.fields['simple'].fields[1].choices
277+
assert ('c__Ps', u'°C/Ps') in bi_dim_form.fields['simple'].fields[1].choices

0 commit comments

Comments
 (0)