fix 500 when searching users

This commit is contained in:
thomas girod 2024-09-29 23:01:55 +02:00
parent 8ec3074488
commit c5f5ad3f75
3 changed files with 17 additions and 1 deletions

View File

@ -33,6 +33,9 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True) text = indexes.CharField(document=True, use_template=True)
auto = indexes.EdgeNgramField(use_template=True) auto = indexes.EdgeNgramField(use_template=True)
last_update = indexes.DateTimeField(model_attr="last_update") last_update = indexes.DateTimeField(model_attr="last_update")
last_login = indexes.DateTimeField(
model_attr="last_login", default="1970-01-01T00:00:00Z"
)
def get_model(self): def get_model(self):
return User return User

View File

@ -37,6 +37,8 @@ class TestSearchUsers(TestCase):
# restore the index # restore the index
call_command("update_index", "core", "--remove") call_command("update_index", "core", "--remove")
class TestSearchUsersAPI(TestSearchUsers):
def test_order(self): def test_order(self):
"""Test that users are ordered by last login date.""" """Test that users are ordered by last login date."""
self.client.force_login(subscriber_user.make()) self.client.force_login(subscriber_user.make())
@ -83,3 +85,13 @@ class TestSearchUsers(TestCase):
response = self.client.get(reverse("api:search_users") + "?search=bél") response = self.client.get(reverse("api:search_users") + "?search=bél")
assert response.status_code == 200 assert response.status_code == 200
assert [r["id"] for r in response.json()["results"]] == [belix.id] assert [r["id"] for r in response.json()["results"]] == [belix.id]
class TestSearchUsersView(TestSearchUsers):
"""Test the search user view (`GET /search`)."""
def test_page_ok(self):
"""Just test that the page loads."""
self.client.force_login(subscriber_user.make())
response = self.client.get(reverse("core:search"))
assert response.status_code == 200

View File

@ -85,7 +85,8 @@ def search_user(query):
SearchQuerySet() SearchQuerySet()
.models(User) .models(User)
.autocomplete(auto=query) .autocomplete(auto=query)
.order_by("-last_login")[:20] .order_by("-last_login")
.load_all()[:20]
) )
return [r.object for r in res] return [r.object for r in res]
except TypeError: except TypeError: