calingen.forms.fields¶
App-specific implementations of django.forms.fields.Field.
Module Contents¶
Classes¶
Misuse a |
|
Custom field to manage JSON objects related to plugins. |
|
App-specific implementation of Django's |
- class calingen.forms.fields.CalingenListField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)¶
Bases:
django.forms.fields.CharFieldMisuse a
CharFieldto handle a list of strings.- prepare_value(value)¶
Create a comma-seperated list of strings from a Python list.
- Parameters:
value (list) –
- Returns:
The
valuelistis split and provided as astrof comma-seperated values.- Return type:
Notes
In Django’s normal form rendering workflow, this method is called to process a value for display in a widget. However, for a Django
django.forms.fields.MultiValueField, the method is not called for sub fields.As this field is specifically intended to work with
PluginField,its prepare_value()method calls this method.
- to_python(value)¶
Convert a comma-seperated list of values to a Python list.
- Parameters:
value (str) –
- Returns:
The
valuestris split and converted in an actual Pythonlist.- Return type:
Notes
While
prepare_value()has to be called specifically, this very method is called automatically during form processing.
- class calingen.forms.fields.PluginField(*args, choices=(), **kwargs)¶
Bases:
django.forms.fields.MultiValueFieldCustom field to manage JSON objects related to plugins.
calingen.models.profile.Profileis used to manage the user’s selection ofEventProviderplugins.calingen.models.profile.Profile.event_provider()handles this internally and maintains a specific JSON schema (without formally defining it), trackingactiveandunavailableinstances ofEventProvider.While the model stores this as a
JSONField, this custom field is used to present it in a form context.Warning
Currently this is very tightly connected to the actual implementation in
calingen.models.profile.Profile.event_provider()and will work only with that model / field out of the box.Additionally, it will only work with
calingen.forms.widgets.PluginWidget, because this class’scompress()method needs a counterpart in the widget’sdecompress()method.Notes
The implementation extends
django.forms.fields.MultiValueFieldand provides theactiveplugins in adjango.forms.fields.MultipleChoiceFieldand tracks theunavailableplugins in acalingen.forms.fields.CalingenListField.- widget¶
Use
PluginWidgetas the fields widget.
- compress(data_list)¶
Compress the values of multiple fields into one object for the ORM layer.
- Parameters:
data_list (list) – A
listof values as provided by the multiple widgets ofPluginWidget.- Returns:
Converts the
data_listback to a object, satisfyingProfile.event_provider'sJSON scheme.- Return type:
obj
Notes
See the corresponding
calingen.forms.widgets.PluginWidget.decompress()method.
- prepare_value(value)¶
Prepare the value / values for display.
- Parameters:
value (dict) –
valueis actually a JSON object as fetched fromProfile.event_provider.
Notes
This method splits the
valueto be used by the widgets.Additionally it ensures, that
CalingenListField.prepare_valueis called to handle theunavailableplugins.
- class calingen.forms.fields.SplitDateTimeOptionalField(*, input_date_formats=None, input_time_formats=None, **kwargs)¶
Bases:
django.forms.fields.SplitDateTimeFieldApp-specific implementation of Django’s
SplitDateTimeField.While Django’s default implementation of
django.forms.fields.SplitDateTimeFieldrequires a valid value for both,dateandtime, calingen will be happy with just a date and then assume that thetimeis"00:00".Warning
The field will accept an empty value for the
timepart, but will actually provide a default value of"00:00"on processing the form.This means, that the database will contain a
datetime.datetimeobject withtime.- clean(value)¶
Catch validation errors of the
timepart and provide a fallback.- Parameters:
value (any) – The value to be cleaned. As this is derived from a
django.forms.fields.MultiValueField, thevaluewill actually be a tuple, containing the values for thedateand thetimepart. This method just provides a fallback for thetimepart and then usessuper().clean(value)to chain further validation/cleaning steps.