mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Introduce DEFAULT_USER_PREFERENCES dynamic config setting
This commit is contained in:
@ -66,6 +66,22 @@ CUSTOM_VALIDATORS = {
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## DEFAULT_USER_PREFERENCES
|
||||||
|
|
||||||
|
This is a dictionary defining the default preferences to be set for newly-created user accounts. For example, to set the default page size for all users to 100, define the following:
|
||||||
|
|
||||||
|
```python
|
||||||
|
DEFAULT_USER_PREFERENCES = {
|
||||||
|
"pagination": {
|
||||||
|
"per_page": 100
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For a complete list of available preferences, log into NetBox and navigate to `/user/preferences/`. A period in a preference name indicates a level of nesting in the JSON data. The example above maps to `pagination.per_page`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## ENFORCE_GLOBAL_UNIQUE
|
## ENFORCE_GLOBAL_UNIQUE
|
||||||
|
|
||||||
Default: False
|
Default: False
|
||||||
|
@ -33,6 +33,9 @@ class ConfigRevisionAdmin(admin.ModelAdmin):
|
|||||||
('NAPALM', {
|
('NAPALM', {
|
||||||
'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'),
|
'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'),
|
||||||
}),
|
}),
|
||||||
|
('User Preferences', {
|
||||||
|
'fields': ('DEFAULT_USER_PREFERENCES',),
|
||||||
|
}),
|
||||||
('Miscellaneous', {
|
('Miscellaneous', {
|
||||||
'fields': ('MAINTENANCE_MODE', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'MAPS_URL'),
|
'fields': ('MAINTENANCE_MODE', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'MAPS_URL'),
|
||||||
}),
|
}),
|
||||||
|
@ -131,6 +131,15 @@ PARAMS = (
|
|||||||
field=forms.JSONField
|
field=forms.JSONField
|
||||||
),
|
),
|
||||||
|
|
||||||
|
# User preferences
|
||||||
|
ConfigParam(
|
||||||
|
name='DEFAULT_USER_PREFERENCES',
|
||||||
|
label='Default preferences',
|
||||||
|
default={},
|
||||||
|
description="Default preferences for new users",
|
||||||
|
field=forms.JSONField
|
||||||
|
),
|
||||||
|
|
||||||
# Miscellaneous
|
# Miscellaneous
|
||||||
ConfigParam(
|
ConfigParam(
|
||||||
name='MAINTENANCE_MODE',
|
name='MAINTENANCE_MODE',
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm as DjangoPasswordChangeForm
|
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm as DjangoPasswordChangeForm
|
||||||
|
from django.utils.html import mark_safe
|
||||||
|
|
||||||
from utilities.forms import BootstrapMixin, DateTimePicker, StaticSelect
|
from utilities.forms import BootstrapMixin, DateTimePicker, StaticSelect
|
||||||
from utilities.utils import flatten_dict
|
from utilities.utils import flatten_dict
|
||||||
@ -22,10 +23,12 @@ class UserConfigFormMetaclass(forms.models.ModelFormMetaclass):
|
|||||||
# Emulate a declared field for each supported user preference
|
# Emulate a declared field for each supported user preference
|
||||||
preference_fields = {}
|
preference_fields = {}
|
||||||
for field_name, preference in PREFERENCES.items():
|
for field_name, preference in PREFERENCES.items():
|
||||||
|
description = f'{preference.description}<br />' if preference.description else ''
|
||||||
|
help_text = f'{description}<code>{field_name}</code>'
|
||||||
field_kwargs = {
|
field_kwargs = {
|
||||||
'label': preference.label,
|
'label': preference.label,
|
||||||
'choices': preference.choices,
|
'choices': preference.choices,
|
||||||
'help_text': preference.description,
|
'help_text': mark_safe(help_text),
|
||||||
'coerce': preference.coerce,
|
'coerce': preference.coerce,
|
||||||
'required': False,
|
'required': False,
|
||||||
'widget': StaticSelect,
|
'widget': StaticSelect,
|
||||||
|
@ -10,6 +10,7 @@ from django.db.models.signals import post_save
|
|||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from netbox.config import get_config
|
||||||
from netbox.models import BigIDModel
|
from netbox.models import BigIDModel
|
||||||
from utilities.querysets import RestrictedQuerySet
|
from utilities.querysets import RestrictedQuerySet
|
||||||
from utilities.utils import flatten_dict
|
from utilities.utils import flatten_dict
|
||||||
@ -166,7 +167,8 @@ def create_userconfig(instance, created, **kwargs):
|
|||||||
Automatically create a new UserConfig when a new User is created.
|
Automatically create a new UserConfig when a new User is created.
|
||||||
"""
|
"""
|
||||||
if created:
|
if created:
|
||||||
UserConfig(user=instance).save()
|
config = get_config()
|
||||||
|
UserConfig(user=instance, data=config.DEFAULT_USER_PREFERENCES).save()
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -31,6 +31,7 @@ PREFERENCES = {
|
|||||||
'pagination.per_page': UserPreference(
|
'pagination.per_page': UserPreference(
|
||||||
label='Page length',
|
label='Page length',
|
||||||
choices=get_page_lengths(),
|
choices=get_page_lengths(),
|
||||||
|
description='The number of objects to display per page',
|
||||||
coerce=lambda x: int(x)
|
coerce=lambda x: int(x)
|
||||||
),
|
),
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user