mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-17 10:07:04 +02:00
Add waffle flags to migrations
This commit is contained in:
parent
5e2ec5d1c8
commit
91545f45be
6 changed files with 81 additions and 20 deletions
0
docs/developer/feature-flags.md
Normal file
0
docs/developer/feature-flags.md
Normal file
|
@ -1,4 +1,4 @@
|
||||||
# Generated by Django 4.2.10 on 2024-04-30 16:25
|
# Generated by Django 4.2.10 on 2024-04-30 16:56
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
@ -33,9 +33,8 @@ class Migration(migrations.Migration):
|
||||||
('users', models.ManyToManyField(blank=True, help_text='Activate this flag for these users.', to=settings.AUTH_USER_MODEL, verbose_name='Users')),
|
('users', models.ManyToManyField(blank=True, help_text='Activate this flag for these users.', to=settings.AUTH_USER_MODEL, verbose_name='Users')),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'verbose_name': 'Flag',
|
'verbose_name': 'waffle flag',
|
||||||
'verbose_name_plural': 'Flags',
|
'verbose_name_plural': 'Waffle flags',
|
||||||
'abstract': False,
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
38
src/registrar/migrations/0091_create_waffle_flags_v01.py
Normal file
38
src/registrar/migrations/0091_create_waffle_flags_v01.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
# This migration creates default WaffleFlag objects for our DB.
|
||||||
|
# Whenever you add to the `create_waffle_flags` function, increment/copy this
|
||||||
|
# migration by one
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
from registrar.models import WaffleFlag
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
# For linting: RunPython expects a function reference,
|
||||||
|
# so let's give it one
|
||||||
|
def create_flags():
|
||||||
|
"""
|
||||||
|
Populates pre-existing flags we wish to associate.
|
||||||
|
Only generates a flag name and a note, but no other data is loaded at this point.
|
||||||
|
"""
|
||||||
|
WaffleFlag.create_waffle_flags_for_migrations()
|
||||||
|
|
||||||
|
def delete_flags():
|
||||||
|
"""
|
||||||
|
Deletes all prexisting flags.
|
||||||
|
Does not delete flags not defined in this scope (user generated).
|
||||||
|
"""
|
||||||
|
WaffleFlag.delete_waffle_flags_for_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("registrar", "0090_waffleflag"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(
|
||||||
|
code=create_flags,
|
||||||
|
reverse_code=delete_flags,
|
||||||
|
atomic=True,
|
||||||
|
),
|
||||||
|
]
|
|
@ -17,21 +17,45 @@ class WaffleFlag(AbstractUserFlag):
|
||||||
verbose_name = "waffle flag"
|
verbose_name = "waffle flag"
|
||||||
verbose_name_plural = "Waffle flags"
|
verbose_name_plural = "Waffle flags"
|
||||||
|
|
||||||
|
# Defines which waffle flags should be created at startup.
|
||||||
|
# Add to this list if you want to add another flag that is generated at startup.
|
||||||
|
# When you do so, you will need to add a new instance of `0091_create_waffle_flags_v{version_number}`
|
||||||
|
# in registrar/migrations for that change to update automatically on migrate.
|
||||||
|
DEFAULT_WAFFLE_FLAGS = [
|
||||||
|
"profile_feature",
|
||||||
|
"dns_hosting_feature"
|
||||||
|
]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_waffle_flags(cls):
|
def create_waffle_flags_for_migrations(cls):
|
||||||
"""
|
"""
|
||||||
Creates a pre-defined list of flags for our migrations.
|
Creates a pre-defined list of flags for our migrations.
|
||||||
"""
|
"""
|
||||||
logger.info("Creating default waffle flags...")
|
logger.info("Creating default waffle flags...")
|
||||||
try:
|
# Flags can be changed through the command line or through django admin.
|
||||||
# Flags can be activated through the command line or through django admin.
|
|
||||||
# To keep the scope of this function minimal and simple, if we require additional
|
# To keep the scope of this function minimal and simple, if we require additional
|
||||||
# config on these flag, it should be done in a seperate function or as a command.
|
# config on these flag, it should be done in a seperate function or as a command.
|
||||||
flag_names = [
|
for flag_name in cls.DEFAULT_WAFFLE_FLAGS:
|
||||||
"profile_feature",
|
try:
|
||||||
"dns_hosting_feature",
|
cls.objects.update_or_create(
|
||||||
]
|
name=flag_name,
|
||||||
flags = [cls(name=flag_name) for flag_name in flag_names]
|
# Booleans like superusers or is_staff can be set here, if needed.
|
||||||
cls.objects.bulk_create(flags)
|
defaults={
|
||||||
|
'note': 'Auto-generated waffle flag'
|
||||||
|
}
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"An error occurred when attempting to create WaffleFlags: {e}")
|
logger.error(f"An error occurred when attempting to add or update flag {flag_name}: {e}")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def delete_waffle_flags_for_migrations(cls):
|
||||||
|
"""
|
||||||
|
Delete a pre-defined list of flags for our migrations (the reverse_code operation).
|
||||||
|
"""
|
||||||
|
logger.info("Deleting default waffle flags...")
|
||||||
|
existing_flags = cls.objects.filter(name__in=cls.DEFAULT_WAFFLE_FLAGS)
|
||||||
|
for flag in existing_flags:
|
||||||
|
try:
|
||||||
|
cls.objects.get(name=flag.name).delete()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"An error occurred when attempting to delete flag {flag.name}: {e}")
|
|
@ -72,9 +72,6 @@
|
||||||
|
|
||||||
{% if not IS_PRODUCTION %}
|
{% if not IS_PRODUCTION %}
|
||||||
{% include "includes/non-production-alert.html" %}
|
{% include "includes/non-production-alert.html" %}
|
||||||
{% if has_profile_feature_flag %}
|
|
||||||
<h2>it worked!</h2>
|
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<section class="usa-banner" aria-label="Official website of the United States government">
|
<section class="usa-banner" aria-label="Official website of the United States government">
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
<div class="usa-alert">
|
<div class="usa-alert">
|
||||||
<div class="usa-alert__body {% if add_body_class %}{{ add_body_class }}{% endif %}">
|
<div class="usa-alert__body {% if add_body_class %}{{ add_body_class }}{% endif %}">
|
||||||
<b>Attention:</b> You are on a test site.
|
<b>Attention:</b> You are on a test site.
|
||||||
|
{% if has_profile_feature_flag %}
|
||||||
|
The profile_feature flag is active.
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
Loading…
Add table
Add a link
Reference in a new issue