Skip to content

Commit

Permalink
Fix broken Flickr Account list in Django Admin
Browse files Browse the repository at this point in the history
Was getting an error while loading the Flickr Account list in Django
Admin. Ended in:

```
  File "/Users/phil/Projects/personal/django-ditto/devproject/venv/lib/python3.11/site-packages/django/contrib/admin/templatetags/admin_list.py", line 310, in __init__
    super().__init__(*items)
  File "/Users/phil/Projects/personal/django-ditto/devproject/venv/lib/python3.11/site-packages/django/contrib/admin/templatetags/admin_list.py", line 231, in items_for_result
    result_repr = display_for_value(value, empty_value_display, boolean)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phil/Projects/personal/django-ditto/devproject/venv/lib/python3.11/site-packages/django/contrib/admin/utils.py", line 461, in display_for_value
    return _boolean_icon(value)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/phil/Projects/personal/django-ditto/devproject/venv/lib/python3.11/site-packages/django/contrib/admin/templatetags/admin_list.py", line 182, in _boolean_icon
    "admin/img/icon-%s.svg" % {True: "yes", False: "no", None: "unknown"}[field_val]
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: '40fdeee308d1a21f'
```

It was because `ditto.flickr.models.Account.has_credentials()` was returning
a string, not a boolean.

The it turns out that the `self.assertTrue()` and `self.assertFalse()`
tests weren't too picky about whether something was a boolean or not.

Fixes #245
  • Loading branch information
philgyford committed Apr 8, 2024
1 parent d526ea7 commit a5396f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ditto/flickr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_absolute_url(self):

def has_credentials(self):
"Does this at least have something in its API fields? True or False"
return self.api_key and self.api_secret
return bool(self.api_key and self.api_secret)


class TaggedPhoto(TaggedItemBase):
Expand Down
16 changes: 11 additions & 5 deletions tests/flickr/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ def test_ordering(self):
self.assertEqual(accounts[0].user.realname, "Bob")
self.assertEqual(accounts[1].user.realname, "Terry")

def test_has_credentials(self):
account_1 = AccountFactory(api_key="1234", api_secret="9876")
self.assertTrue(account_1.has_credentials())
account_2 = AccountFactory(api_key="", api_secret="")
self.assertFalse(account_2.has_credentials())
def test_has_credentials_both_set(self):
account = AccountFactory(api_key="1234", api_secret="9876")
self.assertEqual(account.has_credentials(), True)

def test_has_credentials_neither_set(self):
account = AccountFactory(api_key="", api_secret="")
self.assertEqual(account.has_credentials(), False)

def test_has_credentials_one_set(self):
account = AccountFactory(api_key="", api_secret="9876")
self.assertEqual(account.has_credentials(), False)

def test_get_absolute_url_no_user(self):
account = AccountFactory(user=None)
Expand Down

0 comments on commit a5396f4

Please sign in to comment.