Ticket #8723: search.diff

File search.diff, 9.6 KB (added by Fraser Nevett, 16 years ago)
  • djangodocs/views.py

     
    55from django.core import urlresolvers
    66from django.http import HttpResponseRedirect, Http404
    77from django.shortcuts import render_to_response
     8from django.template import RequestContext
    89from unipath import FSPath as Path
    910
    1011def index(request):
     
    2627    )
    2728
    2829def document(request, lang, version, url):
    29     if lang != 'en' or version != 'dev' or url == "search/": raise Http404()
     30    if lang != 'en' or version != 'dev': raise Http404()
    3031
    3132    docroot = Path(settings.DOCS_PICKLE_ROOT)
    3233
     
    4445        'docs/%s.html' % '-'.join([b for b in bits if b]),
    4546        'docs/doc.html'
    4647    ]
    47     return render_to_response(template_names, {
    48         'doc': pickle.load(open(doc)),
    49         'env': pickle.load(open(docroot.child('globalcontext.pickle'))),
     48    return render_to_response(template_names, RequestContext(request, {
     49        'doc': pickle.load(open(doc, 'rb')),
     50        'env': pickle.load(open(docroot.child('globalcontext.pickle'), 'rb')),
    5051        'update_date': datetime.datetime.fromtimestamp(docroot.child('last_build').mtime()),
    5152        'home': urlresolvers.reverse(document, kwargs={'lang':lang, 'version':version, 'url':''}),
    52     })
     53        'search': urlresolvers.reverse(search, kwargs={'lang':lang, 'version':version}),
     54    }))
    5355
    5456def images(request, lang, version, path):
    5557    if lang != 'en' or version != 'dev': raise Http404()
     
    6567        request,
    6668        document_root = Path(settings.DOCS_PICKLE_ROOT).child('_sources'),
    6769        path = path,
    68     )
    69  No newline at end of file
     70    )
     71
     72def search(request, lang, version):
     73    if lang != 'en' or version != 'dev': raise Http404()
     74   
     75    docroot = Path(settings.DOCS_PICKLE_ROOT)
     76   
     77    # Remove the 'cof' GET variable from the query string so that the page
     78    # linked to by the Javascript fallback doesn't think its inside an iframe.
     79    mutable_get = request.GET.copy()
     80    if 'cof' in mutable_get:
     81        del mutable_get['cof']
     82   
     83    return render_to_response('docs/search.html', RequestContext(request, {
     84        'query': request.GET.get('q'),
     85        'query_string': mutable_get.urlencode(),
     86        'env': pickle.load(open(docroot.child('globalcontext.pickle'), 'rb')),
     87        'home': urlresolvers.reverse(document, kwargs={'lang':lang, 'version':version, 'url':''}),
     88        'search': urlresolvers.reverse(search, kwargs={'lang':lang, 'version':version}),
     89    }))
     90 No newline at end of file
  • djangodocs/settings.py

     
    11from django_website.settings import *
    22
    33PREPEND_WWW = False
    4 INSTALLED_APPS = []
     4INSTALLED_APPS = ['djangodocs']
    55TEMPLATE_DIRS = [os.path.join(os.path.dirname(__file__), "templates")] + TEMPLATE_DIRS
     6TEMPLATE_CONTEXT_PROCESSORS = ['django.core.context_processors.request']
    67ROOT_URLCONF = 'djangodocs.urls'
    78CACHE_MIDDLEWARE_KEY_PREFIX = 'djangodocs'
    89
  • djangodocs/templatetags/docs.py

     
     1from django.template import Library
     2
     3from djangodocs.forms import SearchForm
     4
     5
     6register = Library()
     7
     8@register.inclusion_tag('docs/search_form.html', takes_context=True)
     9def search_form(context, search_form_id='search'):
     10    request = context['request']
     11    auto_id = 'id_%s_%%s' % search_form_id
     12    return {
     13        'form': SearchForm(initial=request.GET, auto_id=auto_id),
     14        'search_form_id': search_form_id,
     15        'action': context['search'],
     16        }
     17 No newline at end of file
  • djangodocs/urls.py

     
    1111        djangodocs.views.language,
    1212    ),
    1313    url(
    14         r'^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/(?P<url>[\w./-]*)$',
    15         djangodocs.views.document,
     14        r'^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/search/$',
     15        djangodocs.views.search,
    1616    ),
    1717    url(
    1818        r'^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_images/(?P<path>.*)$',
     
    2222        r'^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/_source/(?P<path>.*)$',
    2323        djangodocs.views.source,
    2424    ),
     25    url(
     26        r'^(?P<lang>[a-z-]+)/(?P<version>[\w.-]+)/(?P<url>[\w./-]*)$',
     27        djangodocs.views.document,
     28    ),
    2529)
  • djangodocs/forms.py

     
     1from django import newforms as forms
     2
     3AS_Q_CHOICES = (
     4    ('more:dev_docs', 'Latest'),
     5    ('more:1.0_docs', '1.0'),
     6    ('more:0.96_docs', '0.96'),
     7    ('more:all_docs', 'All'),
     8    )
     9
     10class SearchForm(forms.Form):
     11    q = forms.CharField(widget=forms.TextInput({'class': 'query'}))
     12    as_q = forms.ChoiceField(choices=AS_Q_CHOICES, widget=forms.RadioSelect, initial='more:dev_docs')
     13   
     14 No newline at end of file
  • djangodocs/templates/docs/doc.html

     
    11{% extends "base_docs.html" %}
    22
     3{% load docs %}
     4
    35{% block title %}{{ doc.title }} | Django Documentation{% endblock %}
    46
    57{% block extrahead %}
    68  {{ block.super }}
     9  <style type="text/css" media="screen">
     10    #docs-search {
     11      color: #000;
     12      float: right;
     13    }
     14    #docs-search form {
     15      font-size: 92%;
     16      margin: 0;
     17      padding: 1em 1em 0;
     18      white-space: nowrap;
     19    }
     20    form.search ul {
     21      list-style: none;
     22      margin: 0;
     23      padding: 0;
     24    }
     25    form.search li {
     26      display: inline;
     27      padding-right: 1em;
     28    }
     29    form.search .query {
     30      width: 18em;
     31    }
     32  </style>
    733  <link rel="stylesheet" href="http://media.djangoproject.com/css/pygments.css" type="text/css" />
    834{% endblock %}
    935
    10 {% block billboard %}<h2><a href="{{ home }}">Django documentation</a></h2>{% endblock %}
     36{% block billboard %}
     37  {% block search %}
     38    <div id="docs-search">
     39      {% search_form %}
     40    </div>
     41  {% endblock %}
     42  <h2><a href="{{ home }}">Django documentation</a></h2>
     43{% endblock %}
    1144
    1245{% block content %}
    1346
     
    73106    </ul>
    74107  {% endblock %}
    75108 
    76   <h3>Last update:</h3>
    77   <div>{{ update_date|date:"F j, Y, P" }} (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
     109  {% block last-update-wrapper %}
     110    <h3>Last update:</h3>
     111    <div>{{ update_date|date:"F j, Y, P" }} (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
     112  {% endblock %}
    78113{% endblock %}
  • djangodocs/templates/docs/search.html

     
     1{% extends "docs/doc.html" %}
     2
     3{% load docs %}
     4
     5{% block title %}Search | Django Docuemtation{% endblock %}
     6
     7{% block extrahead %}
     8  {{ block.super }}
     9  <style type="text/css" media="screen">
     10    #cse-search-results iframe {
     11      width: 100%;
     12    }
     13  </style>
     14{% endblock %}
     15
     16{% block toc-wrapper %}{% endblock %}
     17
     18{% block breadcrumbs-wrapper %}{% endblock %}
     19
     20{% block last-update-wrapper %}{% endblock %}
     21
     22{% block body %}
     23  {% if query %}
     24    <h1>Search Results</h1>
     25    <div id="cse-search-results">
     26      <div id="cse-search-results-js-required">
     27        The Django Documentation search uses a Google Custom Search, which
     28        requires Javascript. If your results don't appear here shortly, please
     29        <a href="http://www.google.com/cse?{{ query_string|escape }}">view the results on Google's site</a>.
     30      </div>
     31    </div>
     32    <script type="text/javascript">
     33      var googleSearchMessage = document.getElementById("cse-search-results-js-required");
     34      googleSearchMessage.parentNode.removeChild(googleSearchMessage);
     35   
     36      var googleSearchIframeName = "cse-search-results";
     37      var googleSearchFormName = "cse-search-box";
     38      var googleSearchDomain = "www.google.com";
     39      var googleSearchPath = "/cse";
     40    </script>
     41    <script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
     42  {% else %}
     43    <h1>Search</h1>
     44    {% search_form "search_page" %}
     45  {% endif %}
     46{% endblock %}
  • djangodocs/templates/docs/search_form.html

     
     1<form action="{{ action|escape }}" id="{{ search_form_id|escape }}" class="search">
     2  <div>
     3    <input type="hidden" name="cx" value="014182425414053223532:sfhzyefydv8" />
     4    <input type="hidden" name="cof" value="FORID:11" />
     5    <input type="hidden" name="ie" value="UTF-8" />
     6    <input type="hidden" name="hl" value="{{ lang|escape }}" />
     7    {{ form.q }}
     8    <input type="submit" name="sa" class="submit" value="Search" />
     9    {{ form.as_q }}
     10  </div>
     11</form>
     12<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form={{ search_form_id|escape }}&lang={{ lang|escape }}"></script>
Back to Top