src/p4l/search/forms.py
changeset 113 c05567404888
child 115 4749704f9b40
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/p4l/search/forms.py	Fri Sep 20 22:21:48 2013 +0200
@@ -0,0 +1,32 @@
+# -*- coding: utf-8 -*-
+'''
+Created on Sep 20, 2013
+
+@author: ymh
+'''
+from haystack.forms import SearchForm
+
+from p4l.utils import strip_accents
+
+
+class RecordSearchForm(SearchForm):
+    
+    def __init__(self, *args, **kwargs):
+        SearchForm.__init__(self, *args, **kwargs)
+        
+    def no_query_found(self):
+        return self.searchqueryset.all()
+    
+    def search(self):
+        if not self.is_valid():
+            return self.no_query_found()
+
+        if not self.cleaned_data.get('q'):
+            return self.no_query_found()
+
+        sqs = self.searchqueryset.auto_query(strip_accents(self.cleaned_data['q']))
+
+        if self.load_all:
+            sqs = sqs.load_all()
+
+        return sqs