Add the foundations for user-customizable condition rules.
Review Request #8268 — Created July 6, 2016 and submitted
This change introduces the core support for condition rules. These are a way to allow applications to give users a degree of flexibility for choosing when certain actions should take place. Users can define one or more "conditions," which consist of a "condition choice" (a property or an object, generally, that the condition is matched upon), an "operator" (which applies to a choice -- something like "is", "starts with", "greater than", etc.), and a value (depending on the type of choice and operator). Along with these, they can decide whether all conditions must match, or any of them. For example, if an application wants to post notifications to a chat service, it could allow the user to customize under what conditions a message would be sent, making it easy to have a range of per-channel configurations. Conditions can be serialized to a JSON-compatible form (useful for storage in JSONFields or elsewhere), and deserialized from them as well. This makes them usable just about anywhere, including in API requests. Consumers would create a set of condition choices they want to expose for some part of their application. Each choice has an ID (unique within that batch of choices), a human-readable name, a list of one or more operators, and an optional default value field (which handles representing input fields on a web UI and handling serialization/deserialization). Operators, similarly, have an ID and a name, and can optionally override the value field (allowing, for instance, some operators for a date-based choice to show calendar entries or numeric text fields, whichever is more appropriate). It also contains the logic for matching a stored condition value with the lookup value from the caller. Both the list of choices and list of operators are registries, which allows other code (such as extensions) to add additional choices/operators to any existing list. Through this, authors could add even more flexibility for conditions beyond what the application author intended, helping to match on specific types of metadata or state in the database or anything else the author wants. This is just the beginning of this support. Upcoming changes will add standard sets of choices, operators, and values, and will introduce standard UI for configuration conditions.
Unit tests pass.
Tested this along with the upcoming changes, adding working condition
support in an extension.
Description | From | Last Updated |
---|---|---|
"instance" is superfluous. The most pythonic way would be to have a property called operators but get_operators would be OK … |
david | |
Same here: a choices property would be best but get_choices would be OK too. |
david | |
I think it would be clearer if this said it's used internally by the registry framework. As it was I … |
david | |
Col: 80 E501 line too long (80 > 79 characters) |
reviewbot | |
Perhaps set the condition_index on the existing exception and re-raise? |
david | |
Hmm. I don't know if you designed it that way but it looks like you could add a ConditionSet as … |
david | |
I think it would be clearer if this said it's used internally by the registry framework. As it was I … |
david | |
Col: 80 E501 line too long (80 > 79 characters) |
reviewbot |
-
-
"instance" is superfluous. The most pythonic way would be to have a property called
operators
butget_operators
would be OK too. -
-
I think it would be clearer if this said it's used internally by the registry framework. As it was I was looking through the code to figure out what it was used for.
-
-
Hmm. I don't know if you designed it that way but it looks like you could add a ConditionSet as one of the conditions...
-
I think it would be clearer if this said it's used internally by the registry framework. As it was I was looking through the code to figure out what it was used for.
- Change Summary:
-
- Renamed
BaseConditionChoice.get_operator_instances
toget_operators
. - Renamed
BaseConditionChoice.get_instances
toget_choices
. - Clarified docs for
get_defaults
for registry subclasses. - Updated an
InvalidConditionValueError
to set a property on an exception and re-raise, instead of raising a new exception. - Fixed long lines.
- Renamed
- Commit:
-
7e7977d1ddc963068e92e1b13b09eb1e50753bad6f2c4c116756818f2b3f6a5489ad819efe31d7c8
- Diff:
-
Revision 2 (+1844)
-
Tool: PEP8 Style Checker Processed Files: djblets/conditions/tests/test_values.py djblets/conditions/conditions.py djblets/conditions/choices.py djblets/conditions/tests/test_choices.py djblets/conditions/operators.py djblets/conditions/values.py djblets/conditions/errors.py djblets/conditions/__init__.py djblets/conditions/tests/test_conditions.py djblets/conditions/tests/test_operators.py Ignored Files: docs/djblets/guides/registries/index.rst djblets/conditions/tests/__init__.py docs/djblets/coderef/index.rst Tool: Pyflakes Processed Files: djblets/conditions/tests/test_values.py djblets/conditions/conditions.py djblets/conditions/choices.py djblets/conditions/tests/test_choices.py djblets/conditions/operators.py djblets/conditions/values.py djblets/conditions/errors.py djblets/conditions/__init__.py djblets/conditions/tests/test_conditions.py djblets/conditions/tests/test_operators.py Ignored Files: docs/djblets/guides/registries/index.rst djblets/conditions/tests/__init__.py docs/djblets/coderef/index.rst