src/p4l/search/forms.py
author ymh <ymh.work@gmail.com>
Sun, 22 Sep 2013 00:28:01 +0200
changeset 115 4749704f9b40
parent 113 c05567404888
child 126 a345f1a67bf1
permissions -rw-r--r--
use a custom query parser
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Sep 20, 2013
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from haystack.forms import SearchForm
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
115
4749704f9b40 use a custom query parser
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
     9
from p4l.models.data import Record
4749704f9b40 use a custom query parser
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    10
from p4l.search.query_parser import QueryParser
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from p4l.utils import strip_accents
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
class RecordSearchForm(SearchForm):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    def __init__(self, *args, **kwargs):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        SearchForm.__init__(self, *args, **kwargs)
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    def no_query_found(self):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        return self.searchqueryset.all()
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    def search(self):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        if not self.is_valid():
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
            return self.no_query_found()
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        if not self.cleaned_data.get('q'):
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
            return self.no_query_found()
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
115
4749704f9b40 use a custom query parser
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    29
        qp = QueryParser("text")
4749704f9b40 use a custom query parser
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    30
        sqs = self.searchqueryset.models(Record).filter(qp.parse(strip_accents(self.cleaned_data['q'])))
113
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        if self.load_all:
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            sqs = sqs.load_all()
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
c05567404888 First version of indexation. Replace the list view by a search view
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        return sqs