From 1bfd8ec8fceedfbd24140530bce0d173e45fde99 Mon Sep 17 00:00:00 2001 From: olivier <> Date: Mon, 9 Feb 2009 21:53:55 +0000 Subject: [PATCH] #37: more flexible id, title, and creator search (regex based) --- telemeta/models/query.py | 32 ++++++++++++++++++++++++-------- telemeta/web/base.py | 6 +++--- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/telemeta/models/query.py b/telemeta/models/query.py index a9919c80..fa589d74 100644 --- a/telemeta/models/query.py +++ b/telemeta/models/query.py @@ -17,6 +17,19 @@ class CoreQuerySet(QuerySet): "Return an empty result set" return self.extra(where = ["0 = 1"]) + def pattern_to_regex(self, pattern): + regex = pattern; + regex = regex.replace('*', '.*') + regex = regex.replace('.', '.*') + regex = regex.replace('-', '.*') + regex = regex.replace(' ', '.*') + return regex + + def word_search(self, field, pattern): + regex = self.pattern_to_regex(pattern) + kwargs = {field + '__iregex': regex} + return self.filter(**kwargs) + class CoreManager(Manager): "Base class for all models managers" @@ -27,10 +40,11 @@ class MediaCollectionQuerySet(CoreQuerySet): def quick_search(self, pattern): "Perform a quick search on id, title and creator name" + regex = self.pattern_to_regex(pattern) return self.filter( - Q(id__icontains=pattern) | - Q(title__icontains=pattern) | - Q(creator__icontains=pattern) + Q(id__iregex=regex) | + Q(title__iregex=regex) | + Q(creator__iregex=regex) ) def by_country(self, country): @@ -131,10 +145,11 @@ class MediaItemQuerySet(CoreQuerySet): def quick_search(self, pattern): "Perform a quick search on id and title" + regex = self.pattern_to_regex(pattern) return self.filter( - Q(id__icontains=pattern) | - Q(_title__icontains=pattern) | - Q(auteur__icontains=pattern) + Q(id__iregex=regex) | + Q(_title__iregex=regex) | + Q(auteur__iregex=regex) ) def without_collection(self): @@ -149,8 +164,9 @@ class MediaItemQuerySet(CoreQuerySet): def by_title(self, pattern): # to (sort of) sync with models.media.MediaItem.get_title() - return self.filter(Q(_title__icontains=pattern) - | Q(collection__title__icontains=pattern)) + regex = self.pattern_to_regex(pattern) + return self.filter(Q(_title__iregex=regex) + | Q(collection__title__iregex=regex)) def by_publish_date(self, pattern): return self.filter(collection__date_published__icontains=pattern) diff --git a/telemeta/web/base.py b/telemeta/web/base.py index 9335e57b..1371a6dd 100644 --- a/telemeta/web/base.py +++ b/telemeta/web/base.py @@ -143,7 +143,7 @@ class WebView(Component): collections.quick_search(value), items.quick_search(value)), 'title': lambda value: ( - collections.filter(title__icontains=value), + collections.word_search('title', value), items.by_title(value)), 'country': lambda value: ( collections.by_country(value), @@ -155,8 +155,8 @@ class WebView(Component): collections.by_ethnic_group(value), items.filter(ethnie_grsocial = value)), 'creator': lambda value: ( - collections.filter(creator__icontains=value), - items.filter(auteur__icontains=value)), + collections.word_search('creator', value), + items.word_search('auteur', value)), 'rec_date': lambda value: ( collections.by_recording_date(value), items.by_recording_date(value)), -- 2.39.5