oncall-engine/engine/apps/webhooks/presets/preset_options.py
Ravishankar 6f3f4e3f14
Allow webhook modification by API for advanced webhook (#4175)
# What this PR does

Enables the API to perform updates on the advanced webhooks created via
the UI

## Which issue(s) this PR closes

Closes #3958 

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-04-23 19:18:12 +00:00

33 lines
1.3 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 = {}
ADVANCED_PRESET_META_DATA = {}
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
if webhook_preset_config == settings.ADVANCED_WEBHOOK_PRESET:
ADVANCED_PRESET_META_DATA = preset.metadata
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)