search.py
41 lines
| 1.2 KiB
| text/x-python
|
PythonLexer
|
r718 | from django.shortcuts import render | ||
from django.template import RequestContext | ||||
from django.views.generic import View | ||||
from haystack.query import SearchQuerySet | ||||
from boards.abstracts.paginator import get_paginator | ||||
from boards.forms import SearchForm, PlainErrorList | ||||
|
r721 | FORM_QUERY = 'query' | ||
CONTEXT_QUERY = 'query' | ||||
CONTEXT_FORM = 'form' | ||||
CONTEXT_PAGE = 'page' | ||||
REQUEST_PAGE = 'page' | ||||
|
r718 | __author__ = 'neko259' | ||
TEMPLATE = 'search/search.html' | ||||
class BoardSearchView(View): | ||||
def get(self, request): | ||||
context = RequestContext(request) | ||||
form = SearchForm(request.GET, error_class=PlainErrorList) | ||||
|
r721 | context[CONTEXT_FORM] = form | ||
|
r718 | |||
if form.is_valid(): | ||||
|
r721 | query = form.cleaned_data[FORM_QUERY] | ||
|
r718 | if len(query) >= 3: | ||
|
r862 | results = SearchQuerySet().auto_query(query).order_by('-id') | ||
|
r718 | paginator = get_paginator(results, 10) | ||
|
r721 | if REQUEST_PAGE in request.GET: | ||
page = int(request.GET[REQUEST_PAGE]) | ||||
|
r718 | else: | ||
page = 1 | ||||
|
r721 | context[CONTEXT_PAGE] = paginator.page(page) | ||
context[CONTEXT_QUERY] = query | ||||
|
r718 | |||
|
r872 | # TODO Use dict here | ||
return render(request, TEMPLATE, context_instance=context) | ||||