Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make search URLs like /tree/search?q= rather than /search?tree=x&q=y.…

… Closes bug 834509.

Invalid trees now return 404, because that makes sense now that we're putting the tree name in the URL path.
  • Loading branch information...
commit 1914a6d52228dd0cfab8e0ff575a5af16d1a08d9 1 parent b902f06
@erikrose erikrose authored
View
15 dxr/app.py
@@ -39,8 +39,8 @@ def index():
return send_file(current_app.open_instance_resource('trees/index.html'))
-@dxr_blueprint.route('/search')
-def search():
+@dxr_blueprint.route('/<tree>/search')
+def search(tree):
"""Search by regex, caller, superclass, or whatever."""
# TODO: This function still does too much.
querystring = request.values
@@ -49,7 +49,6 @@ def search():
limit = non_negative_int(querystring.get('limit'), 100)
# Get and validate tree:
- tree = querystring.get('tree')
config = current_app.config
# Arguments for the template:
@@ -62,6 +61,7 @@ def search():
'generated_date': config['GENERATED_DATE']}
error = warning = ''
+ status_code = None
if tree in config['TREES']:
arguments['tree'] = tree
@@ -111,6 +111,7 @@ def search():
error = 'Failed to establish database connection.'
else:
error = "Tree '%s' is not a valid tree." % tree
+ status_code = 404
if warning or error:
arguments['error'] = error or warning
@@ -119,7 +120,7 @@ def search():
if error:
# Return a non-OK code so the live search doesn't try to replace
# the results with our empty ones:
- return jsonify(arguments), 500
+ return jsonify(arguments), status_code or 500
# Tuples are encoded as lists in JSON, and these are not real
# easy to unpack or read in Javascript. So for ease of use, we
@@ -133,8 +134,10 @@ def search():
for icon, path, lines in arguments['results']]
return jsonify(arguments)
- return render_template('error.html' if error else 'search.html',
- **arguments)
+ if error:
+ return render_template('error.html', **arguments), status_code or 500
+ else:
+ return render_template('search.html', **arguments)
@dxr_blueprint.route('/<tree>/source/')
View
5 dxr/static/advanced-search.js
@@ -144,9 +144,8 @@ function initAdvancedSearch(){
var as = document.getElementById("advanced-search");
as.addEventListener('submit', function(e){
// Change document location
- document.location = createSearchUrl({
- q: buildQueryFromAdvanced(),
- tree: dxr.tree()
+ document.location = createSearchUrl(dxr.tree(), {
+ q: buildQueryFromAdvanced()
});
e.preventDefault();
return false;
View
12 dxr/static/search.js
@@ -237,7 +237,6 @@ function fetch_results(display_fetcher){
// parameters for request
var params = {
q: state.query,
- tree: dxr.tree(),
limit: state.limit,
offset: state.offset,
redirect: 'false',
@@ -245,7 +244,7 @@ function fetch_results(display_fetcher){
};
// Start a new request
- request.open("GET", createSearchUrl(params), true);
+ request.open("GET", createSearchUrl(dxr.tree(), params), true);
request.send();
}
@@ -261,14 +260,14 @@ function parseQuerystring(){
}
/** Create search URL from search parameters as querystring */
-function createSearchUrl(params){
+function createSearchUrl(tree, params){
var elements = []
for(var key in params){
var k = encodeURIComponent(key);
var v = encodeURIComponent(params[key]);
elements.push(k + "=" + v);
}
- return wwwroot + "/search?" + elements.join("&");
+ return wwwroot + "/" + tree + "/search?" + elements.join("&");
}
window.createSearchUrl = createSearchUrl; // used in advanced-search.js
@@ -292,18 +291,17 @@ function initMenu(){
// Parse querystring so we can make some urls
var params = {
- tree: dxr.tree(),
limit: state.limit,
redirect: 'false'
};
// Create url to limit search
params.q = state.query + " path:" + path;
- var limit_url = createSearchUrl(params);
+ var limit_url = createSearchUrl(dxr.tree(), params);
// Create url to exclude path from search
params.q = state.query + " -path:" + path;
- var exclude_url = createSearchUrl(params);
+ var exclude_url = createSearchUrl(dxr.tree(), params);
// Populate menu with links
menu.populate([
View
5 dxr/templates/search.html
@@ -134,9 +134,8 @@
{# We don't show next link if there's no more results,
# and show prev link if offset != 0. Note that these links will
# only be available to non-javascript users. #}
- {% set search_url = wwwroot
- + '/search?tree=' + tree
- + '&q=' + quoted_query + '&redirect=false' %}
+ {% set search_url = wwwroot + '/' + tree + '/search?q=' +
+ quoted_query + '&redirect=false' %}
{% set prev = offset - results|length %}
{% set next = offset + results|length %}
<div id="result-page-switch">
View
2  dxr/testing.py
@@ -114,7 +114,7 @@ def search_results(self, query):
"""
response = self.client().get(
- '/search?format=json&tree=code&q=%s&redirect=false' % quote(query))
+ '/code/search?format=json&q=%s&redirect=false' % quote(query))
return json.loads(response.data)['results']
Please sign in to comment.
Something went wrong with that request. Please try again.