~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlsearch/views.py

  • Committer: Holger Rapp
  • Date: 2010-09-26 13:30:30 UTC
  • Revision ID: sirver@gmx.de-20100926133030-ceirjf83vde91tyt
Added a simple events model to display dates on the homepage

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
 
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
 
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
11
 
 
12
 
choices = {'Forum': 'incl_forum',
13
 
           'Encyclopedia': 'incl_help',
14
 
           'Wiki': 'incl_wiki',
15
 
           'News': 'incl_news',
16
 
           'Maps': 'incl_maps'}
17
 
 
 
13
from online_help.models import Building, Ware
 
14
from wlmaps.models import Map 
 
15
 
 
16
class DummyEmptyQueryset(object):
 
17
    """
 
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.
 
21
    """
 
22
    def count(self):
 
23
        return 0
18
24
 
19
25
def search(request):
20
 
    """Custom search view."""
21
 
 
22
26
    if request.method == 'POST':
23
 
        """This is executed when searching through the box in the navigation.
24
 
 
25
 
        We build the query string and redirect it to this view again.
26
 
 
27
 
        """
28
 
        form = WlSearchForm(request.POST)
29
 
        if form.is_valid() and form.cleaned_data['q'] != '':
30
 
            # Query string
31
 
            search_url = 'q=%s' % (form.cleaned_data['q'])
32
 
            
33
 
            section = choices.get(request.POST['section'], 'all')
34
 
            if section == 'all':
35
 
                # Add initial values of all the form fields
36
 
                for field, v in form.fields.iteritems():
37
 
                    if field == 'q':
38
 
                        # Don't change the query string
39
 
                        continue
40
 
                    search_url += '&%s=%s' % (field, v.initial)
41
 
            else:
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)
46
 
 
47
 
            return HttpResponseRedirect('%s?%s' % (reverse('search'), search_url))
48
 
        
49
 
        # Form invalid or no search query was given
50
 
        form = WlSearchForm()
51
 
        return render(request, 'search/search.html', {'form': form})
52
 
    
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'],
58
 
                       'result': {}}
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})
66
 
                if len(post_results):
67
 
                    context['result'].update({'posts': post_results})
68
 
    
69
 
            if form.cleaned_data['incl_wiki']:
70
 
                wiki_results = [x for x in form.search(Article)]
71
 
                if len(wiki_results):
72
 
                    context['result'].update({'wiki': wiki_results})
73
 
    
74
 
            if form.cleaned_data['incl_news']:
75
 
                news_results = [x for x in form.search(NewsPost)]
76
 
                if len(news_results):
77
 
                    context['result'].update({'news': news_results})
78
 
    
79
 
            if form.cleaned_data['incl_maps']:
80
 
                map_results = [x for x in form.search(Map)]
81
 
                if len(map_results):
82
 
                    context['result'].update({'maps': map_results})
83
 
    
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})
90
 
                if len(ware_results):
91
 
                    context['result'].update({'wares': ware_results})
92
 
                if len(building_results):
93
 
                    context['result'].update({'buildings': building_results})
94
 
    
95
 
            return render(request, 'search/search.html', context)
96
 
        
97
 
        # Form errors or no search query was given
98
 
        return render(request, 'search/search.html', {'form': form})
 
27
        form = SearchForm(request.POST)
 
28
         
 
29
        if form.is_valid():
 
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"]
 
36
            
 
37
            # Help
 
38
            help_wares = Ware.search.query(query) if do_help else DummyEmptyQueryset()
 
39
            help_buildings = Building.search.query(query) if do_help else DummyEmptyQueryset()
 
40
        
 
41
            # Maps
 
42
            map_results = Map.search.query(query) if do_maps else DummyEmptyQueryset()
 
43
            
 
44
            # Wiki
 
45
            wiki_results = Article.search.query(query) if do_wiki else DummyEmptyQueryset()
 
46
 
 
47
            # Forum
 
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()
 
50
 
 
51
            # News
 
52
            news_results = NewsPost.search.query(query) if do_news else DummyEmptyQueryset()
 
53
            
 
54
            template_params = {
 
55
                "wiki_results": wiki_results,
 
56
 
 
57
                "help_hits": help_wares.count() + help_buildings.count(),
 
58
                "help_results_wares": help_wares,
 
59
                "help_results_buildings": help_buildings,
 
60
 
 
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,
 
64
 
 
65
                "map_results": map_results,
 
66
 
 
67
                "news_results": news_results,
 
68
 
 
69
                "search_form": form,
 
70
                "post": True,
 
71
            }
 
72
 
 
73
            return render_to_response("wlsearch/search.html",
 
74
                       template_params,
 
75
                       context_instance=RequestContext(request))
 
76
    else:
 
77
        form = SearchForm()
 
78
 
 
79
    template_params = {
 
80
        "search_form": form,
 
81
        "post": False,
 
82
    }
 
83
    return render_to_response("wlsearch/search.html",
 
84
                              template_params,
 
85
                              context_instance=RequestContext(request))
 
86
 
 
87