[SciPy-Dev] Google custom search for ask.scipy.org?

Pauli Virtanen pav@iki...
Thu Nov 11 14:53:51 CST 2010


On Sat, 30 Oct 2010 17:49:56 +0000, Pauli Virtanen wrote:
> A current (killer) problem with ask.scipy.org is that it does not have
> search. As a band-aid solution, it should be possible to add a Google
> custom search box, like on this page:
> http://scikit-learn.sourceforge.net/
> 
> Google searches "site:ask.scipy.org/en/topic KEYWORDS" work well, so
> that a custom search should also work OK.

Here's a patch (my previous didn't seem to reach the list...):


# HG changeset patch
# User Pauli Virtanen <pav@iki.fi>
# Date 1288530804 -3600
# Node ID 342b2a291ab1a37ff9f198e1833d3b7a4d60d8b3
# Parent  1af6eeb468587f11ff8017bd422f8c90830e27a9
ENH: add Google-powered search

diff --git a/solace/default_settings.cfg b/solace/default_settings.cfg
--- a/solace/default_settings.cfg
+++ b/solace/default_settings.cfg
@@ -172,5 +172,12 @@
 #: a proxy.
 IS_BEHIND_PROXY = False
 
+# target site for Google "site:" search
+GOOGLE_SEARCH_SITE = "ask.scipy.org/*/topic"
+
+# key for Google Custom Search
+# if None, the search is simply a redirect to Google
+GOOGLE_SEARCH_KEY = "001245708749086444135:49umyzakcpw"
+
 # get rid of them again
 del os, tempfile
diff --git a/solace/templates/_helpers.html b/solace/templates/_helpers.html
--- a/solace/templates/_helpers.html
+++ b/solace/templates/_helpers.html
@@ -27,3 +27,34 @@
   </span></span>
 {%- endmacro %}
 
+{% macro render_search() %}
+  <div class="search_form">
+    {% if settings.GOOGLE_SEARCH_KEY %}
+      <div id="cse">.</div>
+      <script src="http://www.google.com/jsapi" type="text/javascript"></script>
+      <script type="text/javascript">
+	google.load('search', '1', {language : 'en'});
+	google.setOnLoadCallback(function() {
+	var customSearchControl = new google.search.CustomSearchControl('{{ settings.GOOGLE_SEARCH_KEY }}');
+	customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
+	var options = new google.search.DrawOptions();
+	options.setAutoComplete(true);
+	customSearchControl.draw('cse', options);
+	}, true);
+      </script>
+      <noscript>
+      <form method="get" action="http://www.google.com/search">
+	<input type="text" id="search" name="q" value="" />
+	<input type="hidden" name="sitesearch" value="{{ settings.GOOGLE_SEARCH_SITE }}" />
+	<input type="submit" id="go" value="Search" />
+      </form>
+      </noscript>
+    {% else %}
+      <form method="get" action="http://www.google.com/search">
+	<input type="text" id="search" name="q" value="" />
+	<input type="hidden" name="sitesearch" value="{{ settings.GOOGLE_SEARCH_SITE }}" />
+	<input type="submit" id="go" value="Search" />
+      </form>
+    {% endif %}
+  </div>
+{% endmacro %}
diff --git a/solace/templates/kb/overview.html b/solace/templates/kb/overview.html
--- a/solace/templates/kb/overview.html
+++ b/solace/templates/kb/overview.html
@@ -1,5 +1,6 @@
 {% extends 'layout.html' %}
 {% from 'kb/_boxes.html' import render_topics, render_topic_tabs %}
+{% from '_helpers.html' import render_search %}
 {% set page_title = _('Overview') %}
 {% block html_head %}
 {{- super() }}
@@ -10,8 +11,10 @@
   <h1>{{ page_title }}</h1>
   <div class="explanation">
     <p>{% trans -%}
-      This is an overview of some of the newest topics here on Solace. You can view other topics grouped by activity, votes and hotness. 
+      This is an overview of some of the newest topics here on Solace. You can view other topics grouped by activity, votes and hotness.
     {%- endtrans %}
+    </p>
+    {{ render_search() }}
   </div>
   {{ render_topic_tabs('kb.overview', order_by) }}
   {{ render_topics(topics) }}
diff --git a/solace/templates/kb/search.html b/solace/templates/kb/search.html
new file mode 100644
--- /dev/null
+++ b/solace/templates/kb/search.html
@@ -0,0 +1,9 @@
+{% extends 'layout.html' %}
+{% from '_helpers.html' import render_search %}
+{% set page_title = _(Search) %}
+{% block body %}
+  <h1>{{ _('Search') }}</h1>
+  <p>
+    {{ render_search() }}
+  </p>
+{% endblock %}
diff --git a/solace/templates/layout.html b/solace/templates/layout.html
--- a/solace/templates/layout.html
+++ b/solace/templates/layout.html
@@ -77,6 +77,7 @@
     ('kb.new', true, _('Ask')),
     ('kb.overview', true, _('Overview')),
     ('kb.unanswered', true, _('Unanswered')),
+    ('kb.search', true, _('Search')),
     ('kb.tags', true, _('Tags')),
     (('kb.userlist', true, _('Users')) if request.view_lang and settings.LANGUAGE_SECTIONS|length > 1
      else ('users.userlist', false, _('Users'))),
diff --git a/solace/urls.py b/solace/urls.py
--- a/solace/urls.py
+++ b/solace/urls.py
@@ -43,6 +43,9 @@
         Rule('/users/') > 'kb.userlist'
     ]),
 
+    # kb search
+    Rule('/search/') > 'kb.search',
+
     # kb sections not depending on the lang code
     Rule('/sections/') > 'kb.sections',
 
diff --git a/solace/views/kb.py b/solace/views/kb.py
--- a/solace/views/kb.py
+++ b/solace/views/kb.py
@@ -381,6 +381,10 @@
     return common_userlist(request, locale=request.view_lang)
 
 
+def search(request):
+    """Show a Google search form."""
+    return render_template('kb/search.html')
+
 @no_cache
 @require_login
 @exchange_token_protected



More information about the SciPy-Dev mailing list