1
from django.urls import reverse
2
from django.shortcuts import render
3
from django.http import HttpResponseRedirect
4
from forms import WlSearchForm
5
from pybb.models import Topic
6
from pybb.models import Post as ForumPost
1
# Create your views here.
4
from django.core.urlresolvers import reverse
5
from django.shortcuts import render_to_response
6
from django.template import RequestContext
8
from forms import SearchForm
7
10
from wiki.models import Article
11
from pybb.models import Post, Topic
8
12
from news.models import Post as NewsPost
9
from wlmaps.models import Map
10
from wlhelp.models import Building, Ware, Worker
12
choices = {'Forum': 'incl_forum',
13
'Encyclopedia': 'incl_help',
13
from online_help.models import Building, Ware
14
from wlmaps.models import Map
16
class DummyEmptyQueryset(object):
18
A simple dummy class when a search
19
should not be run. The template expects
20
a queryset and checks for the count member.
19
25
def search(request):
20
"""Custom search view."""
22
26
if request.method == 'POST':
23
"""This is executed when searching through the box in the navigation.
25
We build the query string and redirect it to this view again.
28
form = WlSearchForm(request.POST)
29
if form.is_valid() and form.cleaned_data['q'] != '':
31
search_url = 'q=%s' % (form.cleaned_data['q'])
33
section = choices.get(request.POST['section'], 'all')
35
# Add initial values of all the form fields
36
for field, v in form.fields.iteritems():
38
# Don't change the query string
40
search_url += '&%s=%s' % (field, v.initial)
42
# A particular section was chosen
43
search_url += '&%s=True' % (section)
44
# Set initial start date
45
search_url += '&start_date=%s' % (form.fields['start_date'].initial)
47
return HttpResponseRedirect('%s?%s' % (reverse('search'), search_url))
49
# Form invalid or no search query was given
51
return render(request, 'search/search.html', {'form': form})
53
elif request.method == 'GET':
54
form = WlSearchForm(request.GET)
55
if form.is_valid() and form.cleaned_data['q'] != '':
56
context = {'form': form,
57
'query': form.cleaned_data['q'],
59
# Search the models depending on the given section
60
# Add search results, if any is found, to the context
61
if form.cleaned_data['incl_forum']:
62
topic_results = [x for x in form.search(Topic)]
63
post_results = [x for x in form.search(ForumPost)]
64
if len(topic_results):
65
context['result'].update({'topics': topic_results})
67
context['result'].update({'posts': post_results})
69
if form.cleaned_data['incl_wiki']:
70
wiki_results = [x for x in form.search(Article)]
72
context['result'].update({'wiki': wiki_results})
74
if form.cleaned_data['incl_news']:
75
news_results = [x for x in form.search(NewsPost)]
77
context['result'].update({'news': news_results})
79
if form.cleaned_data['incl_maps']:
80
map_results = [x for x in form.search(Map)]
82
context['result'].update({'maps': map_results})
84
if form.cleaned_data['incl_help']:
85
worker_results = [x for x in form.search(Worker)]
86
ware_results = [x for x in form.search(Ware)]
87
building_results = [x for x in form.search(Building)]
88
if len(worker_results):
89
context['result'].update({'workers': worker_results})
91
context['result'].update({'wares': ware_results})
92
if len(building_results):
93
context['result'].update({'buildings': building_results})
95
return render(request, 'search/search.html', context)
97
# Form errors or no search query was given
98
return render(request, 'search/search.html', {'form': form})
27
form = SearchForm(request.POST)
30
query = form.cleaned_data["search"]
31
do_wiki = form.cleaned_data["incl_wiki"]
32
do_forum = form.cleaned_data["incl_forum"]
33
do_news = form.cleaned_data["incl_news"]
34
do_help = form.cleaned_data["incl_help"]
35
do_maps = form.cleaned_data["incl_maps"]
38
help_wares = Ware.search.query(query) if do_help else DummyEmptyQueryset()
39
help_buildings = Building.search.query(query) if do_help else DummyEmptyQueryset()
42
map_results = Map.search.query(query) if do_maps else DummyEmptyQueryset()
45
wiki_results = Article.search.query(query) if do_wiki else DummyEmptyQueryset()
48
forum_results_topic = Topic.search.query(query) if do_forum else DummyEmptyQueryset()
49
forum_results_post = Post.search.query(query) if do_forum else DummyEmptyQueryset()
52
news_results = NewsPost.search.query(query) if do_news else DummyEmptyQueryset()
55
"wiki_results": wiki_results,
57
"help_hits": help_wares.count() + help_buildings.count(),
58
"help_results_wares": help_wares,
59
"help_results_buildings": help_buildings,
61
"forum_hits": forum_results_post.count() + forum_results_topic.count(),
62
"forum_results_topic": forum_results_topic,
63
"forum_results_post": forum_results_post,
65
"map_results": map_results,
67
"news_results": news_results,
73
return render_to_response("wlsearch/search.html",
75
context_instance=RequestContext(request))
83
return render_to_response("wlsearch/search.html",
85
context_instance=RequestContext(request))