Skip to content

Commit

Permalink
Properly handle empty file field
Browse files Browse the repository at this point in the history
Fix: #508
  • Loading branch information
PavelPancocha committed Apr 8, 2024
1 parent 78824ee commit a61a48f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
6 changes: 4 additions & 2 deletions jazzmin/templatetags/jazzmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,17 @@ 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 avatar_field is not None:
if not avatar_field:
return no_avatar
if isinstance(avatar_field, str):
return avatar_field
elif hasattr(avatar_field, "url"):
return avatar_field.url
elif callable(avatar_field):
return avatar_field()

logger.warning("avatar field must be an ImageField/URLField on the user model, or a callable")
logger.warning("Avatar field must be an ImageField/URLField on the user model, or a callable")

return no_avatar

Expand Down
43 changes: 37 additions & 6 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from unittest.mock import MagicMock
from typing import Any, Union
from unittest.mock import MagicMock, NonCallableMock

import pytest
from django.contrib.admin.models import CHANGE, LogEntry
Expand Down Expand Up @@ -50,16 +51,46 @@ def test_style_bold_first_word():

@pytest.mark.django_db
@pytest.mark.parametrize(
"case,test_input,field,expected",
"case,test_input,field,expected,log",
[
(1, MagicMock(avatar="image.jpg"), "avatar", "image.jpg"),
(2, MagicMock(avatar="image.jpg"), lambda u: u.avatar, "image.jpg"),
(3, MagicMock(avatar=MagicMock(url="image.jpg")), "avatar", "image.jpg"),
(1, MagicMock(avatar="image.jpg"), "avatar", "image.jpg", None),
(2, MagicMock(avatar="image.jpg"), lambda u: u.avatar, "image.jpg", None),
(3, MagicMock(avatar=MagicMock(url="image.jpg")), "avatar", "image.jpg", None),
# Properly set file field but empty (no image uploaded)
(
4,
MagicMock(avatar=MagicMock(__bool__=lambda x: False)),
"avatar",
"/static/vendor/adminlte/img/user2-160x160.jpg",
None,
),
# No avatar field set
(
5,
MagicMock(
avatar="image.jpg",
),
None,
"/static/vendor/adminlte/img/user2-160x160.jpg",
None,
),
# No proper avatar field set
(
6,
MagicMock(avatar=NonCallableMock(spec_set=["__bool__"], __bool__=lambda x: True)),
"avatar",
"/static/vendor/adminlte/img/user2-160x160.jpg",
"Avatar field must be",
),
],
)
def test_get_user_avatar(case, test_input, field, expected, custom_jazzmin_settings):
def test_get_user_avatar(case, test_input, field, expected, log, custom_jazzmin_settings, caplog):
"""
We can specify the name of a charfield or imagefield on our user model, or a callable that receives our user
"""
custom_jazzmin_settings["user_avatar"] = field
assert jazzmin.get_user_avatar(test_input) == expected
if log:
assert log in caplog.text
else:
assert not caplog.text

0 comments on commit a61a48f

Please sign in to comment.