mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Always use a JSON object to convey change data when no body template is present
This commit is contained in:
@ -42,7 +42,7 @@ The following data is available as context for Jinja2 templates:
|
|||||||
|
|
||||||
### Default Request Body
|
### Default Request Body
|
||||||
|
|
||||||
If no body template is specified, and the HTTP content type is `application/json`, the request body will be populated with a JSON object containing the context data. For example, a newly created site might appear as follows:
|
If no body template is specified, the request body will be populated with a JSON object containing the context data. For example, a newly created site might appear as follows:
|
||||||
|
|
||||||
```no-highlight
|
```no-highlight
|
||||||
{
|
{
|
||||||
|
@ -58,7 +58,7 @@ class WebhookAdmin(admin.ModelAdmin):
|
|||||||
}),
|
}),
|
||||||
('HTTP Request', {
|
('HTTP Request', {
|
||||||
'fields': (
|
'fields': (
|
||||||
'http_method', 'payload_url', 'http_content_type', 'additional_headers', 'body_template', 'secret',
|
'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template', 'secret',
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
('SSL', {
|
('SSL', {
|
||||||
|
@ -106,10 +106,9 @@ class Webhook(models.Model):
|
|||||||
)
|
)
|
||||||
body_template = models.TextField(
|
body_template = models.TextField(
|
||||||
blank=True,
|
blank=True,
|
||||||
help_text='Jinja2 template for a custom request body. If blank, a JSON object or form data representing the '
|
help_text='Jinja2 template for a custom request body. If blank, a JSON object representing the change will be '
|
||||||
'change will be included. Available context data includes: <code>event</code>, '
|
'included. Available context data includes: <code>event</code>, <code>model</code>, '
|
||||||
'<code>timestamp</code>, <code>model</code>, <code>username</code>, <code>request_id</code>, and '
|
'<code>timestamp</code>, <code>username</code>, <code>request_id</code>, and <code>data</code>.'
|
||||||
'<code>data</code>.'
|
|
||||||
)
|
)
|
||||||
secret = models.CharField(
|
secret = models.CharField(
|
||||||
max_length=255,
|
max_length=255,
|
||||||
@ -165,12 +164,13 @@ class Webhook(models.Model):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
def render_body(self, context):
|
def render_body(self, context):
|
||||||
|
"""
|
||||||
|
Render the body template, if defined. Otherwise, jump the context as a JSON object.
|
||||||
|
"""
|
||||||
if self.body_template:
|
if self.body_template:
|
||||||
return render_jinja2(self.body_template, context)
|
return render_jinja2(self.body_template, context)
|
||||||
elif self.http_content_type == HTTP_CONTENT_TYPE_JSON:
|
|
||||||
return json.dumps(context, cls=JSONEncoder)
|
|
||||||
else:
|
else:
|
||||||
return context
|
return json.dumps(context, cls=JSONEncoder)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
Reference in New Issue
Block a user