Coverage for netbox/preferences.py: 71%
24 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
1from django.conf import settings
2from django.utils.translation import gettext_lazy as _
4from netbox.registry import registry
5from users.preferences import UserPreference
6from utilities.choices import Choice
7from utilities.constants import CSV_DELIMITERS
8from utilities.paginator import EnhancedPaginator
11def get_page_lengths():
12 return [
13 Choice(v, str(v)) for v in EnhancedPaginator.default_page_lengths
14 ]
17def get_csv_delimiters():
18 choices = []
19 for k, v in CSV_DELIMITERS.items():
20 label = _(k.title())
21 if v.strip():
22 label = f'{label} ({v})'
23 choices.append(Choice(k, label))
24 return choices
27PREFERENCES = {
29 # User interface
30 'locale.language': UserPreference(
31 label=_('Language'),
32 choices=(
33 ('', _('Auto')),
34 *settings.LANGUAGES,
35 ),
36 description=_('Forces UI translation to the specified language'),
37 warning=(
38 _("Support for translation has been disabled locally")
39 if not settings.TRANSLATION_ENABLED
40 else ''
41 )
42 ),
43 'ui.copilot_enabled': UserPreference(
44 label=_('NetBox Copilot'),
45 choices=(
46 ('', _('Disabled')),
47 ('true', _('Enabled')),
48 ),
49 description=_('Enable the NetBox Copilot AI agent'),
50 default=False,
51 ),
52 'pagination.per_page': UserPreference(
53 label=_('Page length'),
54 choices=get_page_lengths(),
55 description=_('The default number of objects to display per page'),
56 coerce=lambda x: int(x)
57 ),
58 'pagination.placement': UserPreference(
59 label=_('Paginator placement'),
60 choices=(
61 ('bottom', _('Bottom')),
62 ('top', _('Top')),
63 ('both', _('Both')),
64 ),
65 default='bottom',
66 description=_('Where the paginator controls will be displayed relative to a table')
67 ),
68 'ui.tables.striping': UserPreference(
69 label=_('Striped table rows'),
70 choices=(
71 ('', _('Disabled')),
72 ('true', _('Enabled')),
73 ),
74 description=_('Render table rows with alternating colors to increase readability'),
75 ),
77 # Measurements
78 'ui.measurement_system': UserPreference(
79 label=_('Measurement units'),
80 choices=(
81 ('', _('Inherited')),
82 ('metric', _('Metric')),
83 ('imperial', _('Imperial')),
84 ),
85 default='',
86 description=_('Preferred unit system for displaying weight and distance measurements'),
87 ),
89 # Miscellaneous
90 'data_format': UserPreference(
91 label=_('Data format'),
92 choices=(
93 ('json', 'JSON'),
94 ('yaml', 'YAML'),
95 ),
96 description=_('The preferred syntax for displaying generic data within the UI')
97 ),
98 'csv_delimiter': UserPreference(
99 label=_('CSV delimiter'),
100 choices=get_csv_delimiters(),
101 default='comma',
102 description=_('The character used to separate fields in CSV data')
103 ),
105}
107# Register plugin preferences
108if registry['plugins']['preferences']: 108 ↛ 109line 108 didn't jump to line 109 because the condition on line 108 was never true
109 plugin_preferences = {}
111 for plugin_name, preferences in registry['plugins']['preferences'].items():
112 for name, userpreference in preferences.items():
113 PREFERENCES[f'plugins.{plugin_name}.{name}'] = userpreference
115 PREFERENCES.update(plugin_preferences)