##// END OF EJS Templates
Refactorings, refactorings...
Refactorings, refactorings...

File last commit:

r1986:0b41439a default
r2008:5d3c1f7e default
Show More
search.py
54 lines | 1.7 KiB | text/x-python | PythonLexer
neko259
Use own search form and view
r718 from django.shortcuts import render
from django.views.generic import View
neko259
Search posts by text and title
r1849 from django.db.models import Q
neko259
Adapt to django-2.0
r1986 from django.urls import reverse
neko259
Refactoring
r917
neko259
Use own search form and view
r718 from boards.abstracts.paginator import get_paginator
from boards.forms import SearchForm, PlainErrorList
neko259
Search by tags also, not only their posts
r1950 from boards.models import Post, Tag
neko259
Simplified pagination templates. Added a list of pages to the search results page
r1878 from boards.views.mixins import PaginatedMixin
neko259
Use own search form and view
r718
neko259
Refactoring
r917
MIN_QUERY_LENGTH = 3
RESULTS_PER_PAGE = 10
neko259
Small code cleanups
r721 FORM_QUERY = 'query'
CONTEXT_QUERY = 'query'
CONTEXT_FORM = 'form'
CONTEXT_PAGE = 'page'
neko259
Search by tags also, not only their posts
r1950 CONTEXT_TAGS = 'tags'
neko259
Small code cleanups
r721
REQUEST_PAGE = 'page'
neko259
Use own search form and view
r718 __author__ = 'neko259'
TEMPLATE = 'search/search.html'
neko259
Simplified pagination templates. Added a list of pages to the search results page
r1878 class BoardSearchView(View, PaginatedMixin):
neko259
Use own search form and view
r718 def get(self, request):
neko259
Refactoring
r917 params = dict()
neko259
Use own search form and view
r718 form = SearchForm(request.GET, error_class=PlainErrorList)
neko259
Refactoring
r917 params[CONTEXT_FORM] = form
neko259
Use own search form and view
r718
if form.is_valid():
neko259
Small code cleanups
r721 query = form.cleaned_data[FORM_QUERY]
neko259
Refactoring
r917 if len(query) >= MIN_QUERY_LENGTH:
neko259
Search OPs by the tag name
r1877 results = Post.objects.filter(Q(text__icontains=query)
| Q(title__icontains=query) | Q(opening=True,
neko259
Search by attachment urls
r1982 thread__tags__aliases__name__icontains=query)
| Q(attachments__url__icontains=query)).order_by('-id').distinct()
neko259
Refactoring
r917 paginator = get_paginator(results, RESULTS_PER_PAGE)
neko259
Simplified pagination templates. Added a list of pages to the search results page
r1878 paginator.set_url(reverse('search'), request.GET.dict())
neko259
Use own search form and view
r718
neko259
Refactoring
r917 page = int(request.GET.get(REQUEST_PAGE, '1'))
neko259
Use own search form and view
r718
neko259
Refactoring
r917 params[CONTEXT_QUERY] = query
neko259
Simplified pagination templates. Added a list of pages to the search results page
r1878 params.update(self.get_page_context(paginator, page))
neko259
Refactoring
r917
neko259
Search by tags also, not only their posts
r1950 tags = Tag.objects.get_tag_url_list(Tag.objects.filter(aliases__name__icontains=query))
params[CONTEXT_TAGS] = tags
neko259
Refactoring
r917 return render(request, TEMPLATE, params)