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