Commit c0d7e2ab by benjaoming

Search function

parent c2cfcc2a
...@@ -351,4 +351,9 @@ class PermissionsForm(PluginSettingsFormMixin, forms.ModelForm): ...@@ -351,4 +351,9 @@ class PermissionsForm(PluginSettingsFormMixin, forms.ModelForm):
class DirFilterForm(forms.Form): class DirFilterForm(forms.Form):
query = forms.CharField(widget=forms.TextInput(attrs={'placeholder': _(u'Filter...'), query = forms.CharField(widget=forms.TextInput(attrs={'placeholder': _(u'Filter...'),
'class': 'search-query'})) 'class': 'search-query'}), required=False)
class SearchForm(forms.Form):
query = forms.CharField(widget=forms.TextInput(attrs={'placeholder': _(u'Search...'),
'class': 'search-query'}), required=False)
...@@ -108,9 +108,9 @@ ...@@ -108,9 +108,9 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
</a> </a>
<a class="brand" href="{% url 'wiki:root' %}">django-wiki</a> <a class="brand" href="{% url 'wiki:root' %}">django-wiki</a>
<form class="navbar-search pull-right" id="navbar_wiki_search" method="GET" action="asdsd"> <form class="navbar-search pull-right" id="navbar_wiki_search" method="GET" action="{% url 'wiki:search' %}">
<span class="icon-search"></span> <span class="icon-search"></span>
<input type="text" class="search-query" placeholder="{% trans "Search..." %}" /> <input type="text" name="query" class="search-query" placeholder="{% trans "Search..." %}" />
</form> </form>
<div class="pull-right"> <div class="pull-right">
{% if user.is_authenticated %} {% if user.is_authenticated %}
......
{% extends "wiki/article.html" %}
{% load wiki_tags i18n humanize %}
{% load url from future %}
{% block pagetitle %}{% trans "Search results for:" %} {{ search_query }}{% endblock %}
{% block wiki_contents_tab %}
<form class="form-search directory-toolbar">
<div class="well well-small">
<div class="pull-right">
{{ search_form.query }}
</div>
<div class="clearfix"></div>
</div>
</form>
<table class="table table-striped">
<tr>
<th style="width: 75%">{% trans "Title" %}</th>
<th>{% trans "Last modified" %}</th>
</tr>
{% for article in articles %}
<tr>
<td>
{% for urlpath in article.urlpath_set.all %}
<a href="{% url 'wiki:get' path=urlpath.path %}">{{ article.current_revision.title }}<br /><small class="muted">/{{ urlpath.path }}</small></a>
{% empty %}
<a href="{% url 'wiki:get' article_id=article.id %}">{{ article.current_revision.title }}</a>
{% endfor %}
{% if article.current_revision.deleted %}
<span class="icon-trash"></span>
{% endif %}
{% if article.current_revision.locked %}
<span class="icon-lock"></span>
{% endif %}
<p class="muted"><small>{{ article.current_revision.content|get_content_snippet:search_query }}</small></p>
</td>
<td style="white-space: nowrap">
{{ article.current_revision.created|naturaltime }}
</td>
</tr>
{% empty%}
<tr>
<td colspan="100">
<em>{% trans "There are no articles in this level" %}</em>
</td>
</tr>
{% endfor %}
</table>
{% include "wiki/includes/pagination.html" %}
{% endblock %}
# -*- coding: utf-8 -*-
import re
from django.conf import settings as django_settings from django.conf import settings as django_settings
from django import template from django import template
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.db.models import Model from django.db.models import Model
from django.forms import BaseForm from django.forms import BaseForm
from django.utils.safestring import mark_safe
from django.template.defaultfilters import striptags
register = template.Library() register = template.Library()
...@@ -56,6 +61,24 @@ def wiki_form(context, form_obj): ...@@ -56,6 +61,24 @@ def wiki_form(context, form_obj):
} }
@register.filter @register.filter
def get_content_snippet(content, keyword, max_words=30):
max_words = int(max_words)
p = re.compile(r'(?P<before>.*)%s(?P<after>.*)' % re.escape(keyword), re.MULTILINE | re.IGNORECASE | re.DOTALL)
m = p.search(content)
html = ""
if m:
words = filter(lambda x: x!="", striptags(m.group("before")).replace("\n", " ").split(" "))
before_words = words[-max_words/2:]
words = filter(lambda x: x!="", striptags(m.group("after")).replace("\n", " ").split(" "))
after = " ".join(words[:max_words - len(before_words)])
before = " ".join(before_words)
html = "%s <strong>%s</strong> %s" % (before,
striptags(keyword),
after)
html = mark_safe(html)
return html
@register.filter
def can_read(obj, user): def can_read(obj, user):
"""Articles and plugins have a can_read method...""" """Articles and plugins have a can_read method..."""
return obj.can_read(user=user) return obj.can_read(user=user)
......
...@@ -8,6 +8,7 @@ from wiki.core.plugins import registry ...@@ -8,6 +8,7 @@ from wiki.core.plugins import registry
urlpatterns = patterns('', urlpatterns = patterns('',
url('^$', article.ArticleView.as_view(), name='root', kwargs={'path': ''}), url('^$', article.ArticleView.as_view(), name='root', kwargs={'path': ''}),
url('^create-root/$', 'wiki.views.article.root_create', name='root_create'), url('^create-root/$', 'wiki.views.article.root_create', name='root_create'),
url('^_search/$', article.SearchView.as_view(), name='search'),
url('^_revision/diff/(?P<revision_id>\d+)/$', 'wiki.views.article.diff', name='diff'), url('^_revision/diff/(?P<revision_id>\d+)/$', 'wiki.views.article.diff', name='diff'),
) )
......
...@@ -443,8 +443,40 @@ class Dir(ListView, ArticleMixin): ...@@ -443,8 +443,40 @@ class Dir(ListView, ArticleMixin):
kwargs[self.context_object_name] = updated_children kwargs[self.context_object_name] = updated_children
return kwargs return kwargs
class SearchView(ListView):
template_name="wiki/search.html"
paginate_by = 25
context_object_name = "articles"
def dispatch(self, request, *args, **kwargs):
# Do not allow anonymous users to search if they cannot read content
if request.user.is_anonymous and not settings.ANONYMOUS:
return(settings.LOGIN_URL)
self.search_form = forms.SearchForm(request.GET)
if self.search_form.is_valid():
self.query = self.search_form.cleaned_data['query']
else:
self.query = None
return super(SearchView, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
if not self.query:
return models.Article.objects.get_empty_query_set()
articles = models.Article.objects.filter(Q(current_revision__title__contains=self.query) |
Q(current_revision__content__contains=self.query))
if not permissions.can_moderate(models.URLPath.root().article, self.request.user):
articles = articles.active().can_read(self.request.user)
return articles
def get_context_data(self, **kwargs):
kwargs = ListView.get_context_data(self, **kwargs)
kwargs['search_form'] = self.search_form
kwargs['search_query'] = self.query
return kwargs
class Plugin(View): class Plugin(View):
def dispatch(self, request, path=None, slug=None, **kwargs): def dispatch(self, request, path=None, slug=None, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment