Skip to content

Commit

Permalink
Fixes #2362: Implemented custom admin site to properly handle BASE_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Aug 16, 2018
1 parent cd2aee3 commit ff65f7f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
19 changes: 8 additions & 11 deletions netbox/extras/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
from django.contrib import admin
from django.utils.safestring import mark_safe

from netbox.admin import admin_site
from utilities.forms import LaxURLField
from .constants import OBJECTCHANGE_ACTION_CREATE, OBJECTCHANGE_ACTION_DELETE, OBJECTCHANGE_ACTION_UPDATE
from .models import (
ConfigContext, CustomField, CustomFieldChoice, Graph, ExportTemplate, ObjectChange, TopologyMap, UserAction,
Webhook,
)
from .models import CustomField, CustomFieldChoice, Graph, ExportTemplate, TopologyMap, UserAction, Webhook


def order_content_types(field):
Expand Down Expand Up @@ -39,7 +36,7 @@ def __init__(self, *args, **kwargs):
order_content_types(self.fields['obj_type'])


@admin.register(Webhook)
@admin.register(Webhook, site=admin_site)
class WebhookAdmin(admin.ModelAdmin):
list_display = [
'name', 'models', 'payload_url', 'http_content_type', 'enabled', 'type_create', 'type_update',
Expand Down Expand Up @@ -72,7 +69,7 @@ class CustomFieldChoiceAdmin(admin.TabularInline):
extra = 5


@admin.register(CustomField)
@admin.register(CustomField, site=admin_site)
class CustomFieldAdmin(admin.ModelAdmin):
inlines = [CustomFieldChoiceAdmin]
list_display = ['name', 'models', 'type', 'required', 'filter_logic', 'default', 'weight', 'description']
Expand All @@ -86,7 +83,7 @@ def models(self, obj):
# Graphs
#

@admin.register(Graph)
@admin.register(Graph, site=admin_site)
class GraphAdmin(admin.ModelAdmin):
list_display = ['name', 'type', 'weight', 'source']

Expand All @@ -109,7 +106,7 @@ def __init__(self, *args, **kwargs):
self.fields['content_type'].choices.insert(0, ('', '---------'))


@admin.register(ExportTemplate)
@admin.register(ExportTemplate, site=admin_site)
class ExportTemplateAdmin(admin.ModelAdmin):
list_display = ['name', 'content_type', 'description', 'mime_type', 'file_extension']
form = ExportTemplateForm
Expand All @@ -119,7 +116,7 @@ class ExportTemplateAdmin(admin.ModelAdmin):
# Topology maps
#

@admin.register(TopologyMap)
@admin.register(TopologyMap, site=admin_site)
class TopologyMapAdmin(admin.ModelAdmin):
list_display = ['name', 'slug', 'site']
prepopulated_fields = {
Expand All @@ -131,7 +128,7 @@ class TopologyMapAdmin(admin.ModelAdmin):
# User actions
#

@admin.register(UserAction)
@admin.register(UserAction, site=admin_site)
class UserActionAdmin(admin.ModelAdmin):
actions = None
list_display = ['user', 'action', 'content_type', 'object_id', '_message']
Expand Down
30 changes: 30 additions & 0 deletions netbox/netbox/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.conf import settings
from django.contrib.admin import AdminSite
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from taggit.admin import TagAdmin
from taggit.models import Tag


class NetBoxAdminSite(AdminSite):
"""
Custom admin site
"""
site_header = 'NetBox Administration'
site_title = 'NetBox'
site_url = '/{}'.format(settings.BASE_PATH)


admin_site = NetBoxAdminSite(name='admin')

# Register external models
admin_site.register(Group, GroupAdmin)
admin_site.register(User, UserAdmin)
admin_site.register(Tag, TagAdmin)

# Modify the template to include an RQ link if django_rq is installed (see RQ_SHOW_ADMIN_LINK)
try:
import django_rq
admin_site.index_template = 'django_rq/index.html'
except ImportError:
pass
1 change: 0 additions & 1 deletion netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@
'DEFAULT_TIMEOUT': REDIS_DEFAULT_TIMEOUT,
}
}
RQ_SHOW_ADMIN_LINK = True

# drf_yasg settings for Swagger
SWAGGER_SETTINGS = {
Expand Down
5 changes: 2 additions & 3 deletions netbox/netbox/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

from netbox.views import APIRootView, HomeView, SearchView
from users.views import LoginView, LogoutView
from .admin import admin_site

schema_view = get_schema_view(
openapi.Info(
Expand Down Expand Up @@ -60,7 +60,7 @@
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),

# Admin
url(r'^admin/', admin.site.urls),
url(r'^admin/', admin_site.urls),

]

Expand All @@ -69,7 +69,6 @@
url(r'^admin/webhook-backend-status/', include('django_rq.urls')),
]


if settings.DEBUG:
import debug_toolbar
_patterns += [
Expand Down
3 changes: 2 additions & 1 deletion netbox/secrets/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from django.contrib import admin, messages
from django.shortcuts import redirect, render

from netbox.admin import admin_site
from .forms import ActivateUserKeyForm
from .models import UserKey


@admin.register(UserKey)
@admin.register(UserKey, site=admin_site)
class UserKeyAdmin(admin.ModelAdmin):
actions = ['activate_selected']
list_display = ['user', 'is_filled', 'is_active', 'created']
Expand Down
3 changes: 2 additions & 1 deletion netbox/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django import forms
from django.contrib import admin

from netbox.admin import admin_site
from .models import Token


Expand All @@ -14,7 +15,7 @@ class Meta:
model = Token


@admin.register(Token)
@admin.register(Token, site=admin_site)
class TokenAdmin(admin.ModelAdmin):
form = TokenAdminForm
list_display = ['key', 'user', 'created', 'expires', 'write_enabled', 'description']

0 comments on commit ff65f7f

Please sign in to comment.