Skip to content

Commit

Permalink
Closes #15942: Refactor settings_and_registry() context processor
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed May 3, 2024
1 parent f8cf2a3 commit 4150442
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
44 changes: 38 additions & 6 deletions netbox/netbox/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
from django.conf import settings as django_settings

from netbox.config import get_config
from netbox.registry import registry
from netbox.registry import registry as registry_

__all__ = (
'config',
'preferences',
'registry',
'settings',
)

def settings_and_registry(request):

def config(request):
"""
Expose Django settings and NetBox registry stores in the template context. Example: {{ settings.DEBUG }}
Adds NetBox configuration parameters to the template context. Example: {{ config.BANNER_LOGIN }}
"""
user_preferences = request.user.config if request.user.is_authenticated else {}
return {
'settings': django_settings,
'config': get_config(),
'registry': registry,
}


def preferences(request):
"""
Adds preferences for the current user (if authenticated) to the template context.
Example: {{ preferences|get_key:"pagination.placement" }}
"""
user_preferences = request.user.config if request.user.is_authenticated else {}
return {
'preferences': user_preferences,
'htmx_navigation': user_preferences.get('ui.htmx_navigation', False) == 'true'
}


def registry(request):
"""
Adds NetBox registry items to the template context. Example: {{ registry.models.core }}
"""
return {
'registry': registry_,
}


def settings(request):
"""
Adds Django settings to the template context. Example: {{ settings.DEBUG }}
"""
return {
'settings': django_settings,
}
5 changes: 4 additions & 1 deletion netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ def _setting(name, default=None):
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'netbox.context_processors.settings_and_registry',
'netbox.context_processors.settings',
'netbox.context_processors.config',
'netbox.context_processors.registry',
'netbox.context_processors.preferences',
],
},
},
Expand Down

0 comments on commit 4150442

Please sign in to comment.