~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlsearch/views.py

  • Committer: franku
  • Author(s): GunChleoc
  • Date: 2016-12-13 18:30:38 UTC
  • mfrom: (438.1.6 pyformat_util)
  • Revision ID: somal@arcor.de-20161213183038-5cgmvfh2fkgmoc1s
adding a script to run pyformat over the code base

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
from wlhelp.models import Building, Ware
14
14
from wlmaps.models import Map
15
15
 
 
16
 
16
17
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
 
    """
 
18
    """A simple dummy class when a search should not be run.
 
19
 
 
20
    The template expects a queryset and checks for the count member.
 
21
 
 
22
    """
 
23
 
22
24
    def count(self):
23
25
        return 0
24
26
 
 
27
 
25
28
def search(request):
26
29
    if request.method == 'POST':
27
30
        form = SearchForm(request.POST)
28
31
 
29
32
        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"]
 
33
            query = form.cleaned_data['search']
 
34
            do_wiki = form.cleaned_data['incl_wiki']
 
35
            do_forum = form.cleaned_data['incl_forum']
 
36
            do_news = form.cleaned_data['incl_news']
 
37
            do_help = form.cleaned_data['incl_help']
 
38
            do_maps = form.cleaned_data['incl_maps']
36
39
 
37
40
            # Help
38
 
            wlhelp_wares = Ware.search.query(query) if do_help else DummyEmptyQueryset()
39
 
            wlhelp_buildings = Building.search.query(query) if do_help else DummyEmptyQueryset()
 
41
            wlhelp_wares = Ware.search.query(
 
42
                query) if do_help else DummyEmptyQueryset()
 
43
            wlhelp_buildings = Building.search.query(
 
44
                query) if do_help else DummyEmptyQueryset()
40
45
 
41
46
            # Maps
42
 
            map_results = Map.search.query(query) if do_maps else DummyEmptyQueryset()
 
47
            map_results = Map.search.query(
 
48
                query) if do_maps else DummyEmptyQueryset()
43
49
 
44
50
            # Wiki
45
 
            wiki_results = Article.search.query(query) if do_wiki else DummyEmptyQueryset()
 
51
            wiki_results = Article.search.query(
 
52
                query) if do_wiki else DummyEmptyQueryset()
46
53
 
47
54
            # 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()
 
55
            forum_results_topic = Topic.search.query(
 
56
                query) if do_forum else DummyEmptyQueryset()
 
57
            forum_results_post = Post.search.query(
 
58
                query) if do_forum else DummyEmptyQueryset()
50
59
 
51
60
            # News
52
 
            news_results = NewsPost.search.query(query) if do_news else DummyEmptyQueryset()
 
61
            news_results = NewsPost.search.query(
 
62
                query) if do_news else DummyEmptyQueryset()
53
63
 
54
64
            template_params = {
55
 
                "wiki_results": wiki_results,
56
 
 
57
 
                "wlhelp_hits": wlhelp_wares.count() + wlhelp_buildings.count(),
58
 
                "wlhelp_results_wares": wlhelp_wares,
59
 
                "wlhelp_results_buildings": wlhelp_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,
 
65
                'wiki_results': wiki_results,
 
66
 
 
67
                'wlhelp_hits': wlhelp_wares.count() + wlhelp_buildings.count(),
 
68
                'wlhelp_results_wares': wlhelp_wares,
 
69
                'wlhelp_results_buildings': wlhelp_buildings,
 
70
 
 
71
                'forum_hits': forum_results_post.count() + forum_results_topic.count(),
 
72
                'forum_results_topic': forum_results_topic,
 
73
                'forum_results_post': forum_results_post,
 
74
 
 
75
                'map_results': map_results,
 
76
 
 
77
                'news_results': news_results,
 
78
 
 
79
                'search_form': form,
 
80
                'post': True,
71
81
            }
72
82
 
73
 
            return render_to_response("wlsearch/search.html",
74
 
                       template_params,
75
 
                       context_instance=RequestContext(request))
 
83
            return render_to_response('wlsearch/search.html',
 
84
                                      template_params,
 
85
                                      context_instance=RequestContext(request))
76
86
    else:
77
87
        form = SearchForm()
78
88
 
79
89
    template_params = {
80
 
        "search_form": form,
81
 
        "post": False,
 
90
        'search_form': form,
 
91
        'post': False,
82
92
    }
83
 
    return render_to_response("wlsearch/search.html",
 
93
    return render_to_response('wlsearch/search.html',
84
94
                              template_params,
85
95
                              context_instance=RequestContext(request))
86
 
 
87