mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-19 10:59:21 +02:00
Add waffleswitch and hook it to email
This commit is contained in:
parent
ef4e758517
commit
6ad2546a40
6 changed files with 100 additions and 4 deletions
|
@ -15,8 +15,8 @@ from django.contrib.contenttypes.models import ContentType
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from dateutil.relativedelta import relativedelta # type: ignore
|
from dateutil.relativedelta import relativedelta # type: ignore
|
||||||
from epplibwrapper.errors import ErrorCode, RegistryError
|
from epplibwrapper.errors import ErrorCode, RegistryError
|
||||||
from waffle.admin import FlagAdmin
|
from waffle.admin import FlagAdmin, SwitchAdmin
|
||||||
from waffle.models import Sample, Switch
|
from waffle.models import Sample
|
||||||
from registrar.models import Contact, Domain, DomainRequest, DraftDomain, User, Website
|
from registrar.models import Contact, Domain, DomainRequest, DraftDomain, User, Website
|
||||||
from registrar.utility.errors import FSMDomainRequestError, FSMErrorCodes
|
from registrar.utility.errors import FSMDomainRequestError, FSMErrorCodes
|
||||||
from registrar.views.utility.mixins import OrderableFieldsMixin
|
from registrar.views.utility.mixins import OrderableFieldsMixin
|
||||||
|
@ -2308,6 +2308,7 @@ class UserGroupAdmin(AuditedAdmin):
|
||||||
|
|
||||||
|
|
||||||
class WaffleFlagAdmin(FlagAdmin):
|
class WaffleFlagAdmin(FlagAdmin):
|
||||||
|
"""Custom admin implementation of django-waffle's Flag class"""
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Contains meta information about this class"""
|
"""Contains meta information about this class"""
|
||||||
|
|
||||||
|
@ -2315,6 +2316,14 @@ class WaffleFlagAdmin(FlagAdmin):
|
||||||
fields = "__all__"
|
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.unregister(LogEntry) # Unregister the default registration
|
||||||
|
|
||||||
admin.site.register(LogEntry, CustomLogEntryAdmin)
|
admin.site.register(LogEntry, CustomLogEntryAdmin)
|
||||||
|
@ -2340,7 +2349,7 @@ admin.site.register(models.VerifiedByStaff, VerifiedByStaffAdmin)
|
||||||
|
|
||||||
# Register our custom waffle implementations
|
# Register our custom waffle implementations
|
||||||
admin.site.register(models.WaffleFlag, WaffleFlagAdmin)
|
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(Sample)
|
||||||
admin.site.unregister(Switch)
|
|
||||||
|
|
|
@ -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?
|
# If Waffle encounters a reference to a flag that is not in the database, should Waffle create the flag?
|
||||||
WAFFLE_CREATE_MISSING_FLAGS = True
|
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.
|
# The model that will be used to keep track of flags. Extends AbstractUserFlag.
|
||||||
# Used to replace the default flag class (for customization purposes).
|
# Used to replace the default flag class (for customization purposes).
|
||||||
WAFFLE_FLAG_MODEL = "registrar.WaffleFlag"
|
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
|
# endregion
|
||||||
|
|
||||||
# region: Headers-----------------------------------------------------------###
|
# region: Headers-----------------------------------------------------------###
|
||||||
|
|
55
src/registrar/migrations/0096_waffleswitch.py
Normal file
55
src/registrar/migrations/0096_waffleswitch.py
Normal 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",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
|
@ -16,6 +16,7 @@ from .website import Website
|
||||||
from .transition_domain import TransitionDomain
|
from .transition_domain import TransitionDomain
|
||||||
from .verified_by_staff import VerifiedByStaff
|
from .verified_by_staff import VerifiedByStaff
|
||||||
from .waffle_flag import WaffleFlag
|
from .waffle_flag import WaffleFlag
|
||||||
|
from .waffle_switch import WaffleSwitch
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
@ -36,6 +37,7 @@ __all__ = [
|
||||||
"TransitionDomain",
|
"TransitionDomain",
|
||||||
"VerifiedByStaff",
|
"VerifiedByStaff",
|
||||||
"WaffleFlag",
|
"WaffleFlag",
|
||||||
|
"WaffleSwitch",
|
||||||
]
|
]
|
||||||
|
|
||||||
auditlog.register(Contact)
|
auditlog.register(Contact)
|
||||||
|
@ -55,3 +57,4 @@ auditlog.register(Website)
|
||||||
auditlog.register(TransitionDomain)
|
auditlog.register(TransitionDomain)
|
||||||
auditlog.register(VerifiedByStaff)
|
auditlog.register(VerifiedByStaff)
|
||||||
auditlog.register(WaffleFlag)
|
auditlog.register(WaffleFlag)
|
||||||
|
auditlog.register(WaffleSwitch)
|
||||||
|
|
18
src/registrar/models/waffle_switch.py
Normal file
18
src/registrar/models/waffle_switch.py
Normal 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"
|
|
@ -8,6 +8,7 @@ from django.template.loader import get_template
|
||||||
from email.mime.application import MIMEApplication
|
from email.mime.application import MIMEApplication
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from waffle import switch_is_active
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -33,6 +34,9 @@ def send_templated_email(
|
||||||
context as Django's HTML templates. context gives additional information
|
context as Django's HTML templates. context gives additional information
|
||||||
that the template may use.
|
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}")
|
logger.info(f"An email was sent! Template name: {template_name} to {to_address}")
|
||||||
template = get_template(template_name)
|
template = get_template(template_name)
|
||||||
email_body = template.render(context=context)
|
email_body = template.render(context=context)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue