Skip to content

Commit

Permalink
Webhook.http_content_type to slug (#3569)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Dec 5, 2019
1 parent 89e720c commit 5d772d7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
20 changes: 20 additions & 0 deletions netbox/extras/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
8 changes: 0 additions & 8 deletions netbox/extras/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@
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',
Expand Down
35 changes: 35 additions & 0 deletions netbox/extras/migrations/0032_3569_webhook_fields.py
Original file line number Diff line number Diff line change
@@ -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
),

]
7 changes: 4 additions & 3 deletions netbox/extras/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 5d772d7

Please sign in to comment.