oncall-engine/engine/apps/webhooks/presets/preset_options.py
Michael Derynck ab56e53fe8
Allow deletion for removed presets (#3072)
If a webhook preset is removed from configuration while there are still
existing webhooks referencing it they will have the following behavior:

- Webhook can be viewed
- Webhook can be deleted
- Webhook cannot be modified
- Webhook will not execute

Removing a preset from configuration effectively disables all existing
webhooks referencing it while retaining their data.
2023-09-27 10:21:55 -06:00

30 lines
1.2 KiB
Python

from importlib import import_module
from django.conf import settings
from django.db.models.signals import pre_save
from django.dispatch import receiver
from apps.webhooks.models import Webhook
class WebhookPresetOptions:
WEBHOOK_PRESETS = {}
for webhook_preset_config in settings.INSTALLED_WEBHOOK_PRESETS:
module_path, class_name = webhook_preset_config.rsplit(".", 1)
module = import_module(module_path)
preset = getattr(module, class_name)()
WEBHOOK_PRESETS[preset.metadata.id] = preset
WEBHOOK_PRESET_CHOICES = [webhook_preset.metadata for webhook_preset in WEBHOOK_PRESETS.values()]
@receiver(pre_save, sender=Webhook)
def listen_for_webhook_save(sender: Webhook, instance: Webhook, raw: bool, *args, **kwargs) -> None:
if instance.preset and not instance.deleted_at:
if instance.preset in WebhookPresetOptions.WEBHOOK_PRESETS:
WebhookPresetOptions.WEBHOOK_PRESETS[instance.preset].override_parameters_before_save(instance)
else:
raise NotImplementedError(f"Webhook references unknown preset implementation {instance.preset}")
pre_save.connect(listen_for_webhook_save, Webhook)