Skip to content

Commit

Permalink
Format files using ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Sami El Achi committed Mar 30, 2024
1 parent 5d2c12a commit 0f821fd
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 40 deletions.
9 changes: 5 additions & 4 deletions jazzmin/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
import logging
from typing import Dict, Any
from typing import Any, Dict

from django.conf import settings
from django.templatetags.static import static
Expand Down Expand Up @@ -61,7 +61,8 @@
"custom_links": {},
# Custom icons for side menu apps/models See the link below
# https://fontawesome.com/icons?d=gallery&m=free&v=5.0.0,5.0.1,5.0.10,5.0.11,5.0.12,5.0.13,5.0.2,5.0.3,5.0.4,5.0.5,5.0.6,5.0.7,5.0.8,5.0.9,5.1.0,
# 5.1.1,5.2.0,5.3.0,5.3.1,5.4.0,5.4.1,5.4.2,5.13.0,5.12.0,5.11.2,5.11.1,5.10.0,5.9.0,5.8.2,5.8.1,5.7.2,5.7.1,5.7.0,5.6.3,5.5.0,5.4.2
# 5.1.1,5.2.0,5.3.0,5.3.1,5.4.0,5.4.1,5.4.2,5.13.0,5.12.0,
# 5.11.2,5.11.1,5.10.0,5.9.0,5.8.2,5.8.1,5.7.2,5.7.1,5.7.0,5.6.3,5.5.0,5.4.2
# for the full list of 5.13.0 free icon classes
"icons": {"auth": "fas fa-users-cog", "auth.user": "fas fa-user", "auth.Group": "fas fa-users"},
# Icons that are used when one is not manually specified
Expand Down Expand Up @@ -229,11 +230,11 @@ def get_settings() -> Dict:
jazzmin_settings["search_models_parsed"].append(jazzmin_search_model)

# Deal with single strings in hide_apps/hide_models and make sure we lower case 'em
if type(jazzmin_settings["hide_apps"]) == str:
if isinstance(jazzmin_settings["hide_apps"], str):
jazzmin_settings["hide_apps"] = [jazzmin_settings["hide_apps"]]
jazzmin_settings["hide_apps"] = [x.lower() for x in jazzmin_settings["hide_apps"]]

if type(jazzmin_settings["hide_models"]) == str:
if isinstance(jazzmin_settings["hide_models"], str):
jazzmin_settings["hide_models"] = [jazzmin_settings["hide_models"]]
jazzmin_settings["hide_models"] = [x.lower() for x in jazzmin_settings["hide_models"]]

Expand Down
42 changes: 23 additions & 19 deletions jazzmin/templatetags/jazzmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@

from .. import version
from ..settings import CHANGEFORM_TEMPLATES, get_settings, get_ui_tweaks
from ..utils import get_admin_url, get_filter_id, has_fieldsets_check, make_menu, order_with_respect_to
from ..utils import (
get_admin_url,
get_filter_id,
has_fieldsets_check,
make_menu,
order_with_respect_to,
)

User = get_user_model()
register = Library()
Expand Down Expand Up @@ -118,7 +124,13 @@ def get_user_menu(user: AbstractUser, admin_site: str = "admin") -> List[Dict]:
Produce the menu for the user dropdown
"""
options = get_settings()
return make_menu(user, options.get("usermenu_links", []), options, allow_appmenus=False, admin_site=admin_site)
return make_menu(
user,
options.get("usermenu_links", []),
options,
allow_appmenus=False,
admin_site=admin_site,
)


@register.simple_tag
Expand Down Expand Up @@ -179,7 +191,7 @@ def get_user_avatar(user: AbstractUser) -> str:
# If we find the property directly on the user model (imagefield or URLfield)
avatar_field = getattr(user, avatar_field_name, None)
if avatar_field:
if type(avatar_field) == str:
if isinstance(avatar_field, str):
return avatar_field
elif hasattr(avatar_field, "url"):
return avatar_field.url
Expand Down Expand Up @@ -208,18 +220,14 @@ def jazzmin_paginator_number(change_list: ChangeList, i: int) -> SafeText:
<li class="page-item previous {disabled}">
<a class="page-link" href="{link}" data-dt-idx="0" tabindex="0">«</a>
</li>
""".format(
link=link, disabled="disabled" if link == "#" else ""
)
""".format(link=link, disabled="disabled" if link == "#" else "")

if current_page:
html_str += """
<li class="page-item active">
<a class="page-link" href="javascript:void(0);" data-dt-idx="3" tabindex="0">{num}</a>
</li>
""".format(
num=i
)
""".format(num=i)
elif spacer:
html_str += """
<li class="page-item">
Expand All @@ -233,19 +241,15 @@ def jazzmin_paginator_number(change_list: ChangeList, i: int) -> SafeText:
<li class="page-item">
<a href="{query_string}" class="page-link {end}" data-dt-idx="3" tabindex="0">{num}</a>
</li>
""".format(
num=i, query_string=query_string, end=end
)
""".format(num=i, query_string=query_string, end=end)

if end:
link = change_list.get_query_string({PAGE_VAR: change_list.page_num + 1}) if change_list.page_num < i else "#"
html_str += """
<li class="page-item next {disabled}">
<a class="page-link" href="{link}" data-dt-idx="7" tabindex="0">»</a>
</li>
""".format(
link=link, disabled="disabled" if link == "#" else ""
)
""".format(link=link, disabled="disabled" if link == "#" else "")

return format_html(html_str)

Expand All @@ -256,7 +260,7 @@ def admin_extra_filters(cl: ChangeList) -> Dict:
Return the dict of used filters which is not included in list_filters form
"""
used_parameters = list(itertools.chain(*(s.used_parameters.keys() for s in cl.filter_specs)))
return dict((k, v) for k, v in cl.params.items() if k not in used_parameters)
return {k: v for k, v in cl.params.items() if k not in used_parameters}


@register.simple_tag
Expand Down Expand Up @@ -330,7 +334,7 @@ def get_sections(
"""
Get and sort all of the sections that need rendering out in a change form
"""
fieldsets = [x for x in admin_form]
fieldsets = list(admin_form)

# Make inlines behave like formsets
for fieldset in inline_admin_formsets:
Expand Down Expand Up @@ -457,7 +461,7 @@ def app_is_installed(app: str) -> bool:


@register.simple_tag
def action_message_to_list(action: LogEntry) -> List[Dict]:
def action_message_to_list(action: LogEntry) -> List[Dict]: # noqa: C901
"""
Retrieves a formatted list with all actions taken by a user given a log entry object
"""
Expand Down Expand Up @@ -528,7 +532,7 @@ def style_bold_first_word(message: str) -> SafeText:

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

message = " ".join([word for word in message_words])
message = " ".join(list(message_words))

return mark_safe(message)

Expand Down
8 changes: 3 additions & 5 deletions jazzmin/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import logging
from typing import List, Union, Dict, Set, Callable, Any
from typing import Any, Callable, Dict, List, Set, Union
from urllib.parse import urlencode

from django.apps import apps
from django.contrib.admin import ListFilter
from django.contrib.admin.helpers import AdminForm
from django.contrib.auth.models import AbstractUser
from django.db.models.base import ModelBase, Model
from django.db.models.base import Model, ModelBase
from django.db.models.options import Options
from django.utils.translation import gettext

Expand Down Expand Up @@ -40,8 +40,7 @@ def get_admin_url(instance: Any, admin_site: str = "admin", from_app: bool = Fal
url = "#"

try:

if type(instance) == str:
if isinstance(instance, str):
app_label, model_name = instance.split(".")
model_name = model_name.lower()
url = reverse(
Expand Down Expand Up @@ -165,7 +164,6 @@ def make_menu(

menu = []
for link in links:

perm_matches = []
for perm in link.get("permissions", []):
perm_matches.append(user.has_perm(perm))
Expand Down
2 changes: 2 additions & 0 deletions tests/test_admin_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re

import django
import pytest

from jazzmin.compat import reverse

from .test_app.library.books.models import Book
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/library/books/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.auth.models import User
from django.utils.html import format_html
from django.utils.timesince import timesince

from jazzmin.utils import attr

from ..loans.admin import BookLoanInline
Expand Down
10 changes: 5 additions & 5 deletions tests/test_app/library/books/management/commands/reset.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from random import choice

from django.contrib.auth.models import User, Group
from django.contrib.auth.models import Group, User
from django.core.management import BaseCommand

from ...models import Book, Author, Genre
from ....factories import (
BookLoanFactory,
UserFactory,
AuthorFactory,
BookFactory,
BookLoanFactory,
GroupFactory,
LibraryFactory,
UserFactory,
)
from ....loans.models import Library, BookLoan
from ....loans.models import BookLoan, Library
from ...models import Author, Book, Genre


class Command(BaseCommand):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/library/loans/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.views.generic import TemplateView
from django.contrib.admin.sites import site
from django.views.generic import TemplateView


class CustomView(TemplateView):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_app/library/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@
},
# Custom icons for side menu apps/models See the link below
# https://fontawesome.com/icons?d=gallery&m=free&v=5.0.0,5.0.1,5.0.10,5.0.11,5.0.12,5.0.13,5.0.2,5.0.3,5.0.4,5.0.5,5.0.6,5.0.7,5.0.8,5.0.9,5.1.0,
# 5.1.1,5.2.0,5.3.0,5.3.1,5.4.0,5.4.1,5.4.2,5.13.0,5.12.0,5.11.2,5.11.1,5.10.0,5.9.0,5.8.2,5.8.1,5.7.2,5.7.1,5.7.0,5.6.3,5.5.0,5.4.2
# 5.1.1,5.2.0,5.3.0,5.3.1,5.4.0,5.4.1,5.4.2,5.13.0,5.12.0,
# 5.11.2,5.11.1,5.10.0,5.9.0,5.8.2,5.8.1,5.7.2,5.7.1,5.7.0,5.6.3,5.5.0,5.4.2
# for the full list of 5.13.0 free icon classes
"icons": {
"auth": "fas fa-users-cog",
Expand Down
3 changes: 2 additions & 1 deletion tests/test_customisation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from bs4 import BeautifulSoup
from django.urls import reverse

from jazzmin.settings import CHANGEFORM_TEMPLATES
from jazzmin.templatetags.jazzmin import get_sections

Expand Down Expand Up @@ -37,7 +38,7 @@ def test_update_login_logo(client, custom_jazzmin_settings):


@pytest.mark.django_db
@pytest.mark.parametrize("config_value,template", [(k, v) for k, v in CHANGEFORM_TEMPLATES.items()])
@pytest.mark.parametrize("config_value,template", list(CHANGEFORM_TEMPLATES.items()))
def test_changeform_templates(config_value, template, admin_client, custom_jazzmin_settings):
"""
All changeform config values use the correct templates
Expand Down
1 change: 1 addition & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from django.contrib.admin.models import CHANGE, LogEntry

from jazzmin.templatetags import jazzmin


Expand Down
9 changes: 5 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from unittest.mock import patch, MagicMock, Mock
from unittest.mock import MagicMock, Mock, patch

import pytest
from django.db.models.functions import Upper
from django.urls import reverse

from jazzmin.utils import (
order_with_respect_to,
get_admin_url,
get_app_admin_urls,
get_custom_url,
get_model_meta,
get_app_admin_urls,
get_view_permissions,
order_with_respect_to,
)
from .test_app.library.factories import BookFactory, UserFactory

from .test_app.library.books.models import Book
from .test_app.library.factories import BookFactory, UserFactory


def test_order_with_respect_to():
Expand Down

0 comments on commit 0f821fd

Please sign in to comment.