-
Notifications
You must be signed in to change notification settings - Fork 2.7k
12591 config params admin #12904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
12591 config params admin #12904
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
fd6c6ba
12591 initial commit
arthanson c763e44
12591 detail view
arthanson f93702f
12591 add/edit view
arthanson 69b7fb7
12591 edit button
arthanson 718293d
12591 base views and forms
arthanson a1dd3c4
12591 form cleanup
arthanson 6da65e0
12591 form cleanup
arthanson 7ca0712
12591 form cleanup
arthanson c6391bf
12591 review changes
arthanson 03705a3
Merge branch 'feature' into 12591-config-params-admin
arthanson cd5f3cc
12591 move check for restrictedqueryset
arthanson 68341c5
12591 restore view
arthanson b9ad61d
12591 restore page styling
arthanson 2d7003d
12591 remove admin
arthanson 126c605
Remove edit view for ConfigRevision instances
jeremystretch f4aaee2
Order ConfigRevisions by creation time
jeremystretch 8658bfc
Correct permission name
jeremystretch 8565791
Use RestrictedQuerySet for ConfigRevision
jeremystretch b3d4ee6
Fix redirect URL
jeremystretch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,2 @@ | ||
from django.contrib import admin | ||
from django.shortcuts import get_object_or_404, redirect | ||
from django.template.response import TemplateResponse | ||
from django.urls import path, reverse | ||
from django.utils.html import format_html | ||
|
||
from netbox.config import get_config, PARAMS | ||
from .forms import ConfigRevisionForm | ||
from .models import ConfigRevision | ||
|
||
|
||
@admin.register(ConfigRevision) | ||
class ConfigRevisionAdmin(admin.ModelAdmin): | ||
fieldsets = [ | ||
('Rack Elevations', { | ||
'fields': ('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', 'RACK_ELEVATION_DEFAULT_UNIT_WIDTH'), | ||
}), | ||
('Power', { | ||
'fields': ('POWERFEED_DEFAULT_VOLTAGE', 'POWERFEED_DEFAULT_AMPERAGE', 'POWERFEED_DEFAULT_MAX_UTILIZATION') | ||
}), | ||
('IPAM', { | ||
'fields': ('ENFORCE_GLOBAL_UNIQUE', 'PREFER_IPV4'), | ||
}), | ||
('Security', { | ||
'fields': ('ALLOWED_URL_SCHEMES',), | ||
}), | ||
('Banners', { | ||
'fields': ('BANNER_LOGIN', 'BANNER_MAINTENANCE', 'BANNER_TOP', 'BANNER_BOTTOM'), | ||
'classes': ('monospace',), | ||
}), | ||
('Pagination', { | ||
'fields': ('PAGINATE_COUNT', 'MAX_PAGE_SIZE'), | ||
}), | ||
('Validation', { | ||
'fields': ('CUSTOM_VALIDATORS',), | ||
'classes': ('monospace',), | ||
}), | ||
('User Preferences', { | ||
'fields': ('DEFAULT_USER_PREFERENCES',), | ||
}), | ||
('Miscellaneous', { | ||
'fields': ('MAINTENANCE_MODE', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'JOB_RETENTION', 'MAPS_URL'), | ||
}), | ||
('Config Revision', { | ||
'fields': ('comment',), | ||
}) | ||
] | ||
form = ConfigRevisionForm | ||
list_display = ('id', 'is_active', 'created', 'comment', 'restore_link') | ||
ordering = ('-id',) | ||
readonly_fields = ('data',) | ||
|
||
def get_changeform_initial_data(self, request): | ||
""" | ||
Populate initial form data from the most recent ConfigRevision. | ||
""" | ||
latest_revision = ConfigRevision.objects.last() | ||
initial = latest_revision.data if latest_revision else {} | ||
initial.update(super().get_changeform_initial_data(request)) | ||
|
||
return initial | ||
|
||
# Permissions | ||
|
||
def has_add_permission(self, request): | ||
# Only superusers may modify the configuration. | ||
return request.user.is_superuser | ||
|
||
def has_change_permission(self, request, obj=None): | ||
# ConfigRevisions cannot be modified once created. | ||
return False | ||
|
||
def has_delete_permission(self, request, obj=None): | ||
# Only inactive ConfigRevisions may be deleted (must be superuser). | ||
return request.user.is_superuser and ( | ||
obj is None or not obj.is_active() | ||
) | ||
|
||
# List display methods | ||
|
||
def restore_link(self, obj): | ||
if obj.is_active(): | ||
return '' | ||
return format_html( | ||
'<a href="{url}" class="button">Restore</a>', | ||
url=reverse('admin:extras_configrevision_restore', args=(obj.pk,)) | ||
) | ||
restore_link.short_description = "Actions" | ||
|
||
# URLs | ||
|
||
def get_urls(self): | ||
urls = [ | ||
path('<int:pk>/restore/', self.admin_site.admin_view(self.restore), name='extras_configrevision_restore'), | ||
] | ||
|
||
return urls + super().get_urls() | ||
|
||
# Views | ||
|
||
def restore(self, request, pk): | ||
# Get the ConfigRevision being restored | ||
candidate_config = get_object_or_404(ConfigRevision, pk=pk) | ||
|
||
if request.method == 'POST': | ||
candidate_config.activate() | ||
self.message_user(request, f"Restored configuration revision #{pk}") | ||
|
||
return redirect(reverse('admin:extras_configrevision_changelist')) | ||
|
||
# Get the current ConfigRevision | ||
config_version = get_config().version | ||
current_config = ConfigRevision.objects.filter(pk=config_version).first() | ||
|
||
params = [] | ||
for param in PARAMS: | ||
params.append(( | ||
param.name, | ||
current_config.data.get(param.name, None), | ||
candidate_config.data.get(param.name, None) | ||
)) | ||
|
||
context = self.admin_site.each_context(request) | ||
context.update({ | ||
'object': candidate_config, | ||
'params': params, | ||
}) | ||
|
||
return TemplateResponse(request, 'admin/extras/configrevision/restore.html', context) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.