Skip to content

Commit d85cf9e

Browse files
16256 - Allow alphabetical ordering of bookmarks on dashboard (#16426)
* Added alphabetical ordering of bookmarks. * Addressed PR comments. * Rename choice constants & fix unrelated typo --------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent eb3d423 commit d85cf9e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

netbox/extras/choices.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,14 @@ class BookmarkOrderingChoices(ChoiceSet):
117117

118118
ORDERING_NEWEST = '-created'
119119
ORDERING_OLDEST = 'created'
120+
ORDERING_ALPHABETICAL_AZ = 'name'
121+
ORDERING_ALPHABETICAL_ZA = '-name'
120122

121123
CHOICES = (
122124
(ORDERING_NEWEST, _('Newest')),
123125
(ORDERING_OLDEST, _('Oldest')),
126+
(ORDERING_ALPHABETICAL_AZ, _('Alphabetical (A-Z)')),
127+
(ORDERING_ALPHABETICAL_ZA, _('Alphabetical (Z-A)')),
124128
)
125129

126130
#

netbox/extras/dashboard/widgets.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,17 @@ def render(self, request):
381381
if request.user.is_anonymous:
382382
bookmarks = list()
383383
else:
384-
bookmarks = Bookmark.objects.filter(user=request.user).order_by(self.config['order_by'])
384+
user_bookmarks = Bookmark.objects.filter(user=request.user)
385+
if self.config['order_by'] == BookmarkOrderingChoices.ORDERING_ALPHABETICAL_AZ:
386+
bookmarks = sorted(user_bookmarks, key=lambda bookmark: bookmark.__str__().lower())
387+
elif self.config['order_by'] == BookmarkOrderingChoices.ORDERING_ALPHABETICAL_ZA:
388+
bookmarks = sorted(user_bookmarks, key=lambda bookmark: bookmark.__str__().lower(), reverse=True)
389+
else:
390+
bookmarks = user_bookmarks.order_by(self.config['order_by'])
385391
if object_types := self.config.get('object_types'):
386392
models = get_models_from_content_types(object_types)
387-
conent_types = ObjectType.objects.get_for_models(*models).values()
388-
bookmarks = bookmarks.filter(object_type__in=conent_types)
393+
content_types = ObjectType.objects.get_for_models(*models).values()
394+
bookmarks = bookmarks.filter(object_type__in=content_types)
389395
if max_items := self.config.get('max_items'):
390396
bookmarks = bookmarks[:max_items]
391397

0 commit comments

Comments
 (0)