##// END OF EJS Templates
Added tag 4.5.0 for changeset 9a6dc4edfecb
Added tag 4.5.0 for changeset 9a6dc4edfecb

File last commit:

r1878:7fb52044 default
r1936:13b941d2 default
Show More
search.py
49 lines | 1.5 KiB | text/x-python | PythonLexer
from django.shortcuts import render
from django.views.generic import View
from django.db.models import Q
from django.core.urlresolvers import reverse
from boards.abstracts.paginator import get_paginator
from boards.forms import SearchForm, PlainErrorList
from boards.models import Post
from boards.views.mixins import PaginatedMixin
MIN_QUERY_LENGTH = 3
RESULTS_PER_PAGE = 10
FORM_QUERY = 'query'
CONTEXT_QUERY = 'query'
CONTEXT_FORM = 'form'
CONTEXT_PAGE = 'page'
REQUEST_PAGE = 'page'
__author__ = 'neko259'
TEMPLATE = 'search/search.html'
class BoardSearchView(View, PaginatedMixin):
def get(self, request):
params = dict()
form = SearchForm(request.GET, error_class=PlainErrorList)
params[CONTEXT_FORM] = form
if form.is_valid():
query = form.cleaned_data[FORM_QUERY]
if len(query) >= MIN_QUERY_LENGTH:
results = Post.objects.filter(Q(text__icontains=query)
| Q(title__icontains=query) | Q(opening=True,
thread__tags__aliases__name__icontains=query)).order_by('-id').distinct()
paginator = get_paginator(results, RESULTS_PER_PAGE)
paginator.set_url(reverse('search'), request.GET.dict())
page = int(request.GET.get(REQUEST_PAGE, '1'))
params[CONTEXT_QUERY] = query
params.update(self.get_page_context(paginator, page))
return render(request, TEMPLATE, params)