Show More
@@ -1,53 +1,54 b'' | |||
|
1 | 1 | from django.shortcuts import render |
|
2 | 2 | from django.views.generic import View |
|
3 | 3 | from django.db.models import Q |
|
4 | 4 | from django.core.urlresolvers import reverse |
|
5 | 5 | |
|
6 | 6 | from boards.abstracts.paginator import get_paginator |
|
7 | 7 | from boards.forms import SearchForm, PlainErrorList |
|
8 | 8 | from boards.models import Post, Tag |
|
9 | 9 | from boards.views.mixins import PaginatedMixin |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | MIN_QUERY_LENGTH = 3 |
|
13 | 13 | RESULTS_PER_PAGE = 10 |
|
14 | 14 | |
|
15 | 15 | FORM_QUERY = 'query' |
|
16 | 16 | |
|
17 | 17 | CONTEXT_QUERY = 'query' |
|
18 | 18 | CONTEXT_FORM = 'form' |
|
19 | 19 | CONTEXT_PAGE = 'page' |
|
20 | 20 | CONTEXT_TAGS = 'tags' |
|
21 | 21 | |
|
22 | 22 | REQUEST_PAGE = 'page' |
|
23 | 23 | |
|
24 | 24 | __author__ = 'neko259' |
|
25 | 25 | |
|
26 | 26 | TEMPLATE = 'search/search.html' |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | class BoardSearchView(View, PaginatedMixin): |
|
30 | 30 | def get(self, request): |
|
31 | 31 | params = dict() |
|
32 | 32 | |
|
33 | 33 | form = SearchForm(request.GET, error_class=PlainErrorList) |
|
34 | 34 | params[CONTEXT_FORM] = form |
|
35 | 35 | |
|
36 | 36 | if form.is_valid(): |
|
37 | 37 | query = form.cleaned_data[FORM_QUERY] |
|
38 | 38 | if len(query) >= MIN_QUERY_LENGTH: |
|
39 | 39 | results = Post.objects.filter(Q(text__icontains=query) |
|
40 | 40 | | Q(title__icontains=query) | Q(opening=True, |
|
41 |
thread__tags__aliases__name__icontains=query) |
|
|
41 | thread__tags__aliases__name__icontains=query) | |
|
42 | | Q(attachments__url__icontains=query)).order_by('-id').distinct() | |
|
42 | 43 | paginator = get_paginator(results, RESULTS_PER_PAGE) |
|
43 | 44 | paginator.set_url(reverse('search'), request.GET.dict()) |
|
44 | 45 | |
|
45 | 46 | page = int(request.GET.get(REQUEST_PAGE, '1')) |
|
46 | 47 | |
|
47 | 48 | params[CONTEXT_QUERY] = query |
|
48 | 49 | params.update(self.get_page_context(paginator, page)) |
|
49 | 50 | |
|
50 | 51 | tags = Tag.objects.get_tag_url_list(Tag.objects.filter(aliases__name__icontains=query)) |
|
51 | 52 | params[CONTEXT_TAGS] = tags |
|
52 | 53 | |
|
53 | 54 | return render(request, TEMPLATE, params) |
General Comments 0
You need to be logged in to leave comments.
Login now