-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add ConfigurationStep for NotificationsConfig
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions
18
notifications_api_common/contrib/setup_configuration/models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
notifications_api_common/contrib/setup_configuration/steps.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |