from django.shortcuts import render from django.views.generic import View from haystack.query import SearchQuerySet from boards.abstracts.paginator import get_paginator from boards.forms import SearchForm, PlainErrorList 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): 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 = SearchQuerySet().auto_query(query).order_by('-id') paginator = get_paginator(results, RESULTS_PER_PAGE) page = int(request.GET.get(REQUEST_PAGE, '1')) params[CONTEXT_PAGE] = paginator.page(page) params[CONTEXT_QUERY] = query return render(request, TEMPLATE, params)