From 5d772d7055ad782408248116fc97b331d3087100 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 5 Dec 2019 17:11:59 -0500 Subject: [PATCH] Webhook.http_content_type to slug (#3569) --- netbox/extras/choices.py | 20 +++++++++++ netbox/extras/constants.py | 8 ----- .../migrations/0032_3569_webhook_fields.py | 35 +++++++++++++++++++ netbox/extras/models.py | 7 ++-- 4 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 netbox/extras/migrations/0032_3569_webhook_fields.py diff --git a/netbox/extras/choices.py b/netbox/extras/choices.py index af5dedb82..b483d098f 100644 --- a/netbox/extras/choices.py +++ b/netbox/extras/choices.py @@ -118,3 +118,23 @@ class ExportTemplateLanguageChoices(ChoiceSet): LANGUAGE_DJANGO: 10, LANGUAGE_JINJA2: 20, } + + +# +# Webhooks +# + +class WebhookContentTypeChoices(ChoiceSet): + + CONTENTTYPE_JSON = 'application/json' + CONTENTTYPE_FORMDATA = 'application/x-www-form-urlencoded' + + CHOICES = ( + (CONTENTTYPE_JSON, 'JSON'), + (CONTENTTYPE_FORMDATA, 'Form data'), + ) + + LEGACY_MAP = { + CONTENTTYPE_JSON: 1, + CONTENTTYPE_FORMDATA: 2, + } diff --git a/netbox/extras/constants.py b/netbox/extras/constants.py index f087648fb..c64715203 100644 --- a/netbox/extras/constants.py +++ b/netbox/extras/constants.py @@ -99,14 +99,6 @@ LOG_LEVEL_CODES = { LOG_FAILURE: 'failure', } -# webhook content types -WEBHOOK_CT_JSON = 1 -WEBHOOK_CT_X_WWW_FORM_ENCODED = 2 -WEBHOOK_CT_CHOICES = ( - (WEBHOOK_CT_JSON, 'application/json'), - (WEBHOOK_CT_X_WWW_FORM_ENCODED, 'application/x-www-form-urlencoded'), -) - # Models which support registered webhooks WEBHOOK_MODELS = [ 'circuits.circuit', diff --git a/netbox/extras/migrations/0032_3569_webhook_fields.py b/netbox/extras/migrations/0032_3569_webhook_fields.py new file mode 100644 index 000000000..a7bd2e3b5 --- /dev/null +++ b/netbox/extras/migrations/0032_3569_webhook_fields.py @@ -0,0 +1,35 @@ +from django.db import migrations, models + + +WEBHOOK_CONTENTTYPE_CHOICES = ( + (1, 'application/json'), + (2, 'application/x-www-form-urlencoded'), +) + + +def webhook_contenttype_to_slug(apps, schema_editor): + Webhook = apps.get_model('extras', 'Webhook') + for id, slug in WEBHOOK_CONTENTTYPE_CHOICES: + Webhook.objects.filter(http_content_type=str(id)).update(http_content_type=slug) + + +class Migration(migrations.Migration): + atomic = False + + dependencies = [ + ('extras', '0031_3569_exporttemplate_fields'), + ] + + operations = [ + + # Webhook.http_content_type + migrations.AlterField( + model_name='webhook', + name='http_content_type', + field=models.CharField(default='application/json', max_length=50), + ), + migrations.RunPython( + code=webhook_contenttype_to_slug + ), + + ] diff --git a/netbox/extras/models.py b/netbox/extras/models.py index 512eca3ea..48d61ee0f 100644 --- a/netbox/extras/models.py +++ b/netbox/extras/models.py @@ -63,9 +63,10 @@ class Webhook(models.Model): verbose_name='URL', help_text="A POST will be sent to this URL when the webhook is called." ) - http_content_type = models.PositiveSmallIntegerField( - choices=WEBHOOK_CT_CHOICES, - default=WEBHOOK_CT_JSON, + http_content_type = models.CharField( + max_length=50, + choices=WebhookContentTypeChoices, + default=WebhookContentTypeChoices.CONTENTTYPE_JSON, verbose_name='HTTP content type' ) additional_headers = JSONField(