Skip to content

Conversation

@willbarton
Copy link
Member

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.

@willbarton willbarton requested a review from a team February 23, 2021 18:39
Comment on lines 19 to 49
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()
Copy link
Member Author

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.

Copy link
Member

@higs4281 higs4281 left a 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.

willbarton and others added 2 commits February 23, 2021 16:07
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>
@willbarton willbarton merged commit c0e3329 into main Feb 25, 2021
@willbarton willbarton deleted the management-commands branch February 25, 2021 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants