]> git.parisson.com Git - telemeta.git/commitdiff
#37: more flexible id, title, and creator search (regex based)
authorolivier <>
Mon, 9 Feb 2009 21:53:55 +0000 (21:53 +0000)
committerolivier <>
Mon, 9 Feb 2009 21:53:55 +0000 (21:53 +0000)
telemeta/models/query.py
telemeta/web/base.py

index a9919c8055faa77d55ee36c441ac086340311ee4..fa589d74996837432d178b69d61643ee7a2bee67 100644 (file)
@@ -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) 
index 9335e57bbfda4547900b6ff9e28ea510ce1fd4d3..1371a6dd629e4ac7589ed4ec2f4536f947363d2e 100644 (file)
@@ -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)),