Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented the search view, and it works.

Still a fair bit of cleanup to do, mostly around styling, result display,
and result optomization.
  • Loading branch information...
commit 6be1e525603e875c0e5138fab652c431f9d05cc2 1 parent c87bb5e
@jacobian jacobian authored
View
29 django_website/docs/forms.py
@@ -1,13 +1,20 @@
+import haystack.forms
from django import forms
+from .models import DocumentRelease
+
+class DocSearchForm(haystack.forms.SearchForm):
+
+ def __init__(self, *args, **kwargs):
+ super(DocSearchForm, self).__init__(*args, **kwargs)
+ self.fields['release'] = forms.ModelChoiceField(
+ queryset = DocumentRelease.objects.all(),
+ initial = DocumentRelease.objects.default(),
+ empty_label = None,
+ widget = forms.RadioSelect,
+ )
+
+ def search(self):
+ sqs = super(DocSearchForm, self).search()
+ rel = self.cleaned_data['release']
+ return sqs.filter(lang=rel.lang, version=rel.version)
-AS_Q_CHOICES = (
- ('more:dev_docs', 'Latest'),
- ('more:1.0_docs', '1.0'),
- ('more:0.96_docs', '0.96'),
- ('more:all_docs', 'All'),
-)
-
-class SearchForm(forms.Form):
- q = forms.CharField(widget=forms.TextInput({'class': 'query'}))
- as_q = forms.ChoiceField(choices=AS_Q_CHOICES, widget=forms.RadioSelect, initial='more:dev_docs')
-
View
11 django_website/docs/models.py
@@ -49,8 +49,9 @@ def __unicode__(self):
@models.permalink
def get_absolute_url(self):
- return (
- 'document-detail',
- [],
- {'lang': self.lang, 'version': self.version, 'url': self.path}
- )
+ kwargs = {
+ 'lang': self.release.lang,
+ 'version': self.release.version,
+ 'url': self.path
+ }
+ return ('document-detail', [], kwargs)
View
26 django_website/docs/search_indexes.py
@@ -6,20 +6,20 @@
from .models import Document
class DocumentIndex(haystack.indexes.SearchIndex):
- text = haystack.indexes.CharField(document=True)
- lang = haystack.indexes.CharField(model_attr='release__lang', faceted=True)
- version = haystack.indexes.CharField(model_attr='release__version', faceted=True)
- path = haystack.indexes.CharField(model_attr='path')
- title = haystack.indexes.CharField(model_attr='title')
+ text = haystack.indexes.CharField(document=True)
+ lang = haystack.indexes.CharField(model_attr='release__lang', faceted=True)
+ version = haystack.indexes.CharField(model_attr='release__version', faceted=True)
+ path = haystack.indexes.CharField(model_attr='path')
+ title = haystack.indexes.CharField(model_attr='title')
- def get_queryset(self):
- return Document.objects.all().select_related('release')
+ def get_queryset(self):
+ return Document.objects.all().select_related('release')
- def prepare_text(self, obj):
- root = utils.get_doc_root(obj.release.lang, obj.release.version)
- docpath = utils.get_doc_path(root, obj.path)
- with open(docpath) as fp:
- doc = json.load(fp)
- return doc['body']
+ def prepare_text(self, obj):
+ root = utils.get_doc_root(obj.release.lang, obj.release.version)
+ docpath = utils.get_doc_path(root, obj.path)
+ with open(docpath) as fp:
+ doc = json.load(fp)
+ return doc['body']
haystack.site.register(Document, DocumentIndex)
View
56 django_website/docs/templates/docs/search.html
@@ -4,43 +4,33 @@
{% block title %}Search | Django documentation{% endblock %}
-{% block extrahead %}
- {{ block.super }}
- <style type="text/css" media="screen">
- #cse-search-results iframe {
- width: 100%;
- }
- </style>
-{% endblock %}
-
{% block toc-wrapper %}{% endblock %}
-
{% block breadcrumbs-wrapper %}{% endblock %}
-
{% block last-update-wrapper %}{% endblock %}
{% block body %}
- {% if query %}
- <h1>Search results</h1>
- <div id="cse-search-results">
- <div id="cse-search-results-js-required">
- The Django documentation search uses a Google Custom Search, which
- requires Javascript. If your results don't appear here shortly, please
- <a href="http://www.google.com/cse?{{ query_string|escape }}">view the results on Google's site</a>.
- </div>
- </div>
- <script type="text/javascript">
- var googleSearchMessage = document.getElementById("cse-search-results-js-required");
- googleSearchMessage.parentNode.removeChild(googleSearchMessage);
-
- var googleSearchIframeName = "cse-search-results";
- var googleSearchFormName = "cse-search-box";
- var googleSearchDomain = "www.google.com";
- var googleSearchPath = "/cse";
- </script>
- <script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
- {% else %}
- <h1>Search</h1>
- {% search_form "search_page" %}
+ <h2>Search</h2>
+ <form method="get" action=".">
+ {{ form.as_p }}
+ <input type="submit" value="Search">
+ </form>
+
+ {% if query %}
+ <h3>Results</h3>
+ {% for result in page.object_list %}
+ <p>
+ <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
+ </p>
+ {% empty %}
+ <p>No results found.</p>
+ {% endfor %}
+
+ {% if page.has_previous or page.has_next %}
+ <div>
+ {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
+ |
+ {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
+ </div>
+ {% endif %}
{% endif %}
{% endblock %}
View
14 django_website/docs/templates/docs/search_form.html
@@ -1,12 +1,6 @@
-<form action="{{ action|escape }}" id="{{ search_form_id|escape }}" class="search">
+<form action="{% url document-search %}" id="{{ search_form_id }}" class="search">
<div>
- <input type="hidden" name="cx" value="009763561546736975936:e88ek0eurf4" />
- <input type="hidden" name="cof" value="FORID:11" />
- <input type="hidden" name="ie" value="UTF-8" />
- <input type="hidden" name="hl" value="{{ lang|escape }}" />
- {{ form.q }}
- <input type="submit" name="sa" class="submit" value="Search" />
- {{ form.as_q }}
+ {{ form.as_p }}
+ <input type="submit" class="submit" value="Search" />
</div>
-</form>
-<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form={{ search_form_id|escape }}&amp;lang={{ lang|escape }}"></script>
+</form>
View
6 django_website/docs/templatetags/docs.py
@@ -1,7 +1,7 @@
from __future__ import absolute_import
from django import template
-from ..forms import SearchForm
+from ..forms import DocSearchForm
from ..models import DocumentRelease
from ..utils import get_doc_root, get_doc_path
@@ -12,9 +12,7 @@ def search_form(context, search_form_id='search'):
request = context['request']
auto_id = 'id_%s_%%s' % search_form_id
return {
- 'form': SearchForm(initial=request.GET, auto_id=auto_id),
- 'search_form_id': search_form_id,
- 'action': context['search'],
+ 'form': DocSearchForm(initial=request.GET, auto_id=auto_id),
}
@register.tag
View
35 django_website/docs/views.py
@@ -6,6 +6,8 @@
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from django.utils import simplejson
+import haystack.views
+from .forms import DocSearchForm
from .models import DocumentRelease
from .utils import get_doc_root_or_404, get_doc_path_or_404
@@ -31,7 +33,6 @@ def document(request, lang, version, url):
'docurl': url,
'update_date': datetime.datetime.fromtimestamp(docroot.child('last_build').mtime()),
'home': urlresolvers.reverse('document-index', kwargs={'lang':lang, 'version':version}),
- 'search': urlresolvers.reverse('document-search', kwargs={'lang':lang, 'version':version}),
'redirect_from': request.GET.get('from', None),
}))
@@ -58,21 +59,19 @@ def objects_inventory(request, lang, version):
response['Content-Type'] = "text/plain"
return response
-def search(request, lang, version):
- docroot = get_doc_root_or_404(lang, version)
-
- # Remove the 'cof' GET variable from the query string so that the page
- # linked to by the Javascript fallback doesn't think its inside an iframe.
- mutable_get = request.GET.copy()
- if 'cof' in mutable_get:
- del mutable_get['cof']
+class DocSearchView(haystack.views.SearchView):
+ def __init__(self, **kwargs):
+ kwargs.update({
+ 'template': 'docs/search.html',
+ 'form_class': DocSearchForm,
+ })
+ super(DocSearchView, self).__init__(**kwargs)
- return render_to_response('docs/search.html', RequestContext(request, {
- 'query': request.GET.get('q'),
- 'query_string': mutable_get.urlencode(),
- 'lang': lang,
- 'version': version,
- 'env': simplejson.load(open(docroot.child('globalcontext.json'), 'rb')),
- 'home': urlresolvers.reverse('document-index', kwargs={'lang':lang, 'version':version}),
- 'search': urlresolvers.reverse('document-search', kwargs={'lang':lang, 'version':version}),
- }))
+ def extra_context(self):
+ # Constuct a context that matches the rest of the doc page views.
+ default_release = DocumentRelease.objects.default()
+ return {
+ 'lang': default_release.lang,
+ 'version': default_release.version,
+ }
+
View
12 django_website/settings/docs.py
@@ -3,8 +3,8 @@
PREPEND_WWW = False
APPEND_SLASH = True
INSTALLED_APPS = [
- 'django_website.docs',
- 'haystack',
+ 'django_website.docs',
+ 'haystack',
]
TEMPLATE_CONTEXT_PROCESSORS += ["django.core.context_processors.request"]
ROOT_URLCONF = 'django_website.urls.docs'
@@ -19,8 +19,8 @@
# Haystack settings
HAYSTACK_SITECONF = 'django_website.docs.search_sites'
if PRODUCTION:
- HAYSTACK_SEARCH_ENGINE = 'solr'
- HAYSTACK_SOLR_URL = 'http://127.0.0.1:8983/solr'
+ HAYSTACK_SEARCH_ENGINE = 'solr'
+ HAYSTACK_SOLR_URL = 'http://127.0.0.1:8983/solr'
else:
- HAYSTACK_SEARCH_ENGINE = 'whoosh'
- HAYSTACK_WHOOSH_PATH = '/tmp/djangodocs.index'
+ HAYSTACK_SEARCH_ENGINE = 'whoosh'
+ HAYSTACK_WHOOSH_PATH = '/tmp/djangodocs.index'
View
11 django_website/urls/docs.py
@@ -1,5 +1,6 @@
from django.conf import settings
from django.conf.urls.defaults import *
+from haystack.views import search_view_factory
from ..docs import views
urlpatterns = patterns('',
@@ -8,6 +9,11 @@
views.index,
),
url(
+ r'^search/$',
+ search_view_factory(view_class=views.DocSearchView),
+ name = 'document-search'
+ ),
+ url(
r'^(?P<lang>[a-z-]+)/$',
views.language,
),
@@ -18,11 +24,6 @@
name = 'document-index',
),
url(
- r'^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/search/$',
- views.search,
- name = 'document-search',
- ),
- url(
r'^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_objects/$',
views.objects_inventory,
name = 'objects-inv',
Please sign in to comment.
Something went wrong with that request. Please try again.