|
|
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)
|
|
|
|