##// END OF EJS Templates
Search OPs by the tag name
neko259 -
r1877:fdc4df62 default
parent child Browse files
Show More
@@ -1,45 +1,46 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
4
5 from boards.abstracts.paginator import get_paginator
5 from boards.abstracts.paginator import get_paginator
6 from boards.forms import SearchForm, PlainErrorList
6 from boards.forms import SearchForm, PlainErrorList
7 from boards.models import Post
7 from boards.models import Post
8
8
9
9
10 MIN_QUERY_LENGTH = 3
10 MIN_QUERY_LENGTH = 3
11 RESULTS_PER_PAGE = 10
11 RESULTS_PER_PAGE = 10
12
12
13 FORM_QUERY = 'query'
13 FORM_QUERY = 'query'
14
14
15 CONTEXT_QUERY = 'query'
15 CONTEXT_QUERY = 'query'
16 CONTEXT_FORM = 'form'
16 CONTEXT_FORM = 'form'
17 CONTEXT_PAGE = 'page'
17 CONTEXT_PAGE = 'page'
18
18
19 REQUEST_PAGE = 'page'
19 REQUEST_PAGE = 'page'
20
20
21 __author__ = 'neko259'
21 __author__ = 'neko259'
22
22
23 TEMPLATE = 'search/search.html'
23 TEMPLATE = 'search/search.html'
24
24
25
25
26 class BoardSearchView(View):
26 class BoardSearchView(View):
27 def get(self, request):
27 def get(self, request):
28 params = dict()
28 params = dict()
29
29
30 form = SearchForm(request.GET, error_class=PlainErrorList)
30 form = SearchForm(request.GET, error_class=PlainErrorList)
31 params[CONTEXT_FORM] = form
31 params[CONTEXT_FORM] = form
32
32
33 if form.is_valid():
33 if form.is_valid():
34 query = form.cleaned_data[FORM_QUERY]
34 query = form.cleaned_data[FORM_QUERY]
35 if len(query) >= MIN_QUERY_LENGTH:
35 if len(query) >= MIN_QUERY_LENGTH:
36 results = Post.objects.filter(Q(text__icontains=query) |
36 results = Post.objects.filter(Q(text__icontains=query)
37 Q(title__icontains=query)).order_by('-id')
37 | Q(title__icontains=query) | Q(opening=True,
38 thread__tags__aliases__name__icontains=query)).order_by('-id').distinct()
38 paginator = get_paginator(results, RESULTS_PER_PAGE)
39 paginator = get_paginator(results, RESULTS_PER_PAGE)
39
40
40 page = int(request.GET.get(REQUEST_PAGE, '1'))
41 page = int(request.GET.get(REQUEST_PAGE, '1'))
41
42
42 params[CONTEXT_PAGE] = paginator.page(page)
43 params[CONTEXT_PAGE] = paginator.page(page)
43 params[CONTEXT_QUERY] = query
44 params[CONTEXT_QUERY] = query
44
45
45 return render(request, TEMPLATE, params)
46 return render(request, TEMPLATE, params)
General Comments 0
You need to be logged in to leave comments. Login now