Skip to content

Commit

Permalink
✨ Add ConfigurationStep for NotificationsConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Dec 5, 2024
1 parent fb602a2 commit 28912c1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions notifications_api_common/contrib/setup_configuration/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django_setup_configuration.models import ConfigurationModel, DjangoModelRef

from notifications_api_common.models import NotificationsConfig


class NotificationConfigurationModel(ConfigurationModel):
notifications_api_service_identifier: str = DjangoModelRef(
NotificationsConfig, "notifications_api_service", default=""
)

class Meta:
django_model_refs = {
NotificationsConfig: [
"notification_delivery_max_retries",
"notification_delivery_retry_backoff",
"notification_delivery_retry_backoff_max",
]
}
51 changes: 51 additions & 0 deletions notifications_api_common/contrib/setup_configuration/steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django_setup_configuration.configuration import BaseConfigurationStep
from zgw_consumers.models import Service

from notifications_api_common.models import NotificationsConfig

from .models import NotificationConfigurationModel


def get_service(slug: str) -> Service:
"""
Try to find a Service and re-raise DoesNotExist with the identifier
to make debugging easier
"""
try:
return Service.objects.get(slug=slug)
except Service.DoesNotExist as e:
raise Service.DoesNotExist(f"{str(e)} (identifier = {slug})")


class NotificationConfigurationStep(
BaseConfigurationStep[NotificationConfigurationModel]
):
"""
Configure settings for Notificaties API
"""

verbose_name = "Configuration for Notificaties API"
config_model = NotificationConfigurationModel
namespace = "notifications_config"
enable_setting = "notifications_config_enable"

def execute(self, model: NotificationConfigurationModel):
config = NotificationsConfig.get_solo()

if identifier := model.notifications_api_service_identifier:
config.notifications_api_service = get_service(identifier)

if model.notification_delivery_max_retries:
config.notification_delivery_max_retries = (
model.notification_delivery_max_retries
)
if model.notification_delivery_retry_backoff:
config.notification_delivery_retry_backoff = (
model.notification_delivery_retry_backoff
)
if model.notification_delivery_retry_backoff_max:
config.notification_delivery_retry_backoff_max = (
model.notification_delivery_retry_backoff_max
)

config.save()

0 comments on commit 28912c1

Please sign in to comment.