Show short entries first in search results to improve UX

This commit is contained in:
Eliot Berriot 2019-01-03 17:34:14 +01:00
parent 567884b741
commit 89f6e3656b
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
3 changed files with 19 additions and 9 deletions

View file

@ -9,7 +9,7 @@ from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit
from django.conf import settings
from django import urls
from django.db import transaction
from django.db import models, transaction
def rename_file(instance, field_name, new_name, allow_missing_file=False):
@ -139,3 +139,11 @@ def parse_meta(html):
meta = [elem for elem in tree.iter() if elem.tag in ["meta", "link"]]
return [dict([("tag", elem.tag)] + list(elem.items())) for elem in meta]
def order_for_search(qs, field):
"""
When searching, it's often more useful to have short results first,
this function will order the given qs based on the length of the given field
"""
return qs.annotate(__size=models.functions.Length(field)).order_by("__size")