mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
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 <jstretch@netboxlabs.com>
This commit is contained in:
parent
eb3d423077
commit
d85cf9ee0d
@ -117,10 +117,14 @@ class BookmarkOrderingChoices(ChoiceSet):
|
|||||||
|
|
||||||
ORDERING_NEWEST = '-created'
|
ORDERING_NEWEST = '-created'
|
||||||
ORDERING_OLDEST = 'created'
|
ORDERING_OLDEST = 'created'
|
||||||
|
ORDERING_ALPHABETICAL_AZ = 'name'
|
||||||
|
ORDERING_ALPHABETICAL_ZA = '-name'
|
||||||
|
|
||||||
CHOICES = (
|
CHOICES = (
|
||||||
(ORDERING_NEWEST, _('Newest')),
|
(ORDERING_NEWEST, _('Newest')),
|
||||||
(ORDERING_OLDEST, _('Oldest')),
|
(ORDERING_OLDEST, _('Oldest')),
|
||||||
|
(ORDERING_ALPHABETICAL_AZ, _('Alphabetical (A-Z)')),
|
||||||
|
(ORDERING_ALPHABETICAL_ZA, _('Alphabetical (Z-A)')),
|
||||||
)
|
)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -381,11 +381,17 @@ class BookmarksWidget(DashboardWidget):
|
|||||||
if request.user.is_anonymous:
|
if request.user.is_anonymous:
|
||||||
bookmarks = list()
|
bookmarks = list()
|
||||||
else:
|
else:
|
||||||
bookmarks = Bookmark.objects.filter(user=request.user).order_by(self.config['order_by'])
|
user_bookmarks = Bookmark.objects.filter(user=request.user)
|
||||||
|
if self.config['order_by'] == BookmarkOrderingChoices.ORDERING_ALPHABETICAL_AZ:
|
||||||
|
bookmarks = sorted(user_bookmarks, key=lambda bookmark: bookmark.__str__().lower())
|
||||||
|
elif self.config['order_by'] == BookmarkOrderingChoices.ORDERING_ALPHABETICAL_ZA:
|
||||||
|
bookmarks = sorted(user_bookmarks, key=lambda bookmark: bookmark.__str__().lower(), reverse=True)
|
||||||
|
else:
|
||||||
|
bookmarks = user_bookmarks.order_by(self.config['order_by'])
|
||||||
if object_types := self.config.get('object_types'):
|
if object_types := self.config.get('object_types'):
|
||||||
models = get_models_from_content_types(object_types)
|
models = get_models_from_content_types(object_types)
|
||||||
conent_types = ObjectType.objects.get_for_models(*models).values()
|
content_types = ObjectType.objects.get_for_models(*models).values()
|
||||||
bookmarks = bookmarks.filter(object_type__in=conent_types)
|
bookmarks = bookmarks.filter(object_type__in=content_types)
|
||||||
if max_items := self.config.get('max_items'):
|
if max_items := self.config.get('max_items'):
|
||||||
bookmarks = bookmarks[:max_items]
|
bookmarks = bookmarks[:max_items]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user