From f896e5dbebd28abd4d56aca4b54853cdcba19535 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Mon, 13 May 2024 02:36:15 -0700 Subject: [PATCH] switch from monkey patching WebhookModel to using swappable --- archivebox/api/models.py | 29 +++++++++++++++++++++++++++++ archivebox/core/admin.py | 16 +++------------- archivebox/core/settings.py | 4 +++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/archivebox/api/models.py b/archivebox/api/models.py index aefbc47c..b48e5f38 100644 --- a/archivebox/api/models.py +++ b/archivebox/api/models.py @@ -8,6 +8,8 @@ from django.conf import settings from django.db import models from django.utils import timezone +from signal_webhooks.models import WebhookBase + from django_stubs_ext.db.models import TypedModelMeta @@ -61,3 +63,30 @@ class APIToken(models.Model): return True + + + + + +# monkey patch django-signals-webhooks to change how it shows up in Admin UI + +class OutboundWebhook(ABIDModel, WebhookBase): + """ + Model used in place of (extending) signals_webhooks.models.WebhookModel. Swapped using: + settings.SIGNAL_WEBHOOKS_CUSTOM_MODEL = 'api.models.OutboundWebhook' + """ + ID_PREFIX = 'whk' + + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) + + WebhookBase._meta.get_field('name').help_text = ( + 'Give your webhook a descriptive name (e.g. Notify ACME Slack channel of any new ArchiveResults).') + WebhookBase._meta.get_field('signal').help_text = ( + 'The type of event the webhook should fire for (e.g. Create, Update, Delete).') + WebhookBase._meta.get_field('ref').help_text = ( + 'Dot import notation of the model the webhook should fire for (e.g. core.models.Snapshot or core.models.ArchiveResult).') + WebhookBase._meta.get_field('endpoint').help_text = ( + 'External URL to POST the webhook notification to (e.g. https://someapp.example.com/webhook/some-webhook-receiver).') + + class Meta(WebhookBase.Meta): + verbose_name = 'API Outbound Webhook' diff --git a/archivebox/core/admin.py b/archivebox/core/admin.py index 41e2db68..632a861b 100644 --- a/archivebox/core/admin.py +++ b/archivebox/core/admin.py @@ -15,8 +15,7 @@ from django.contrib.auth import get_user_model from django import forms -from signal_webhooks.apps import DjangoSignalWebhooksConfig -from signal_webhooks.admin import WebhookAdmin, WebhookModel +from signal_webhooks.admin import WebhookAdmin, get_webhook_model from ..util import htmldecode, urldecode, ansi_to_html @@ -104,23 +103,14 @@ class ArchiveBoxAdmin(admin.AdminSite): return render(template_name='add.html', request=request, context=context) -# monkey patch django-signals-webhooks to change how it shows up in Admin UI -DjangoSignalWebhooksConfig.verbose_name = 'API' -WebhookModel._meta.get_field('name').help_text = 'Give your webhook a descriptive name (e.g. Notify ACME Slack channel of any new ArchiveResults).' -WebhookModel._meta.get_field('signal').help_text = 'The type of event the webhook should fire for (e.g. Create, Update, Delete).' -WebhookModel._meta.get_field('ref').help_text = 'Dot import notation of the model the webhook should fire for (e.g. core.models.Snapshot or core.models.ArchiveResult).' -WebhookModel._meta.get_field('endpoint').help_text = 'External URL to POST the webhook notification to (e.g. https://someapp.example.com/webhook/some-webhook-receiver).' -WebhookModel._meta.app_label = 'api' - - archivebox_admin = ArchiveBoxAdmin() archivebox_admin.register(get_user_model()) archivebox_admin.register(APIToken) -archivebox_admin.register(WebhookModel, WebhookAdmin) +archivebox_admin.register(get_webhook_model(), WebhookAdmin) archivebox_admin.disable_action('delete_selected') -# patch admin with methods to add data views +# patch admin with methods to add data views (implemented by admin_data_views package) from admin_data_views.admin import get_app_list, admin_data_index_view, get_admin_data_urls, get_urls archivebox_admin.get_app_list = get_app_list.__get__(archivebox_admin, ArchiveBoxAdmin) diff --git a/archivebox/core/settings.py b/archivebox/core/settings.py index 20835e3b..7a72edcf 100644 --- a/archivebox/core/settings.py +++ b/archivebox/core/settings.py @@ -421,9 +421,11 @@ LOGGING = { # Add default webhook configuration to the User model +SIGNAL_WEBHOOKS_CUSTOM_MODEL = 'api.models.OutboundWebhook' SIGNAL_WEBHOOKS = { "HOOKS": { - "django.contrib.auth.models.User": ..., # ... is a special value that means "use the default autogenerated hooks" + # ... is a special sigil value that means "use the default autogenerated hooks" + "django.contrib.auth.models.User": ..., "core.models.Snapshot": ..., "core.models.ArchiveResult": ..., "core.models.Tag": ...,