Skip to content

Commit 433c1b0

Browse files
committed
feat: support group names as menu item display condition
1 parent 586e81f commit 433c1b0

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

docs/configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ JAZZMIN_SETTINGS = {
5050
# Links to put along the top menu
5151
"topmenu_links": [
5252

53-
# Url that gets reversed (Permissions can be added)
54-
{"name": "Home", "url": "admin:index", "permissions": ["auth.view_user"]},
53+
# Url that gets reversed (Permissions and Groups can be added)
54+
{"name": "Home", "url": "admin:index", "permissions": ["auth.view_user"], "groups": []},
5555

5656
# external url that opens in a new window (Permissions can be added)
5757
{"name": "Support", "url": "https://github.com/farridav/django-jazzmin/issues", "new_window": True},

jazzmin/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,23 @@ def make_menu(
162162
return []
163163

164164
model_permissions = get_view_permissions(user)
165+
user_groups = user.groups.all()
165166

166167
menu = []
167168
for link in links:
168169

169170
perm_matches = []
170-
for perm in link.get("permissions", []):
171+
permissions = link.get("permissions", [])
172+
for perm in permissions:
171173
perm_matches.append(user.has_perm(perm))
172174

173-
if not all(perm_matches):
175+
group_matches = []
176+
user_groups_names = [g.name for g in user_groups]
177+
groups = link.get("groups", [])
178+
for group in groups:
179+
group_matches.append(group in user_groups_names)
180+
181+
if not all(perm_matches) or not all(group_matches):
174182
continue
175183

176184
# Url links

tests/test_app/library/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@
147147
############
148148
# Links to put along the top menu
149149
"topmenu_links": [
150-
# Url that gets reversed (Permissions can be added)
151-
{"name": "Home", "url": "admin:index", "permissions": ["auth.view_user"]},
150+
# Url that gets reversed (Permissions and Groups can be added)
151+
{"name": "Home", "url": "admin:index", "permissions": ["auth.view_user"], "groups": []},
152152
# external url that opens in a new window (Permissions can be added)
153153
{"name": "Support", "url": "https://github.com/farridav/django-jazzmin/issues", "new_window": True},
154154
# model admin to link to (Permissions checked against model)

0 commit comments

Comments
 (0)