Add waffleswitch and hook it to email

This commit is contained in:
zandercymatics 2024-05-24 10:08:36 -06:00
parent ef4e758517
commit 6ad2546a40
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
6 changed files with 100 additions and 4 deletions

View file

@ -15,8 +15,8 @@ from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from dateutil.relativedelta import relativedelta # type: ignore
from epplibwrapper.errors import ErrorCode, RegistryError
from waffle.admin import FlagAdmin
from waffle.models import Sample, Switch
from waffle.admin import FlagAdmin, SwitchAdmin
from waffle.models import Sample
from registrar.models import Contact, Domain, DomainRequest, DraftDomain, User, Website
from registrar.utility.errors import FSMDomainRequestError, FSMErrorCodes
from registrar.views.utility.mixins import OrderableFieldsMixin
@ -2308,6 +2308,7 @@ class UserGroupAdmin(AuditedAdmin):
class WaffleFlagAdmin(FlagAdmin):
"""Custom admin implementation of django-waffle's Flag class"""
class Meta:
"""Contains meta information about this class"""
@ -2315,6 +2316,14 @@ class WaffleFlagAdmin(FlagAdmin):
fields = "__all__"
class WaffleSwitchAdmin(SwitchAdmin):
"""Custom admin implementation of django-waffle's Switch class"""
class Meta:
"""Contains meta information about this class"""
model = models.WaffleSwitch
fields = "__all__"
admin.site.unregister(LogEntry) # Unregister the default registration
admin.site.register(LogEntry, CustomLogEntryAdmin)
@ -2340,7 +2349,7 @@ admin.site.register(models.VerifiedByStaff, VerifiedByStaffAdmin)
# Register our custom waffle implementations
admin.site.register(models.WaffleFlag, WaffleFlagAdmin)
admin.site.register(models.WaffleSwitch, WaffleSwitchAdmin)
# Unregister Sample and Switch from the waffle library
# Unregister Sample from the waffle library
admin.site.unregister(Sample)
admin.site.unregister(Switch)

View file

@ -329,10 +329,17 @@ SERVER_EMAIL = "root@get.gov"
# If Waffle encounters a reference to a flag that is not in the database, should Waffle create the flag?
WAFFLE_CREATE_MISSING_FLAGS = True
# If Waffle encounters a reference to a switch that is not in the database, should Waffle create the switch?
WAFFLE_CREATE_MISSING_SWITCHES = True
# The model that will be used to keep track of flags. Extends AbstractUserFlag.
# Used to replace the default flag class (for customization purposes).
WAFFLE_FLAG_MODEL = "registrar.WaffleFlag"
# The model that will be used to keep track of switches. Extends AbstractBaseSwitch.
# Used to replace the default switch class (for customization purposes).
WAFFLE_SWITCH_MODEL = "registrar.WaffleSwitch"
# endregion
# region: Headers-----------------------------------------------------------###

View file

@ -0,0 +1,55 @@
# Generated by Django 4.2.10 on 2024-05-24 15:15
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
("registrar", "0095_user_middle_name_user_title"),
]
operations = [
migrations.CreateModel(
name="WaffleSwitch",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
(
"name",
models.CharField(
help_text="The human/computer readable name.", max_length=100, unique=True, verbose_name="Name"
),
),
(
"active",
models.BooleanField(default=False, help_text="Is this switch active?", verbose_name="Active"),
),
(
"note",
models.TextField(blank=True, help_text="Note where this Switch is used.", verbose_name="Note"),
),
(
"created",
models.DateTimeField(
db_index=True,
default=django.utils.timezone.now,
help_text="Date when this Switch was created.",
verbose_name="Created",
),
),
(
"modified",
models.DateTimeField(
default=django.utils.timezone.now,
help_text="Date when this Switch was last modified.",
verbose_name="Modified",
),
),
],
options={
"verbose_name": "waffle switch",
"verbose_name_plural": "Waffle switches",
},
),
]

View file

@ -16,6 +16,7 @@ from .website import Website
from .transition_domain import TransitionDomain
from .verified_by_staff import VerifiedByStaff
from .waffle_flag import WaffleFlag
from .waffle_switch import WaffleSwitch
__all__ = [
@ -36,6 +37,7 @@ __all__ = [
"TransitionDomain",
"VerifiedByStaff",
"WaffleFlag",
"WaffleSwitch",
]
auditlog.register(Contact)
@ -55,3 +57,4 @@ auditlog.register(Website)
auditlog.register(TransitionDomain)
auditlog.register(VerifiedByStaff)
auditlog.register(WaffleFlag)
auditlog.register(WaffleSwitch)

View file

@ -0,0 +1,18 @@
from waffle.models import AbstractBaseSwitch
import logging
logger = logging.getLogger(__name__)
class WaffleSwitch(AbstractBaseSwitch):
"""
Custom implementation of django-waffles 'switch' object.
Read more here: https://waffle.readthedocs.io/en/stable/types/switch.html
Use this class when dealing with switches.
"""
class Meta:
"""Contains meta information about this class"""
verbose_name = "waffle switch"
verbose_name_plural = "Waffle switches"

View file

@ -8,6 +8,7 @@ from django.template.loader import get_template
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from waffle import switch_is_active
logger = logging.getLogger(__name__)
@ -33,6 +34,9 @@ def send_templated_email(
context as Django's HTML templates. context gives additional information
that the template may use.
"""
if switch_is_active("disable_email_sending"):
raise EmailSendingError("Could not send email. Email sending is disabled due to switch 'disable_email_sending'.")
logger.info(f"An email was sent! Template name: {template_name} to {to_address}")
template = get_template(template_name)
email_body = template.render(context=context)