Merge pull request #941 from ae-utbm/optimize-search

Optimize search
This commit is contained in:
thomas girod 2024-12-06 21:00:06 +01:00 committed by GitHub
commit 8dbec85c8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View File

@ -4,6 +4,7 @@ from typing import Annotated
from annotated_types import MinLen from annotated_types import MinLen
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
from django.db.models import Q from django.db.models import Q
from django.urls import reverse
from django.utils.text import slugify from django.utils.text import slugify
from haystack.query import SearchQuerySet from haystack.query import SearchQuerySet
from ninja import FilterSchema, ModelSchema, Schema from ninja import FilterSchema, ModelSchema, Schema
@ -37,13 +38,13 @@ class UserProfileSchema(ModelSchema):
@staticmethod @staticmethod
def resolve_profile_url(obj: User) -> str: def resolve_profile_url(obj: User) -> str:
return obj.get_absolute_url() return reverse("core:user_profile", kwargs={"user_id": obj.pk})
@staticmethod @staticmethod
def resolve_profile_pict(obj: User) -> str: def resolve_profile_pict(obj: User) -> str:
if obj.profile_pict_id is None: if obj.profile_pict_id is None:
return staticfiles_storage.url("core/img/unknown.jpg") return staticfiles_storage.url("core/img/unknown.jpg")
return obj.profile_pict.get_download_url() return reverse("core:download", kwargs={"file_id": obj.profile_pict_id})
class SithFileSchema(ModelSchema): class SithFileSchema(ModelSchema):

View File

@ -103,6 +103,12 @@ export class AutoCompleteSelectBase extends inheritHtmlElement("select") {
export abstract class AjaxSelect extends AutoCompleteSelectBase { export abstract class AjaxSelect extends AutoCompleteSelectBase {
protected filter?: (items: TomOption[]) => TomOption[] = null; protected filter?: (items: TomOption[]) => TomOption[] = null;
protected minCharNumberForSearch = 2; protected minCharNumberForSearch = 2;
/**
* A cache of researches that have been made using this input.
* For each record, the key is the user's query and the value
* is the list of results sent back by the server.
*/
protected cache = {} as Record<string, TomOption[]>;
protected abstract valueField: string; protected abstract valueField: string;
protected abstract labelField: string; protected abstract labelField: string;
@ -135,7 +141,13 @@ export abstract class AjaxSelect extends AutoCompleteSelectBase {
this.widget.clearOptions(); this.widget.clearOptions();
} }
const resp = await this.search(query); // Check in the cache if this query has already been typed
// and do an actual HTTP request only if the result isn't cached
let resp = this.cache[query];
if (!resp) {
resp = await this.search(query);
this.cache[query] = resp;
}
if (this.filter) { if (this.filter) {
callback(this.filter(resp), []); callback(this.filter(resp), []);