admin.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from django import forms
  2. from django.contrib import admin
  3. from netbox.admin import admin_site
  4. from utilities.forms import LaxURLField
  5. from .models import CustomField, CustomFieldChoice, CustomLink, Graph, ExportTemplate, TopologyMap, Webhook
  6. def order_content_types(field):
  7. """
  8. Order the list of available ContentTypes by application
  9. """
  10. queryset = field.queryset.order_by('app_label', 'model')
  11. field.choices = [(ct.pk, '{} > {}'.format(ct.app_label, ct.name)) for ct in queryset]
  12. #
  13. # Webhooks
  14. #
  15. class WebhookForm(forms.ModelForm):
  16. payload_url = LaxURLField(
  17. label='URL'
  18. )
  19. class Meta:
  20. model = Webhook
  21. exclude = []
  22. def __init__(self, *args, **kwargs):
  23. super().__init__(*args, **kwargs)
  24. if 'obj_type' in self.fields:
  25. order_content_types(self.fields['obj_type'])
  26. @admin.register(Webhook, site=admin_site)
  27. class WebhookAdmin(admin.ModelAdmin):
  28. list_display = [
  29. 'name', 'models', 'payload_url', 'http_content_type', 'enabled', 'type_create', 'type_update',
  30. 'type_delete', 'ssl_verification',
  31. ]
  32. form = WebhookForm
  33. def models(self, obj):
  34. return ', '.join([ct.name for ct in obj.obj_type.all()])
  35. #
  36. # Custom fields
  37. #
  38. class CustomFieldForm(forms.ModelForm):
  39. class Meta:
  40. model = CustomField
  41. exclude = []
  42. def __init__(self, *args, **kwargs):
  43. super().__init__(*args, **kwargs)
  44. order_content_types(self.fields['obj_type'])
  45. class CustomFieldChoiceAdmin(admin.TabularInline):
  46. model = CustomFieldChoice
  47. extra = 5
  48. @admin.register(CustomField, site=admin_site)
  49. class CustomFieldAdmin(admin.ModelAdmin):
  50. inlines = [CustomFieldChoiceAdmin]
  51. list_display = ['name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description']
  52. form = CustomFieldForm
  53. def models(self, obj):
  54. return ', '.join([ct.name for ct in obj.obj_type.all()])
  55. #
  56. # Custom links
  57. #
  58. class CustomLinkForm(forms.ModelForm):
  59. class Meta:
  60. model = CustomLink
  61. exclude = []
  62. help_texts = {
  63. 'text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. Links '
  64. 'which render as empty text will not be displayed.',
  65. 'url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
  66. }
  67. def __init__(self, *args, **kwargs):
  68. super().__init__(*args, **kwargs)
  69. # Format ContentType choices
  70. order_content_types(self.fields['content_type'])
  71. self.fields['content_type'].choices.insert(0, ('', '---------'))
  72. @admin.register(CustomLink, site=admin_site)
  73. class CustomLinkAdmin(admin.ModelAdmin):
  74. list_display = ['name', 'content_type', 'group_name', 'weight']
  75. form = CustomLinkForm
  76. #
  77. # Graphs
  78. #
  79. @admin.register(Graph, site=admin_site)
  80. class GraphAdmin(admin.ModelAdmin):
  81. list_display = ['name', 'type', 'weight', 'source']
  82. #
  83. # Export templates
  84. #
  85. class ExportTemplateForm(forms.ModelForm):
  86. class Meta:
  87. model = ExportTemplate
  88. exclude = []
  89. def __init__(self, *args, **kwargs):
  90. super().__init__(*args, **kwargs)
  91. # Format ContentType choices
  92. order_content_types(self.fields['content_type'])
  93. self.fields['content_type'].choices.insert(0, ('', '---------'))
  94. @admin.register(ExportTemplate, site=admin_site)
  95. class ExportTemplateAdmin(admin.ModelAdmin):
  96. list_display = ['name', 'content_type', 'description', 'mime_type', 'file_extension']
  97. form = ExportTemplateForm
  98. #
  99. # Topology maps
  100. #
  101. @admin.register(TopologyMap, site=admin_site)
  102. class TopologyMapAdmin(admin.ModelAdmin):
  103. list_display = ['name', 'slug', 'site']
  104. prepopulated_fields = {
  105. 'slug': ['name'],
  106. }