~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlsearch/views.py

  • Committer: Holger Rapp
  • Date: 2009-03-22 21:24:54 UTC
  • Revision ID: sirver@h566336-20090322212454-kgcadqq213hp4grh
First draft version of search using the sphinx search engine

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Create your views here.
 
2
 
 
3
 
 
4
from django.core.urlresolvers import reverse
 
5
from django.shortcuts import render_to_response
 
6
from django.template import RequestContext
 
7
 
 
8
from forms import SearchForm
 
9
 
 
10
from wiki.models import Article
 
11
from pybb.models import Post, Topic
 
12
 
 
13
class DummyEmptyQueryset(object):
 
14
    """
 
15
    A simple dummy class when a search
 
16
    should not be run. The template expects
 
17
    a queryset and checks for the count member.
 
18
    """
 
19
    def count(self):
 
20
        return 0
 
21
 
 
22
def search(request):
 
23
    if request.method == 'POST':
 
24
        form = SearchForm(request.POST)
 
25
         
 
26
        if form.is_valid():
 
27
            query = form.cleaned_data["search"]
 
28
            do_wiki = form.cleaned_data["incl_wiki"]
 
29
            do_forum = form.cleaned_data["incl_forum"]
 
30
            
 
31
            wiki_results = Article.search.query(query) if do_wiki else DummyEmptyQueryset()
 
32
            forum_results = Post.search.query(query) if do_forum else DummyEmptyQueryset()
 
33
    
 
34
            template_params = {
 
35
                "wiki_results": wiki_results,
 
36
                "forum_results": forum_results,
 
37
            }
 
38
 
 
39
            return render_to_response("wlsearch/results.html",
 
40
                       template_params,
 
41
                       context_instance=RequestContext(request))
 
42
    else:
 
43
        form = SearchForm()
 
44
 
 
45
    template_params = {
 
46
        "search_form": form,
 
47
    }
 
48
    return render_to_response("wlsearch/search.html",
 
49
                              template_params,
 
50
                              context_instance=RequestContext(request))
 
51
 
 
52