Skip to content

Commit 82e7cf7

Browse files
author
David Farrington
committed
Control use of icons
1 parent 208642a commit 82e7cf7

File tree

5 files changed

+39
-46
lines changed

5 files changed

+39
-46
lines changed

docs/configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ Add a template for your model on your main template directory,
289289
e.g [app/templates/admin/app_name/model_name/submit_line.html](https://github.com/farridav/django-jazzmin/tree/master/tests/test_app/library/books/templates/admin/loans/bookloan/submit_line.html)
290290

291291
```djangotemplate
292-
{# extends "admin/submit_line.html" #}
292+
{#% extends "admin/submit_line.html" %#}
293293
294-
{% block extra-actions %}
294+
{#% block extra-actions %#}
295295
296296
{# For a simple link #}
297297
<div class="form-group">
@@ -302,7 +302,7 @@ e.g [app/templates/admin/app_name/model_name/submit_line.html](https://github.co
302302
<div class="form-group">
303303
<input type="submit" class="btn btn-outline-info form-control" value="SomeAction" name="_your_action">
304304
</div>
305-
{% endblock %}
305+
{#% endblock %#}
306306
```
307307

308308
If you are adding a button that needs processing with the form, e.g (Save and send) you will need to add the

jazzmin/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
############
3131
# Links to put along the nav bar
3232
"topmenu_links": [],
33+
# Whether or not we use icons on the top menu
34+
"topmenu_icons": False,
3335
#############
3436
# User Menu #
3537
#############

jazzmin/templatetags/jazzmin.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
get_admin_url,
3232
make_menu,
3333
has_fieldsets_check,
34-
regroup_available_apps,
34+
regroup_apps,
3535
)
3636

3737
User = get_user_model()
@@ -64,7 +64,7 @@ def get_side_menu(context: Context, using: str = "available_apps") -> List[Dict]
6464

6565
# If we are using custom grouping, overwrite available_apps based on our grouping
6666
if options.get("custom_menu") and options["custom_menu"]:
67-
available_apps = regroup_available_apps(available_apps, options["custom_menu"])
67+
available_apps = regroup_apps(available_apps, options["custom_menu"])
6868

6969
for app in available_apps:
7070
app_label = app["app_label"].lower()
@@ -88,13 +88,18 @@ def get_side_menu(context: Context, using: str = "available_apps") -> List[Dict]
8888

8989
custom_link_names = [x.get("name", "").lower() for x in app_custom_links]
9090
model_ordering = list(
91-
filter(lambda x: x.lower().startswith("{}.".format(app_label)) or x.lower() in custom_link_names, ordering,)
91+
filter(
92+
lambda x: x.lower().startswith("{}.".format(app_label)) or x.lower() in custom_link_names,
93+
ordering,
94+
)
9295
)
9396

9497
if len(menu_items):
9598
if model_ordering:
9699
menu_items = order_with_respect_to(
97-
menu_items, model_ordering, getter=lambda x: x.get("model_str", x.get("name", "").lower()),
100+
menu_items,
101+
model_ordering,
102+
getter=lambda x: x.get("model_str", x.get("name", "").lower()),
98103
)
99104
app["models"] = menu_items
100105
menu.append(app)
@@ -420,7 +425,8 @@ def deleted(x: str) -> Dict:
420425

421426
elif "changed" in sub_message:
422427
sub_message["changed"]["fields"] = get_text_list(
423-
[gettext(field_name) for field_name in sub_message["changed"]["fields"]], gettext("and"),
428+
[gettext(field_name) for field_name in sub_message["changed"]["fields"]],
429+
gettext("and"),
424430
)
425431
if "name" in sub_message["changed"]:
426432
sub_message["changed"]["name"] = gettext(sub_message["changed"]["name"])
@@ -443,7 +449,7 @@ def style_bold_first_word(message: str) -> SafeText:
443449
message_words = escape(message).split()
444450

445451
if not len(message_words):
446-
return ""
452+
return mark_safe("")
447453

448454
message_words[0] = "<strong>{}</strong>".format(message_words[0])
449455

jazzmin/utils.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import logging
2-
from typing import List, Union, Dict, Set, Callable
2+
from typing import List, Union, Dict, Set, Callable, Any
33
from urllib.parse import urlencode
44

55
from django.apps import apps
66
from django.contrib.admin import ListFilter
77
from django.contrib.admin.helpers import AdminForm
88
from django.contrib.auth.models import AbstractUser
9-
from django.db.models.base import ModelBase
9+
from django.db.models.base import ModelBase, Model
1010
from django.db.models.options import Options
1111
from django.utils.translation import gettext
1212

@@ -33,16 +33,13 @@ def order_with_respect_to(original: List, reference: List, getter: Callable = la
3333
return [y for x, y in sorted(zip(ranking, original), key=lambda x: x[0])]
3434

3535

36-
def get_admin_url(
37-
instance: Union[str, ModelBase], admin_site: str = "admin", from_app: bool = False, **kwargs: str
38-
) -> str:
36+
def get_admin_url(instance: Any, admin_site: str = "admin", from_app: bool = False, **kwargs: str) -> str:
3937
"""
4038
Return the admin URL for the given instance, model class or <app>.<model> string
4139
"""
4240
url = "#"
4341

4442
try:
45-
4643
if type(instance) == str:
4744
app_label, model_name = instance.split(".")
4845
model_name = model_name.lower()
@@ -150,16 +147,7 @@ def get_view_permissions(user: AbstractUser) -> Set[str]:
150147
lower_perms = []
151148
for perm in perms:
152149
app, perm_codename = perm.split(".")
153-
<<<<<<< HEAD
154150
lower_perms.append("{app}.{perm_codename}".format(app=app, perm_codename=perm_codename.lower()))
155-
=======
156-
lower_perms.append(
157-
"{app}.{perm_codename}".format(
158-
app=app,
159-
perm_codename=perm_codename.lower(),
160-
)
161-
)
162-
>>>>>>> 3ec6d11 (test)
163151
return {x.replace("view_", "") for x in lower_perms if "view" in x or "change" in x}
164152

165153

@@ -176,12 +164,7 @@ def make_menu(
176164

177165
menu = []
178166
for link in links:
179-
180-
perm_matches = []
181-
for perm in link.get("permissions", []):
182-
perm_matches.append(user.has_perm(perm))
183-
184-
if not all(perm_matches):
167+
if not all([user.has_perm(perm) for perm in link.get("permissions", [])]):
185168
continue
186169

187170
# Url links
@@ -217,16 +200,7 @@ def make_menu(
217200
# App links
218201
elif "app" in link and allow_appmenus:
219202
children = [
220-
<<<<<<< HEAD
221203
{"name": child.get("verbose_name", child["name"]), "url": child["url"], "children": None}
222-
=======
223-
{
224-
"name": child.get("verbose_name", child["name"]),
225-
"url": child["url"],
226-
"children": None,
227-
"icon": options["icons"].get(child["model"].lower()),
228-
}
229-
>>>>>>> 3ec6d11 (test)
230204
for child in get_app_admin_urls(link["app"], admin_site=admin_site)
231205
if child["model"] in model_permissions
232206
]
@@ -252,7 +226,7 @@ def has_fieldsets_check(adminform: AdminForm) -> bool:
252226
return True
253227

254228

255-
def regroup_available_apps(available_apps: List[Dict], grouping: Dict[str, List[str]]) -> List[Dict]:
229+
def regroup_apps(available_apps: List[Dict], grouping: Dict[str, List[str]]) -> List[Dict]:
256230
# Make a list of all apps, and all models, keyed on app name or model name
257231
all_models, all_apps = {}, {}
258232
for app in available_apps:

tests/test_app/library/settings.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,25 @@
7171

7272
DATABASES = {
7373
"default": dj_database_url.config(
74-
env="DATABASE_URL", conn_max_age=500, default="sqlite:///{}".format(os.path.join(BASE_DIR, "db.sqlite3")),
74+
env="DATABASE_URL",
75+
conn_max_age=500,
76+
default="sqlite:///{}".format(os.path.join(BASE_DIR, "db.sqlite3")),
7577
)
7678
}
7779

7880
AUTH_PASSWORD_VALIDATORS = [
79-
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",},
80-
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
81-
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
82-
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
81+
{
82+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
83+
},
84+
{
85+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
86+
},
87+
{
88+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
89+
},
90+
{
91+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
92+
},
8393
]
8494

8595
LANGUAGE_CODE = "en"
@@ -146,6 +156,7 @@
146156
{"app": "books"},
147157
{"app": "loans"},
148158
],
159+
# Whether or not we use icons on the top menu
149160
"topmenu_icons": True,
150161
#############
151162
# User Menu #
@@ -179,7 +190,7 @@
179190
}
180191
]
181192
},
182-
# Dont generate a menu based off installed apps, instead, craft one using this app/arbitrary name -> model mapping
193+
# Dont generate a side menu from installed apps, instead, craft one using this app/arbitrary name -> model mapping
183194
"custom_menu": {},
184195
# Custom icons for side menu apps/models See https://fontawesome.com/icons?d=gallery&m=free
185196
# for a list of icon classes

0 commit comments

Comments
 (0)