From 1fbd3a2c265935e55a28061328daede616f72c5a Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 24 Feb 2020 16:50:20 -0500 Subject: [PATCH] Convert additional_headers to a TextField --- .../migrations/0038_webhook_body_template.py | 23 ---------- .../0038_webhook_template_support.py | 43 +++++++++++++++++++ netbox/extras/models.py | 3 +- 3 files changed, 44 insertions(+), 25 deletions(-) delete mode 100644 netbox/extras/migrations/0038_webhook_body_template.py create mode 100644 netbox/extras/migrations/0038_webhook_template_support.py diff --git a/netbox/extras/migrations/0038_webhook_body_template.py b/netbox/extras/migrations/0038_webhook_body_template.py deleted file mode 100644 index 1e23a2303..000000000 --- a/netbox/extras/migrations/0038_webhook_body_template.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 2.2.10 on 2020-02-24 20:12 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('extras', '0037_configcontexts_clusters'), - ] - - operations = [ - migrations.AddField( - model_name='webhook', - name='body_template', - field=models.TextField(blank=True), - ), - migrations.AlterField( - model_name='webhook', - name='http_content_type', - field=models.CharField(default='application/json', max_length=100), - ), - ] diff --git a/netbox/extras/migrations/0038_webhook_template_support.py b/netbox/extras/migrations/0038_webhook_template_support.py new file mode 100644 index 000000000..80a1d2b7d --- /dev/null +++ b/netbox/extras/migrations/0038_webhook_template_support.py @@ -0,0 +1,43 @@ +import json + +from django.db import migrations, models + + +def json_to_text(apps, schema_editor): + """ + Convert a JSON representation of HTTP headers to key-value pairs (one header per line) + """ + Webhook = apps.get_model('extras', 'Webhook') + for webhook in Webhook.objects.exclude(additional_headers=''): + data = json.loads(webhook.additional_headers) + headers = ['{}: {}'.format(k, v) for k, v in data.items()] + Webhook.objects.filter(pk=webhook.pk).update(additional_headers='\n'.join(headers)) + + +class Migration(migrations.Migration): + + dependencies = [ + ('extras', '0037_configcontexts_clusters'), + ] + + operations = [ + migrations.AddField( + model_name='webhook', + name='body_template', + field=models.TextField(blank=True), + ), + migrations.AlterField( + model_name='webhook', + name='additional_headers', + field=models.TextField(blank=True, default=''), + preserve_default=False, + ), + migrations.AlterField( + model_name='webhook', + name='http_content_type', + field=models.CharField(default='application/json', max_length=100), + ), + migrations.RunPython( + code=json_to_text + ), + ] diff --git a/netbox/extras/models.py b/netbox/extras/models.py index 896595524..52aba763d 100644 --- a/netbox/extras/models.py +++ b/netbox/extras/models.py @@ -90,8 +90,7 @@ class Webhook(models.Model): help_text='The complete list of official content types is available ' 'here.' ) - additional_headers = JSONField( - null=True, + additional_headers = models.TextField( blank=True, help_text="User supplied headers which should be added to the request in addition to the HTTP content type. " "Headers are supplied as key/value pairs in a JSON object."