-
Couldn't load subscription status.
- 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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Management Commands | ||
|
|
||
| Django-Flags provides two management commands that allow for enabling and disabling of feature flags from the command line. | ||
|
|
||
| ## `enable_flag FLAG_NAME` | ||
|
|
||
| Enable a flag by adding or setting an existing database boolean condition to `True`. If the flag has other required conditions, those will take precedence. | ||
|
|
||
| This command calls the [`flags.state.enable_flag` function](../api/state#enable_flagflag_name-create_boolean_conditiontrue-requestnone) function. | ||
|
|
||
| ``` | ||
| ./manage.py enable_flag MY_FLAG | ||
| ``` | ||
|
|
||
| ## `disable_flag FLAG_NAME` | ||
|
|
||
| Disable a flag by adding or setting an existing database boolean condition to `False`. If the flag has other required conditions, those will take precedence. | ||
|
|
||
| This command calls the [`flags.state.enable_flag` function](../api/state#disable_flagflag_name-create_boolean_conditiontrue-requestnone) function. | ||
|
|
||
| ``` | ||
| ./manage.py disable_flag MY_FLAG | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from django.core.management.base import BaseCommand, CommandError | ||
|
|
||
| from flags.state import disable_flag | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = ( | ||
| "Disables the given feature flag " | ||
| "unless any required conditions (if defined) are met" | ||
| ) | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "flag_name", help="The name of the feature flag to disable" | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| try: | ||
| disable_flag(options["flag_name"]) | ||
| except KeyError as e: | ||
| raise CommandError(e) | ||
|
|
||
| self.stdout.write( | ||
| self.style.SUCCESS(f"Successfully disabled {options['flag_name']}") | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from django.core.management.base import BaseCommand, CommandError | ||
|
|
||
| from flags.state import enable_flag | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = ( | ||
| "Enables the given feature flag " | ||
| "when any required conditions (if defined) are met" | ||
| ) | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "flag_name", help="The name of the feature flag to enable" | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| try: | ||
| enable_flag(options["flag_name"]) | ||
| except KeyError as e: | ||
| raise CommandError(e) | ||
|
|
||
| self.stdout.write( | ||
| self.style.SUCCESS(f"Successfully enabled {options['flag_name']}") | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from io import StringIO | ||
|
|
||
| from django.core.management import call_command | ||
| from django.core.management.base import CommandError | ||
| from django.test import TestCase | ||
|
|
||
| from flags.models import FlagState | ||
| from flags.state import flag_disabled | ||
|
|
||
|
|
||
| class disableFlagTestCase(TestCase): | ||
| def test_disable_flag(self): | ||
| FlagState.objects.create( | ||
| name="DB_FLAG", condition="boolean", value="True" | ||
| ) | ||
| out = StringIO() | ||
| self.assertFalse(flag_disabled("DB_FLAG")) | ||
| call_command("disable_flag", "DB_FLAG", stdout=out) | ||
| self.assertTrue(flag_disabled("DB_FLAG")) | ||
| self.assertIn("Successfully disabled", out.getvalue()) | ||
|
|
||
| def test_disable_flag_non_existent_flag(self): | ||
| with self.assertRaises(CommandError): | ||
| call_command("disable_flag", "FLAG_DOES_NOT_EXIST") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from io import StringIO | ||
|
|
||
| from django.core.management import call_command | ||
| from django.core.management.base import CommandError | ||
| from django.test import TestCase | ||
|
|
||
| from flags.state import flag_enabled | ||
|
|
||
|
|
||
| class EnableFlagTestCase(TestCase): | ||
| def test_enable_flag(self): | ||
| out = StringIO() | ||
| self.assertFalse(flag_enabled("DB_FLAG")) | ||
| call_command("enable_flag", "DB_FLAG", stdout=out) | ||
| self.assertTrue(flag_enabled("DB_FLAG")) | ||
| self.assertIn("Successfully enabled", out.getvalue()) | ||
|
|
||
| def test_enable_flag_non_existent_flag(self): | ||
| with self.assertRaises(CommandError): | ||
| call_command("enable_flag", "FLAG_DOES_NOT_EXIST") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.