Created
March 27, 2024 16:05
-
-
Save cnk/f5526c790e160c82fe2848c154d9dcb9 to your computer and use it in GitHub Desktop.
Registering custom menu item groups where the "is shown" needs to be evaluated for each request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalendarViewSetGroup(SnippetViewSetGroup): | |
""" | |
This class defines the Calendar menu, which is only displayed on www and on default sites from other servers. | |
""" | |
items = [EventSeries2ViewSet, EventSponsor2ViewSet, EventTagViewSet, EventSeason2ViewSet, AcademicTermViewSet] | |
menu_icon = 'calendar-alt' | |
menu_label = 'Calendar' | |
menu_name = 'calendar' | |
# This puts the Calendar menu just below News. | |
menu_order = 110 | |
def get_menu_item(self, order=None): | |
""" | |
This is where we define the top-level menu object, which lets us hide it based on some runtime criteria. | |
""" | |
class CustomSubmenu(SubmenuMenuItem): | |
def is_shown(self, request): | |
""" | |
Only show this menu on www to users with permission to see it, or on the default site to all users | |
(since only superusers and super admins can log in to default sites). | |
""" | |
if settings.SITE_TYPE == 'www': | |
return super().is_shown(request) | |
else: | |
return Site.find_for_request(request).is_default_site | |
return CustomSubmenu( | |
label=self.menu_label, | |
menu=Menu(items=self.get_submenu_items()), | |
name=self.menu_name, | |
icon_name=self.menu_icon, | |
order=order or self.menu_order, | |
) | |
def get_submenu_items(self): | |
""" | |
We override this method to customize the list of menu items in this menu, since it needs to contain more than | |
just the various ViewSetGroups in its `items` list. | |
""" | |
# Get the ViewSetGroups from `items`. | |
menu_items = [] | |
for registerable in self.registerables: | |
menu_items.append(registerable.get_menu_item()) | |
# Allow other code to register additional menu items through this hook. | |
for fn in hooks.get_hooks('register_calendar_admin_menu_item'): | |
menu_item = fn() | |
if menu_item: | |
menu_items.append(menu_item) | |
return menu_items | |
register_snippet(CalendarViewSetGroup) | |
@hooks.register('register_calendar_admin_menu_item') | |
def register_all_events_menu_item(): | |
class CustomMenuItem(MenuItem): | |
def is_shown(self, request): | |
""" | |
Show this menu item only on sites which have a MasterCalendarPage2 (which includes only Theme 7.0 sites), | |
and only to users with top-level sitemap permission. | |
""" | |
return ( | |
MasterCalendarPage2.objects.in_site(Site.find_for_request(request)).exists() | |
and current_user_has_sitemap_permission(request) | |
) | |
return CustomMenuItem( | |
'All Events', | |
reverse('master_calendar_admin:event_planner_listing'), | |
icon_name='calendar-alt', | |
order=100, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment