calingen.forms.fields

App-specific implementations of django.forms.fields.Field.

Module Contents

Classes

CalingenListField

Misuse a CharField to handle a list of strings.

PluginField

Custom field to manage JSON objects related to plugins.

SplitDateTimeOptionalField

App-specific implementation of Django's SplitDateTimeField.

class calingen.forms.fields.CalingenListField(*, max_length=None, min_length=None, strip=True, empty_value='', **kwargs)

Bases: django.forms.fields.CharField

Misuse a CharField to handle a list of strings.

prepare_value(value)

Create a comma-seperated list of strings from a Python list.

Parameters:

value (list) –

Returns:

The value list is split and provided as a str of comma-seperated values.

Return type:

str

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 value str is split and converted in an actual Python list.

Return type:

list

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.MultiValueField

Custom field to manage JSON objects related to plugins.

calingen.models.profile.Profile is used to manage the user’s selection of EventProvider plugins.

calingen.models.profile.Profile.event_provider() handles this internally and maintains a specific JSON schema (without formally defining it), tracking active and unavailable instances of EventProvider.

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’s compress() method needs a counterpart in the widget’s decompress() method.

Notes

The implementation extends django.forms.fields.MultiValueField and provides the active plugins in a django.forms.fields.MultipleChoiceField and tracks the unavailable plugins in a calingen.forms.fields.CalingenListField.

widget

Use PluginWidget as the fields widget.

compress(data_list)

Compress the values of multiple fields into one object for the ORM layer.

Parameters:

data_list (list) – A list of values as provided by the multiple widgets of PluginWidget.

Returns:

Converts the data_list back to a object, satisfying Profile.event_provider's JSON 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) – value is actually a JSON object as fetched from Profile.event_provider.

Notes

This method splits the value to be used by the widgets.

Additionally it ensures, that CalingenListField.prepare_value is called to handle the unavailable plugins.

class calingen.forms.fields.SplitDateTimeOptionalField(*, input_date_formats=None, input_time_formats=None, **kwargs)

Bases: django.forms.fields.SplitDateTimeField

App-specific implementation of Django’s SplitDateTimeField.

While Django’s default implementation of django.forms.fields.SplitDateTimeField requires a valid value for both, date and time, calingen will be happy with just a date and then assume that the time is "00:00".

Warning

The field will accept an empty value for the time part, but will actually provide a default value of "00:00" on processing the form.

This means, that the database will contain a datetime.datetime object with time.

clean(value)

Catch validation errors of the time part and provide a fallback.

Parameters:

value (any) – The value to be cleaned. As this is derived from a django.forms.fields.MultiValueField, the value will actually be a tuple, containing the values for the date and the time part. This method just provides a fallback for the time part and then uses super().clean(value) to chain further validation/cleaning steps.