fairdm.forms.fields#

Form fields and widgets for partial dates, quantities and fixed-precision decimals.

Module Contents#

Classes#

PartialDateWidget

Date select widget that orders year, month, day and accepts partial dates.

PartialDateFormField

Field with separate year, month and day selects for a PartialDateField.

PartialDateField

Text field for a partial date typed as yyyy-mm-dd, with a masked input.

QuantityFormFieldMixin

Hand the number input and unit select a stored quantity in the field’s base units.

QuantityFormField

django-pint’s float quantity form field, showing stored values in base units.

IntegerQuantityFormField

django-pint’s integer quantity form field, showing stored values in base units.

DecimalQuantityFormField

django-pint’s decimal quantity form field, showing stored values in base units.

DecimalField

Decimal field whose input is masked to the allowed integer and decimal places.

LatitudeField

Decimal field for a latitude, masked to seven digits with five decimal places.

LongitudeField

Decimal field for a longitude, masked to eight digits with five decimal places.

API#

class PartialDateWidget(attrs=None, years=None, months=None, empty_label=None)[source]#

Bases: django.forms.SelectDateWidget

Date select widget that orders year, month, day and accepts partial dates.

Args: attrs: HTML attributes for the rendered selects. years: Years offered in the year select. Defaults to 1900 through the current year, newest first. months: Months offered in the month select. empty_label: Label of the empty choice in each select.

Initialization

get_context(name, value, attrs)[source]#

Reorder the subwidgets to year, month, day.

format_value(value)[source]#

Split a PartialDate into the year, month and day the selects show.

value_from_datadict(data, files, name)[source]#

Join the year, month and day selects into a partial-date string.

class PartialDateFormField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)[source]#

Bases: django.forms.CharField

Field with separate year, month and day selects for a PartialDateField.

Initialization

widget#

None

class PartialDateField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)[source]#

Bases: django.forms.CharField

Text field for a partial date typed as yyyy-mm-dd, with a masked input.

Initialization

widget#

‘TextInput(…)’

clean(value)[source]#

Strip leading and trailing hyphens, returning None for an empty value.

class QuantityFormFieldMixin[source]#

Hand the number input and unit select a stored quantity in the field’s base units.

django-pint’s form field hands its widget the quantity itself, and the widget names the unit the way pint prints it (“meter”). That matches none of the unit choices when the field spells its units short (“m”), so the select shows no unit and an unchanged form fails to save.

prepare_value(value)[source]#

Split a quantity into its magnitude in base units and the base unit choice.

class QuantityFormField(*args, **kwargs)[source]#

Bases: fairdm.forms.fields.QuantityFormFieldMixin, quantityfield.fields.QuantityFormField

django-pint’s float quantity form field, showing stored values in base units.

Initialization

class IntegerQuantityFormField(*args, **kwargs)[source]#

Bases: fairdm.forms.fields.QuantityFormFieldMixin, quantityfield.fields.IntegerQuantityFormField

django-pint’s integer quantity form field, showing stored values in base units.

Initialization

class DecimalQuantityFormField(*args, **kwargs)[source]#

Bases: fairdm.forms.fields.QuantityFormFieldMixin, quantityfield.fields.DecimalQuantityFormField

django-pint’s decimal quantity form field, showing stored values in base units.

Initialization

class DecimalField(*, max_value=None, min_value=None, max_digits=None, decimal_places=None, **kwargs)[source]#

Bases: django.forms.DecimalField

Decimal field whose input is masked to the allowed integer and decimal places.

Args: max_value: Accepted for signature compatibility but not applied. min_value: Accepted for signature compatibility but not applied. max_digits: Total number of digits allowed. Required. decimal_places: Number of digits after the decimal point. Required. **kwargs: Passed to :class:django.forms.DecimalField.

Attributes: widget: The widget class, a plain text input carrying the mask.

Raises: ValueError: max_digits or decimal_places is missing.

Initialization

widget#

None

widget_attrs(widget)[source]#

Add the input mask, allowing a minus sign unless min_value is >= 0.

class LatitudeField(*args, max_digits=7, decimal_places=5, **kwargs)[source]#

Bases: fairdm.forms.fields.DecimalField

Decimal field for a latitude, masked to seven digits with five decimal places.

Args: *args: Passed to :class:DecimalField. max_digits: Total number of digits allowed. decimal_places: Number of digits after the decimal point. **kwargs: Passed to :class:DecimalField.

Initialization

class LongitudeField(*args, max_digits=8, decimal_places=5, **kwargs)[source]#

Bases: fairdm.forms.fields.DecimalField

Decimal field for a longitude, masked to eight digits with five decimal places.

Args: *args: Passed to :class:DecimalField. max_digits: Total number of digits allowed. decimal_places: Number of digits after the decimal point. **kwargs: Passed to :class:DecimalField.

Initialization