##// END OF EJS Templates
Adapt to django-2.0
Adapt to django-2.0

File last commit:

r1986:0b41439a default
r1986:0b41439a default
Show More
search.py
54 lines | 1.7 KiB | text/x-python | PythonLexer
from django.shortcuts import render
from django.views.generic import View
from django.db.models import Q
from django.urls import reverse
from boards.abstracts.paginator import get_paginator
from boards.forms import SearchForm, PlainErrorList
from boards.models import Post, Tag
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'
CONTEXT_TAGS = 'tags'
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)
| Q(attachments__url__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))
tags = Tag.objects.get_tag_url_list(Tag.objects.filter(aliases__name__icontains=query))
params[CONTEXT_TAGS] = tags
return render(request, TEMPLATE, params)