Skip to content

Commit

Permalink
[#20] Add an identifier field to the Subscription model
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Dec 9, 2024
1 parent 1629259 commit 24d37e0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion notifications_api_common/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 5.1.4 on 2024-12-09 08:15

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,
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,
),
),
]
7 changes: 7 additions & 0 deletions notifications_api_common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_register_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 24d37e0

Please sign in to comment.