Просмотр исходного кода

Introduce DEFAULT_USER_PREFERENCES dynamic config setting

jeremystretch 4 лет назад
Родитель
Сommit
1eeac7f4f4

+ 16 - 0
docs/configuration/dynamic-settings.md

@@ -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
 
 Default: False

+ 3 - 0
netbox/extras/admin.py

@@ -33,6 +33,9 @@ class ConfigRevisionAdmin(admin.ModelAdmin):
         ('NAPALM', {
             'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'),
         }),
+        ('User Preferences', {
+            'fields': ('DEFAULT_USER_PREFERENCES',),
+        }),
         ('Miscellaneous', {
             'fields': ('MAINTENANCE_MODE', 'GRAPHQL_ENABLED', 'CHANGELOG_RETENTION', 'MAPS_URL'),
         }),

+ 9 - 0
netbox/netbox/config/parameters.py

@@ -131,6 +131,15 @@ PARAMS = (
         field=forms.JSONField
     ),
 
+    # User preferences
+    ConfigParam(
+        name='DEFAULT_USER_PREFERENCES',
+        label='Default preferences',
+        default={},
+        description="Default preferences for new users",
+        field=forms.JSONField
+    ),
+
     # Miscellaneous
     ConfigParam(
         name='MAINTENANCE_MODE',

+ 4 - 1
netbox/users/forms.py

@@ -1,5 +1,6 @@
 from django import forms
 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.utils import flatten_dict
@@ -22,10 +23,12 @@ class UserConfigFormMetaclass(forms.models.ModelFormMetaclass):
         # Emulate a declared field for each supported user preference
         preference_fields = {}
         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 = {
                 'label': preference.label,
                 'choices': preference.choices,
-                'help_text': preference.description,
+                'help_text': mark_safe(help_text),
                 'coerce': preference.coerce,
                 'required': False,
                 'widget': StaticSelect,

+ 3 - 1
netbox/users/models.py

@@ -10,6 +10,7 @@ from django.db.models.signals import post_save
 from django.dispatch import receiver
 from django.utils import timezone
 
+from netbox.config import get_config
 from netbox.models import BigIDModel
 from utilities.querysets import RestrictedQuerySet
 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.
     """
     if created:
-        UserConfig(user=instance).save()
+        config = get_config()
+        UserConfig(user=instance, data=config.DEFAULT_USER_PREFERENCES).save()
 
 
 #

+ 1 - 0
netbox/users/preferences.py

@@ -31,6 +31,7 @@ PREFERENCES = {
     'pagination.per_page': UserPreference(
         label='Page length',
         choices=get_page_lengths(),
+        description='The number of objects to display per page',
         coerce=lambda x: int(x)
     ),