1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

167 lines
4.0 KiB
Python
Raw Normal View History

from django import forms
2016-03-01 11:23:03 -05:00
from django.contrib import admin
from netbox.admin import admin_site
from utilities.forms import LaxURLField
2019-08-08 21:33:20 -04:00
from .models import CustomField, CustomFieldChoice, CustomLink, Graph, ExportTemplate, Webhook
def order_content_types(field):
"""
Order the list of available ContentTypes by application
"""
queryset = field.queryset.order_by('app_label', 'model')
field.choices = [(ct.pk, '{} > {}'.format(ct.app_label, ct.name)) for ct in queryset]
#
# Webhooks
#
class WebhookForm(forms.ModelForm):
2018-07-16 13:54:50 -04:00
payload_url = LaxURLField(
label='URL'
)
class Meta:
model = Webhook
exclude = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if 'obj_type' in self.fields:
order_content_types(self.fields['obj_type'])
@admin.register(Webhook, site=admin_site)
class WebhookAdmin(admin.ModelAdmin):
list_display = [
'name', 'models', 'payload_url', 'http_content_type', 'enabled', 'type_create', 'type_update',
'type_delete', 'ssl_verification',
]
list_filter = [
'enabled', 'type_create', 'type_update', 'type_delete', 'obj_type',
]
form = WebhookForm
def models(self, obj):
return ', '.join([ct.name for ct in obj.obj_type.all()])
#
# Custom fields
#
class CustomFieldForm(forms.ModelForm):
class Meta:
model = CustomField
exclude = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
order_content_types(self.fields['obj_type'])
2016-08-12 17:20:01 -04:00
class CustomFieldChoiceAdmin(admin.TabularInline):
model = CustomFieldChoice
2016-08-18 14:23:28 -04:00
extra = 5
2016-08-12 17:20:01 -04:00
@admin.register(CustomField, site=admin_site)
2016-08-12 17:20:01 -04:00
class CustomFieldAdmin(admin.ModelAdmin):
inlines = [CustomFieldChoiceAdmin]
list_display = [
'name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description',
]
list_filter = [
'type', 'required', 'obj_type',
]
form = CustomFieldForm
2016-08-15 15:24:23 -04:00
def models(self, obj):
return ', '.join([ct.name for ct in obj.obj_type.all()])
2016-03-01 11:23:03 -05:00
2019-04-15 17:12:41 -04:00
#
# Custom links
#
2019-04-15 21:29:02 -04:00
class CustomLinkForm(forms.ModelForm):
class Meta:
model = CustomLink
exclude = []
widgets = {
'text': forms.Textarea,
'url': forms.Textarea,
}
2019-04-19 14:56:40 -04:00
help_texts = {
'text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. Links '
'which render as empty text will not be displayed.',
2019-04-19 14:56:40 -04:00
'url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
}
2019-04-15 21:29:02 -04:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Format ContentType choices
order_content_types(self.fields['content_type'])
self.fields['content_type'].choices.insert(0, ('', '---------'))
2019-04-15 17:12:41 -04:00
@admin.register(CustomLink, site=admin_site)
class CustomLinkAdmin(admin.ModelAdmin):
list_display = [
'name', 'content_type', 'group_name', 'weight',
]
list_filter = [
'content_type',
]
2019-04-15 21:29:02 -04:00
form = CustomLinkForm
2019-04-15 17:12:41 -04:00
#
# Graphs
#
@admin.register(Graph, site=admin_site)
2016-03-01 11:23:03 -05:00
class GraphAdmin(admin.ModelAdmin):
list_display = [
'name', 'type', 'weight', 'source',
]
list_filter = [
'type',
]
2016-03-01 11:23:03 -05:00
#
# Export templates
#
class ExportTemplateForm(forms.ModelForm):
class Meta:
model = ExportTemplate
exclude = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Format ContentType choices
order_content_types(self.fields['content_type'])
self.fields['content_type'].choices.insert(0, ('', '---------'))
@admin.register(ExportTemplate, site=admin_site)
2016-03-01 11:23:03 -05:00
class ExportTemplateAdmin(admin.ModelAdmin):
list_display = [
'name', 'content_type', 'description', 'mime_type', 'file_extension',
]
list_filter = [
'content_type',
]
form = ExportTemplateForm