Allow ConditionField to reflect dynamically-added condition choices.

Review Request #14279 — Created Dec. 18, 2024 and updated — Latest diff uploaded

Information

Djblets
release-5.x

Reviewers

The Conditions system was designed with the intent that conditions could
be dynamically registered and unregistered as needed, offering
extensibility in the types of condition choices presented and processed.

This was never tested until now, and we've found a few flaws. One of
those is that ConditionField was calculating the valid choices at
field definition time, and held on to that when copying for a new form
instance. The choices present initially would remain present, and we'd
never recalculate.

To work around this, we now defer any processing of choices until we
need them, and we always refer back to the ConditionSet provided at
the latest initialization time (such as when initializing fields for a
form instance).

We remember the initial choices provided to the field and to the widget,
and refer back to that whenever we need to start fresh. If this is a
class, it's instantiated as late as possible. Same if it's a callback
providing an instance. This state is never deep-copied over to a new
field, allowing new instances to reflect the available choices at that
moment in time.

ConditionsWidget is now the source of truth for the condition choices.
ConditionsField has a wrapper that forwards on to that one. This
simplifies lifecycles considerably.

To take full advantage of this, callers should pass in an instance and
not a class when caling the field.

There's also one fix in here for managing widgets. We were accessing
the value field's widget attribute, which didn't exist. Initially,
this code was built to work with form fields, but we moved to a wrapper
class. We now define a forwarding attribute to get the inner field's
widget.

Unit tests pass.

Tested this with other in-progress work for more dynamic condition
choices. Verified that I could enable an extension and see all the
choices.

Changes between revision 2 and 3

orig
1
2
3
4

Commits

Summary ID Author
Allow ConditionField to reflect dynamically-added condition choices.
The Conditions system was designed with the intent that conditions could be dynamically registered and unregistered as needed, offering extensibility in the types of condition choices presented and processed. This was never tested until now, and we've found a few flaws. One of those is that `ConditionField` was calculating the valid choices at field definition time, and held on to that when copying for a new form instance. The choices present initially would remain present, and we'd never recalculate. To work around this, we now defer any processing of choices until we need them, and we always refer back to the `ConditionSet` provided at the latest initialization time (such as when initializing fields for a form instance). We remember the initial choices provided to the field and to the widget, and refer back to that whenever we need to start fresh. If this is a class, it's instantiated as late as possible. Same if it's a callback providing an instance. This state is never deep-copied over to a new field, allowing new instances to reflect the available choices at that moment in time. `ConditionsWidget` is now the source of truth for the condition choices. `ConditionsField` has a wrapper that forwards on to that one. This simplifies lifecycles considerably. To take full advantage of this, callers should pass in an instance and not a class when caling the field.
6544d8f2cf61d4c35514d56c5c541fc337aa5fe9 Christian Hammond
Allow ConditionField to reflect dynamically-added condition choices.
The Conditions system was designed with the intent that conditions could be dynamically registered and unregistered as needed, offering extensibility in the types of condition choices presented and processed. This was never tested until now, and we've found a few flaws. One of those is that `ConditionField` was calculating the valid choices at field definition time, and held on to that when copying for a new form instance. The choices present initially would remain present, and we'd never recalculate. To work around this, we now defer any processing of choices until we need them, and we always refer back to the `ConditionSet` provided at the latest initialization time (such as when initializing fields for a form instance). We remember the initial choices provided to the field and to the widget, and refer back to that whenever we need to start fresh. If this is a class, it's instantiated as late as possible. Same if it's a callback providing an instance. This state is never deep-copied over to a new field, allowing new instances to reflect the available choices at that moment in time. `ConditionsWidget` is now the source of truth for the condition choices. `ConditionsField` has a wrapper that forwards on to that one. This simplifies lifecycles considerably. To take full advantage of this, callers should pass in an instance and not a class when caling the field.
8c532363c6245a38c67ca82321287f03ca5a960d Christian Hammond
djblets/forms/fields.py
djblets/forms/widgets.py
djblets/forms/tests/test_conditions_widget.py
Loading...