-
Notifications
You must be signed in to change notification settings - Fork 43
Add API and management commands for enabling/disabling flags #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| def _set_flag_state( | ||
| flag_name, state, create_boolean_condition=True, request=None | ||
| ): | ||
| """ A private function to sets a boolean condition to the desired state """ | ||
| flags = get_flags(request=request) | ||
| flag = flags.get(flag_name) | ||
| if flag is None: | ||
| raise KeyError(f"No flag with name {flag_name} exists") | ||
|
|
||
| db_boolean_condition = next( | ||
| ( | ||
| c | ||
| for c in flag.conditions | ||
| if c.condition == "boolean" and getattr(c, "obj", None) is not None | ||
| ), | ||
| None, | ||
| ) | ||
|
|
||
| if db_boolean_condition is not None: | ||
| # We already have a boolean condition | ||
| boolean_condition_obj = db_boolean_condition.obj | ||
| elif db_boolean_condition is None and create_boolean_condition: | ||
| # We can create a boolean condition and we need to. | ||
| boolean_condition_obj = FlagState.objects.create( | ||
| name=flag_name, condition="boolean", value="True" | ||
| ) | ||
| else: | ||
| raise ValueError(f"Flag {flag_name} does not have a boolean condition") | ||
|
|
||
| boolean_condition_obj.value = state | ||
| boolean_condition_obj.save() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The body of this function is lifted from the code in Wagtail-Flags that enables/disables flags from a button in the UI. Once this is merged and released, that code should be replaced with a API calls to the enable_flag/disable_flag API introduced here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two management commands and api functions will be useful.
The commands work for me as expected, and the full test matrix succeeds.
A couple nits noted.
This change adds two flag state functions, `enable_flag` and `disable_flag`, that will either add or modify an existing boolean condition to either enable or disable a flag. It also adds an `enable_flag` and `disable_flag` management commands which call those functions. This will enable easier enabling/disabling of a flag through automations, etc. If a flag has required conditions, those will take precedence over the boolean that's added/modified. Update flags/state.py Co-authored-by: william higgins <higs4281@users.noreply.github.com>
59a0949 to
821e10a
Compare
This change adds two flag state functions,
enable_flaganddisable_flag, that will either add or modify an existing boolean condition to either enable or disable a flag.It also adds an
enable_flaganddisable_flagmanagement commands which call those functions. This will enable easier enabling/disabling of a flag through automations, etc.If a flag has required conditions, those will take precedence over the boolean that's added/modified.