diff --git a/notifications_api_common/admin.py b/notifications_api_common/admin.py index 5a36adf..325601f 100644 --- a/notifications_api_common/admin.py +++ b/notifications_api_common/admin.py @@ -34,5 +34,5 @@ def register_webhook(modeladmin, request, queryset): @admin.register(Subscription) class SubscriptionAdmin(admin.ModelAdmin): - list_display = ("callback_url", "channels", "_subscription") + list_display = ("identifier", "callback_url", "channels", "_subscription") actions = [register_webhook] diff --git a/notifications_api_common/migrations/0009_add_subscription_identifier.py b/notifications_api_common/migrations/0009_add_subscription_identifier.py new file mode 100644 index 0000000..43a861b --- /dev/null +++ b/notifications_api_common/migrations/0009_add_subscription_identifier.py @@ -0,0 +1,55 @@ +from django.db import migrations, models + + +def generate_missing_identifiers(apps, schema_editor): + Subscription = apps.get_model("notifications_api_common", "Subscription") + + count = 1 + for subscription in Subscription.objects.all(): + while Subscription.objects.filter( + identifier=f"subscription-{count:02d}" + ).exists(): + count += 1 + + subscription.identifier = f"subscription-{count:02d}" + subscription.save(update_fields=["identifier"]) + count += 1 + + +class Migration(migrations.Migration): + + dependencies = [ + ( + "notifications_api_common", + "0008_merge_0006_auto_20221213_0214_0007_auto_20221206_0414", + ), + ] + + operations = [ + migrations.AddField( + model_name="subscription", + name="identifier", + field=models.SlugField( + help_text="A human-friendly identifier to refer to this subscription.", + max_length=64, + null=True, + blank=True, + unique=True, + ), + ), + migrations.RunPython( + generate_missing_identifiers, + reverse_code=migrations.RunPython.noop, + ), + migrations.AlterField( + model_name="subscription", + name="identifier", + field=models.SlugField( + help_text="A human-friendly identifier to refer to this subscription.", + max_length=64, + unique=True, + blank=False, + null=False, + ), + ), + ] diff --git a/notifications_api_common/models.py b/notifications_api_common/models.py index 0c5f177..e2742d6 100644 --- a/notifications_api_common/models.py +++ b/notifications_api_common/models.py @@ -76,6 +76,13 @@ class Subscription(models.Model): TODO: on change/update, update the subscription """ + identifier = models.SlugField( + unique=True, + blank=False, + null=False, + max_length=64, + help_text=_("A human-friendly identifier to refer to this subscription."), + ) callback_url = models.URLField( _("callback url"), help_text=_("Where to send the notifications (webhook url)") ) diff --git a/tests/test_register_webhook.py b/tests/test_register_webhook.py index 2b651cb..9c4ccdf 100644 --- a/tests/test_register_webhook.py +++ b/tests/test_register_webhook.py @@ -27,6 +27,7 @@ def test_register_webhook_success( request_with_middleware, notifications_config, *mocks ): subscription = Subscription.objects.create( + identifier="sub", callback_url="https://example.com/callback", client_id="client_id", secret="secret", @@ -48,6 +49,7 @@ def test_register_webhook_request_exception( request_with_middleware, notifications_config ): Subscription.objects.create( + identifier="sub", callback_url="https://example.com/callback", client_id="client_id", secret="secret", @@ -68,6 +70,7 @@ def test_register_webhook_request_exception( @pytest.mark.django_db def test_register_webhook_http_error(request_with_middleware, notifications_config): Subscription.objects.create( + identifier="sub", callback_url="https://example.com/callback", client_id="client_id", secret="secret",