Enable plugins to define user preferences

This commit is contained in:
jeremystretch
2021-12-22 09:10:50 -05:00
parent 1eeac7f4f4
commit 1aafcf241f
8 changed files with 119 additions and 65 deletions
+1 -10
View File
@@ -2,10 +2,10 @@ from django import forms
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm as DjangoPasswordChangeForm
from django.utils.html import mark_safe
from netbox.preferences import PREFERENCES
from utilities.forms import BootstrapMixin, DateTimePicker, StaticSelect
from utilities.utils import flatten_dict
from .models import Token, UserConfig
from .preferences import PREFERENCES
class LoginForm(BootstrapMixin, AuthenticationForm):
@@ -44,15 +44,6 @@ class UserConfigForm(BootstrapMixin, forms.ModelForm, metaclass=UserConfigFormMe
class Meta:
model = UserConfig
fields = ()
fieldsets = (
('User Interface', (
'pagination.per_page',
'ui.colormode',
)),
('Miscellaneous', (
'data_format',
)),
)
def __init__(self, *args, instance=None, **kwargs):
-39
View File
@@ -1,12 +1,3 @@
from utilities.paginator import EnhancedPaginator
def get_page_lengths():
return [
(v, str(v)) for v in EnhancedPaginator.default_page_lengths
]
class UserPreference:
def __init__(self, label, choices, default=None, description='', coerce=lambda x: x):
@@ -15,33 +6,3 @@ class UserPreference:
self.default = default if default is not None else choices[0]
self.description = description
self.coerce = coerce
PREFERENCES = {
# User interface
'ui.colormode': UserPreference(
label='Color mode',
choices=(
('light', 'Light'),
('dark', 'Dark'),
),
default='light',
),
'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)
),
# Miscellaneous
'data_format': UserPreference(
label='Data format',
choices=(
('json', 'JSON'),
('yaml', 'YAML'),
),
),
}