Skip to content

Commit

Permalink
clean-up based on PR comments, add import explanation, change delete …
Browse files Browse the repository at this point in the history
…logic and make config path a setting, change get_or_create to update_or_create
  • Loading branch information
John Tordoff committed Jun 29, 2022
1 parent 74f70bd commit 07fb352
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
3 changes: 2 additions & 1 deletion osf/features.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yaml
from website import settings

with open('osf/features.yaml', 'r') as stream:
with open(settings.WAFFLE_VALUES_YAML, 'r') as stream:
features = yaml.safe_load(stream)

for flag in features['flags']:
Expand Down
35 changes: 19 additions & 16 deletions osf/management/commands/manage_switch_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@
import yaml

from django.core.management.base import BaseCommand
from django.db import transaction
from website import settings

logger = logging.getLogger(__name__)


def manage_waffle(delete_waffle=False):
# Inline importation of models is done to so for use in post migrate signal.
from django.apps import apps

Flag = apps.get_model('waffle.Flag')
Switch = apps.get_model('waffle.Switch')

with open('osf/features.yaml', 'r') as stream:
features = yaml.safe_load(stream)
for flag in features['flags']:
flag.pop('flag_name')
Flag.objects.get_or_create(name=flag['name'], defaults=flag)
for switch in features['switches']:
switch.pop('flag_name')
Switch.objects.get_or_create(name=switch['name'], defaults=switch)

if delete_waffle:
results = Switch.objects.exclude(name__in=[switch['name'] for switch in features['switches']]).delete()
logger.info(f'Deleting switches: {results}')

results = Flag.objects.exclude(name__in=[flag['name'] for flag in features['flags']]).delete()
logger.info(f'Deleting flags: {results}')
with transaction.atomic():
if delete_waffle:
results = Switch.objects.all().delete()
logger.info(f'Deleting switches: {results}')
results = Flag.objects.all().delete()
logger.info(f'Deleting flags: {results}')

with open(settings.WAFFLE_VALUES_YAML, 'r') as stream:
features = yaml.safe_load(stream)
for flag in features['flags']:
flag.pop('flag_name')
Flag.objects.update_or_create(name=flag['name'], defaults=flag)
for switch in features['switches']:
switch.pop('flag_name')
Switch.objects.update_or_create(name=switch['name'], defaults=switch)



class Command(BaseCommand):
Expand Down
1 change: 1 addition & 0 deletions website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2101,3 +2101,4 @@ def from_node_usage(cls, usage_bytes, private_limit=None, public_limit=None):
CAS_LOG_LEVEL = 3 # ERROR

PREPRINT_METRICS_START_DATE = datetime.datetime(2019, 1, 1)
WAFFLE_VALUES_YAML = 'osf/features.yaml'

0 comments on commit 07fb352

Please sign in to comment.