Merge pull request #845 from ae-utbm/fix-search

Fix 500 whean searching users
This commit is contained in:
thomas girod 2024-09-30 10:51:14 +02:00 committed by GitHub
commit c67155f02c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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)
auto = indexes.EdgeNgramField(use_template=True)
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):
return User

View File

@ -37,6 +37,8 @@ class TestSearchUsers(TestCase):
# restore the index
call_command("update_index", "core", "--remove")
class TestSearchUsersAPI(TestSearchUsers):
def test_order(self):
"""Test that users are ordered by last login date."""
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")
assert response.status_code == 200
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()
.models(User)
.autocomplete(auto=query)
.order_by("-last_login")[:20]
.order_by("-last_login")
.load_all()[:20]
)
return [r.object for r in res]
except TypeError: