mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-18 19:32:24 -06:00
Merge branch 'develop' into develop-2.8
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
|
||||
from netbox.admin import admin_site
|
||||
from utilities.forms import LaxURLField
|
||||
from .models import CustomField, CustomFieldChoice, CustomLink, Graph, ExportTemplate, ReportResult, Webhook
|
||||
from .reports import get_report
|
||||
@@ -35,7 +34,7 @@ class WebhookForm(forms.ModelForm):
|
||||
order_content_types(self.fields['obj_type'])
|
||||
|
||||
|
||||
@admin.register(Webhook, site=admin_site)
|
||||
@admin.register(Webhook)
|
||||
class WebhookAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
'name', 'models', 'payload_url', 'http_content_type', 'enabled', 'type_create', 'type_update', 'type_delete',
|
||||
@@ -93,7 +92,7 @@ class CustomFieldChoiceAdmin(admin.TabularInline):
|
||||
extra = 5
|
||||
|
||||
|
||||
@admin.register(CustomField, site=admin_site)
|
||||
@admin.register(CustomField)
|
||||
class CustomFieldAdmin(admin.ModelAdmin):
|
||||
inlines = [CustomFieldChoiceAdmin]
|
||||
list_display = [
|
||||
@@ -135,7 +134,7 @@ class CustomLinkForm(forms.ModelForm):
|
||||
self.fields['content_type'].choices.insert(0, ('', '---------'))
|
||||
|
||||
|
||||
@admin.register(CustomLink, site=admin_site)
|
||||
@admin.register(CustomLink)
|
||||
class CustomLinkAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
'name', 'content_type', 'group_name', 'weight',
|
||||
@@ -150,7 +149,7 @@ class CustomLinkAdmin(admin.ModelAdmin):
|
||||
# Graphs
|
||||
#
|
||||
|
||||
@admin.register(Graph, site=admin_site)
|
||||
@admin.register(Graph)
|
||||
class GraphAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
'name', 'type', 'weight', 'template_language', 'source',
|
||||
@@ -178,7 +177,7 @@ class ExportTemplateForm(forms.ModelForm):
|
||||
self.fields['content_type'].choices.insert(0, ('', '---------'))
|
||||
|
||||
|
||||
@admin.register(ExportTemplate, site=admin_site)
|
||||
@admin.register(ExportTemplate)
|
||||
class ExportTemplateAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
'name', 'content_type', 'description', 'mime_type', 'file_extension',
|
||||
@@ -193,7 +192,7 @@ class ExportTemplateAdmin(admin.ModelAdmin):
|
||||
# Reports
|
||||
#
|
||||
|
||||
@admin.register(ReportResult, site=admin_site)
|
||||
@admin.register(ReportResult)
|
||||
class ReportResultAdmin(admin.ModelAdmin):
|
||||
list_display = [
|
||||
'report', 'active', 'created', 'user', 'passing',
|
||||
|
||||
85
netbox/extras/management/commands/webhook_receiver.py
Normal file
85
netbox/extras/management/commands/webhook_receiver.py
Normal file
@@ -0,0 +1,85 @@
|
||||
import sys
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
|
||||
request_counter = 1
|
||||
|
||||
|
||||
class WebhookHandler(BaseHTTPRequestHandler):
|
||||
show_headers = True
|
||||
|
||||
def __getattr__(self, item):
|
||||
|
||||
# Return the same method for any type of HTTP request (GET, POST, etc.)
|
||||
if item.startswith('do_'):
|
||||
return self.do_ANY
|
||||
|
||||
raise AttributeError
|
||||
|
||||
def log_message(self, format_str, *args):
|
||||
global request_counter
|
||||
|
||||
print("[{}] {} {} {}".format(
|
||||
request_counter,
|
||||
self.date_time_string(),
|
||||
self.address_string(),
|
||||
format_str % args
|
||||
))
|
||||
|
||||
def do_ANY(self):
|
||||
global request_counter
|
||||
|
||||
# Send a 200 response regardless of the request content
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.wfile.write(b'Webhook received!\n')
|
||||
|
||||
request_counter += 1
|
||||
|
||||
# Print the request headers to stdout
|
||||
if self.show_headers:
|
||||
for k, v in self.headers.items():
|
||||
print('{}: {}'.format(k, v))
|
||||
print()
|
||||
|
||||
# Print the request body (if any)
|
||||
content_length = self.headers.get('Content-Length')
|
||||
if content_length is not None:
|
||||
body = self.rfile.read(int(content_length))
|
||||
print(body.decode('utf-8'))
|
||||
else:
|
||||
print('(No body)')
|
||||
|
||||
print('------------')
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Start a simple listener to display received HTTP requests"
|
||||
|
||||
default_port = 9000
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--port', type=int, default=self.default_port,
|
||||
help="Optional port number (default: {})".format(self.default_port)
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-headers", action='store_true', dest='no_headers',
|
||||
help="Hide HTTP request headers"
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
port = options['port']
|
||||
quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'
|
||||
|
||||
WebhookHandler.show_headers = not options['no_headers']
|
||||
|
||||
self.stdout.write('Listening on port http://localhost:{}. Stop with {}.'.format(port, quit_command))
|
||||
httpd = HTTPServer(('localhost', port), WebhookHandler)
|
||||
|
||||
try:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
self.stdout.write("\nExiting...")
|
||||
Reference in New Issue
Block a user