Skip to content

Commit 7057f50

Browse files
committed
refactor and cleanup code
1 parent b1d6534 commit 7057f50

File tree

8 files changed

+12
-32
lines changed

8 files changed

+12
-32
lines changed
File renamed without changes.

ansible_base/lib/dynamic_config/feature_flags/flag_source.py renamed to ansible_base/feature_flags/flag_source.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
class DatabaseCondition(Condition):
6-
"""Condition that includes the FlagState database object"""
6+
"""Condition that includes the AAPFlags database object"""
77

88
def __init__(self, condition, value, required=False, obj=None):
99
super().__init__(condition, value, required=required)
@@ -13,8 +13,8 @@ def __init__(self, condition, value, required=False, obj=None):
1313
class AAPFlagSource(object):
1414

1515
def get_queryset(self):
16-
FlagState = apps.get_model('dab_feature_flags', 'AAPFlag')
17-
return FlagState.objects.all()
16+
aap_flags = apps.get_model('dab_feature_flags', 'AAPFlag')
17+
return aap_flags.objects.all()
1818

1919
def get_flags(self):
2020
flags = {}

ansible_base/feature_flags/utils.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.core.exceptions import ValidationError
66
from flags.sources import get_flags
77

8-
from ansible_base.lib.dynamic_config.feature_flags.platform_flags import AAP_FEATURE_FLAGS
8+
from ansible_base.feature_flags.feature_flags import AAP_FEATURE_FLAGS
99

1010
logger = logging.getLogger('ansible_base.feature_flags.utils')
1111

@@ -14,7 +14,7 @@ def get_django_flags():
1414
return get_flags()
1515

1616

17-
def create_initial_data(**kwargs):
17+
def create_initial_data(**kwargs): # NOSONAR
1818
"""
1919
Loads in platform feature flags when the server starts
2020
"""
@@ -40,22 +40,22 @@ def load_feature_flags():
4040
"""
4141
Loads in all feature flags into the database. Updates them if necessary.
4242
"""
43-
FeatureFlags = apps.get_model('dab_feature_flags', 'AAPFlag')
43+
feature_flags_model = apps.get_model('dab_feature_flags', 'AAPFlag')
4444
for flag in AAP_FEATURE_FLAGS:
4545
try:
46-
existing_flag = FeatureFlags.objects.filter(name=flag['name'], condition=flag['condition'])
46+
existing_flag = feature_flags_model.objects.filter(name=flag['name'], condition=flag['condition'])
4747
if existing_flag:
4848
feature_flag = update_feature_flag(existing_flag.first(), flag)
4949
else:
5050
if hasattr(settings, flag['name']):
5151
flag['value'] = getattr(settings, flag['name'])
52-
feature_flag = FeatureFlags(**flag)
52+
feature_flag = feature_flags_model(**flag)
5353
feature_flag.full_clean()
5454
feature_flag.save()
5555
except ValidationError as e:
5656
# Ignore this error unless better way to bypass this
5757
if e.messages[0] == 'Aap flag with this Name and Condition already exists.':
58-
pass
58+
logger.info(f"Feature flag: {flag['name']} already exists")
5959
else:
6060
error_msg = f"Invalid feature flag: {flag['name']}. Error: {e}"
6161
logger.error(error_msg)
@@ -70,10 +70,11 @@ def delete_feature_flags():
7070
for _flag in AAP_FEATURE_FLAGS:
7171
if flag.name == _flag['name'] and flag.condition == _flag['condition']:
7272
found = True
73-
continue
73+
break
7474
if found:
7575
continue
7676
if not found:
77+
logger.info(f"Deleting feature flag: {flag.name} as it is no longer available as a platform flag")
7778
flag.delete()
7879

7980
delete_feature_flags()

ansible_base/feature_flags/views.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from ansible_base.lib.utils.views.ansible_base import AnsibleBaseView
1111
from ansible_base.lib.utils.views.django_app_api import AnsibleBaseDjangoAppApiView
1212
from ansible_base.lib.utils.views.permissions import IsSuperuserOrAuditor, try_add_oauth2_scope_permission
13-
14-
# from ansible_base.oauth2_provider.permissions import OAuth2ScopePermission
1513
from ansible_base.rest_pagination import DefaultPaginator
1614

1715
from .utils import get_django_flags

ansible_base/lib/dynamic_config/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
load_envvars,
66
load_python_file_with_injected_context,
77
load_standard_settings_files,
8-
toggle_database_feature_flags,
98
toggle_feature_flags,
109
validate,
1110
)
@@ -18,6 +17,5 @@
1817
"load_python_file_with_injected_context",
1918
"load_standard_settings_files",
2019
"toggle_feature_flags",
21-
"toggle_database_feature_flags",
2220
"validate",
2321
]

ansible_base/lib/dynamic_config/dynaconf_helpers.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
from dynaconf.loaders.yaml_loader import yaml
1616
from dynaconf.utils.files import glob
1717
from dynaconf.utils.functional import empty
18-
from flags.sources import get_flags
19-
from flags.state import disable_flag, enable_flag
2018

2119
from ansible_base.lib.dynamic_config.settings_logic import get_mergeable_dab_settings
2220

@@ -317,17 +315,3 @@ def toggle_feature_flags(settings: Dynaconf) -> dict[str, Any]:
317315
feature_content[0]["value"] = installer_value
318316
data[f"FLAGS__{feature_name}"] = feature_content
319317
return data
320-
321-
322-
def toggle_database_feature_flags(settings: Dynaconf) -> dict[str, Any]:
323-
"""Toggle FLAGS based on installer settings.
324-
FLAGS is a django-flags formatted dictionary.
325-
Installers will place `FEATURE_SOME_PLATFORM_FLAG_ENABLED=True/False` in the settings file.
326-
This function will update the value in the database with the expected boolean value
327-
"""
328-
for feature_name in get_flags():
329-
if (installer_value := settings.get(feature_name, empty)) is not empty:
330-
if installer_value:
331-
enable_flag(feature_name)
332-
else:
333-
disable_flag(feature_name)

ansible_base/lib/dynamic_config/settings_logic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def get_mergeable_dab_settings(settings: dict) -> dict: # NOSONAR
306306
if "flags" not in installed_apps:
307307
installed_apps.append('flags')
308308

309-
dab_data['FLAG_SOURCES'] = ('ansible_base.lib.dynamic_config.feature_flags.flag_source.AAPFlagSource',)
309+
dab_data['FLAG_SOURCES'] = ('ansible_base.feature_flags.flag_source.AAPFlagSource',)
310310

311311
found_template_backend = False
312312
template_context_processor = 'django.template.context_processors.request'

test_app/tests/authentication/management/test_authenticators.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def test_authenticators_cli_initialize(
9797
err = StringIO()
9898

9999
# Sanity check:
100-
# _system user exists
101100
assert django_user_model.objects.count() == 0
102101

103102
# Optionally create admin user

0 commit comments

Comments
 (0)