2
0

admin.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from django.contrib import admin
  2. from django.shortcuts import get_object_or_404, redirect
  3. from django.template.response import TemplateResponse
  4. from django.urls import path, reverse
  5. from django.utils.html import format_html
  6. from netbox.config import get_config, PARAMS
  7. from .forms import ConfigRevisionForm
  8. from .models import ConfigRevision
  9. @admin.register(ConfigRevision)
  10. class ConfigRevisionAdmin(admin.ModelAdmin):
  11. fieldsets = [
  12. ('Rack Elevations', {
  13. 'fields': ('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', 'RACK_ELEVATION_DEFAULT_UNIT_WIDTH'),
  14. }),
  15. ('Power', {
  16. 'fields': ('POWERFEED_DEFAULT_VOLTAGE', 'POWERFEED_DEFAULT_AMPERAGE', 'POWERFEED_DEFAULT_MAX_UTILIZATION')
  17. }),
  18. ('IPAM', {
  19. 'fields': ('ENFORCE_GLOBAL_UNIQUE', 'PREFER_IPV4'),
  20. }),
  21. ('Security', {
  22. 'fields': ('ALLOWED_URL_SCHEMES',),
  23. }),
  24. ('Banners', {
  25. 'fields': ('BANNER_LOGIN', 'BANNER_MAINTENANCE', 'BANNER_TOP', 'BANNER_BOTTOM'),
  26. 'classes': ('monospace',),
  27. }),
  28. ('Pagination', {
  29. 'fields': ('PAGINATE_COUNT', 'MAX_PAGE_SIZE'),
  30. }),
  31. ('Validation', {
  32. 'fields': ('CUSTOM_VALIDATORS',),
  33. 'classes': ('monospace',),
  34. }),
  35. ('User Preferences', {
  36. 'fields': ('DEFAULT_USER_PREFERENCES',),
  37. }),
  38. ('Miscellaneous', {
  39. 'fields': ('MAINTENANCE_MODE', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'JOB_RETENTION', 'MAPS_URL'),
  40. }),
  41. ('Config Revision', {
  42. 'fields': ('comment',),
  43. })
  44. ]
  45. form = ConfigRevisionForm
  46. list_display = ('id', 'is_active', 'created', 'comment', 'restore_link')
  47. ordering = ('-id',)
  48. readonly_fields = ('data',)
  49. def get_changeform_initial_data(self, request):
  50. """
  51. Populate initial form data from the most recent ConfigRevision.
  52. """
  53. latest_revision = ConfigRevision.objects.last()
  54. initial = latest_revision.data if latest_revision else {}
  55. initial.update(super().get_changeform_initial_data(request))
  56. return initial
  57. # Permissions
  58. def has_add_permission(self, request):
  59. # Only superusers may modify the configuration.
  60. return request.user.is_superuser
  61. def has_change_permission(self, request, obj=None):
  62. # ConfigRevisions cannot be modified once created.
  63. return False
  64. def has_delete_permission(self, request, obj=None):
  65. # Only inactive ConfigRevisions may be deleted (must be superuser).
  66. return request.user.is_superuser and (
  67. obj is None or not obj.is_active()
  68. )
  69. # List display methods
  70. def restore_link(self, obj):
  71. if obj.is_active():
  72. return ''
  73. return format_html(
  74. '<a href="{url}" class="button">Restore</a>',
  75. url=reverse('admin:extras_configrevision_restore', args=(obj.pk,))
  76. )
  77. restore_link.short_description = "Actions"
  78. # URLs
  79. def get_urls(self):
  80. urls = [
  81. path('<int:pk>/restore/', self.admin_site.admin_view(self.restore), name='extras_configrevision_restore'),
  82. ]
  83. return urls + super().get_urls()
  84. # Views
  85. def restore(self, request, pk):
  86. # Get the ConfigRevision being restored
  87. candidate_config = get_object_or_404(ConfigRevision, pk=pk)
  88. if request.method == 'POST':
  89. candidate_config.activate()
  90. self.message_user(request, f"Restored configuration revision #{pk}")
  91. return redirect(reverse('admin:extras_configrevision_changelist'))
  92. # Get the current ConfigRevision
  93. config_version = get_config().version
  94. current_config = ConfigRevision.objects.filter(pk=config_version).first()
  95. params = []
  96. for param in PARAMS:
  97. params.append((
  98. param.name,
  99. current_config.data.get(param.name, None),
  100. candidate_config.data.get(param.name, None)
  101. ))
  102. context = self.admin_site.each_context(request)
  103. context.update({
  104. 'object': candidate_config,
  105. 'params': params,
  106. })
  107. return TemplateResponse(request, 'admin/extras/configrevision/restore.html', context)