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.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 astr
of 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
value
str
is 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.MultiValueField
Custom field to manage JSON objects related to plugins.
calingen.models.profile.Profile
is used to manage the user’s selection ofEventProvider
plugins.calingen.models.profile.Profile.event_provider()
handles this internally and maintains a specific JSON schema (without formally defining it), trackingactive
andunavailable
instances 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.MultiValueField
and provides theactive
plugins in adjango.forms.fields.MultipleChoiceField
and tracks theunavailable
plugins in acalingen.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 ofPluginWidget
.- Returns:
Converts the
data_list
back to a object, satisfyingProfile.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 fromProfile.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 theunavailable
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
andtime
, calingen will be happy with just a date and then assume that thetime
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 withtime
.- 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
, thevalue
will actually be a tuple, containing the values for thedate
and thetime
part. This method just provides a fallback for thetime
part and then usessuper().clean(value)
to chain further validation/cleaning steps.