admin.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from django.contrib import admin
  2. from .forms import ConfigRevisionForm
  3. from .models import ConfigRevision, JobResult
  4. @admin.register(ConfigRevision)
  5. class ConfigRevisionAdmin(admin.ModelAdmin):
  6. fieldsets = [
  7. # ('Authentication', {
  8. # 'fields': ('LOGIN_REQUIRED', 'LOGIN_PERSISTENCE', 'LOGIN_TIMEOUT'),
  9. # }),
  10. # ('Rack Elevations', {
  11. # 'fields': ('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', 'RACK_ELEVATION_DEFAULT_UNIT_WIDTH'),
  12. # }),
  13. ('IPAM', {
  14. 'fields': ('ENFORCE_GLOBAL_UNIQUE', 'PREFER_IPV4'),
  15. }),
  16. # ('Security', {
  17. # 'fields': (
  18. # 'ALLOWED_URL_SCHEMES', 'EXEMPT_VIEW_PERMISSIONS',
  19. # ),
  20. # }),
  21. ('Banners', {
  22. 'fields': ('BANNER_LOGIN', 'BANNER_TOP', 'BANNER_BOTTOM'),
  23. }),
  24. # ('Logging', {
  25. # 'fields': ('CHANGELOG_RETENTION',),
  26. # }),
  27. # ('Pagination', {
  28. # 'fields': ('MAX_PAGE_SIZE', 'PAGINATE_COUNT'),
  29. # }),
  30. # ('Miscellaneous', {
  31. # 'fields': ('GRAPHQL_ENABLED', 'METRICS_ENABLED', 'MAINTENANCE_MODE', 'MAPS_URL'),
  32. # }),
  33. ('Config Revision', {
  34. 'fields': ('comment',),
  35. })
  36. ]
  37. form = ConfigRevisionForm
  38. list_display = ('id', 'is_active', 'created', 'comment')
  39. ordering = ('-id',)
  40. readonly_fields = ('data',)
  41. def get_changeform_initial_data(self, request):
  42. """
  43. Populate initial form data from the most recent ConfigRevision.
  44. """
  45. latest_revision = ConfigRevision.objects.last()
  46. initial = latest_revision.data if latest_revision else {}
  47. initial.update(super().get_changeform_initial_data(request))
  48. return initial
  49. def has_add_permission(self, request):
  50. # Only superusers may modify the configuration.
  51. return request.user.is_superuser
  52. def has_change_permission(self, request, obj=None):
  53. # ConfigRevisions cannot be modified once created.
  54. return False
  55. def has_delete_permission(self, request, obj=None):
  56. # Only inactive ConfigRevisions may be deleted (must be superuser).
  57. return request.user.is_superuser and (
  58. obj is None or not obj.is_active()
  59. )
  60. #
  61. # Reports & scripts
  62. #
  63. @admin.register(JobResult)
  64. class JobResultAdmin(admin.ModelAdmin):
  65. list_display = [
  66. 'obj_type', 'name', 'created', 'completed', 'user', 'status',
  67. ]
  68. fields = [
  69. 'obj_type', 'name', 'created', 'completed', 'user', 'status', 'data', 'job_id'
  70. ]
  71. list_filter = [
  72. 'status',
  73. ]
  74. readonly_fields = fields
  75. def has_add_permission(self, request):
  76. return False