[enh] storing search categories in cookies

This commit is contained in:
asciimoo 2013-10-17 21:46:35 +02:00
parent b915386456
commit 9bc6865ddc
1 changed files with 17 additions and 4 deletions

View File

@ -22,7 +22,7 @@ if __name__ == "__main__":
from os.path import realpath, dirname from os.path import realpath, dirname
path.append(realpath(dirname(realpath(__file__))+'/../')) path.append(realpath(dirname(realpath(__file__))+'/../'))
from flask import Flask, request, flash, render_template, url_for, Response from flask import Flask, request, flash, render_template, url_for, Response, make_response
import ConfigParser import ConfigParser
from os import getenv from os import getenv
from searx.engines import search, categories from searx.engines import search, categories
@ -54,6 +54,12 @@ def render(template_name, **kwargs):
global categories global categories
kwargs['categories'] = categories.keys() kwargs['categories'] = categories.keys()
if not 'selected_categories' in kwargs: if not 'selected_categories' in kwargs:
kwargs['selected_categories'] = []
cookie_categories = request.cookies.get('categories', '').split(',')
for ccateg in cookie_categories:
if ccateg in categories:
kwargs['selected_categories'].append(ccateg)
if not len(kwargs['selected_categories']):
kwargs['selected_categories'] = ['general'] kwargs['selected_categories'] = ['general']
return render_template(template_name, **kwargs) return render_template(template_name, **kwargs)
@ -74,13 +80,20 @@ def index():
selected_categories.append(category) selected_categories.append(category)
selected_engines.extend(x.name for x in categories[category]) selected_engines.extend(x.name for x in categories[category])
if not len(selected_engines): if not len(selected_engines):
selected_engines = [x.name for x in categories['general']] cookie_categories = request.cookies.get('categories', '').split(',')
for ccateg in cookie_categories:
if ccateg in categories:
selected_categories.append(ccateg)
selected_engines.extend(x.name for x in categories[ccateg])
query = request.form['q'].encode('utf-8') query = request.form['q'].encode('utf-8')
results = search(query, request, selected_engines) results = search(query, request, selected_engines)
if request.form.get('format') == 'json': if request.form.get('format') == 'json':
# TODO HTTP headers # TODO HTTP headers
return json.dumps({'query': query, 'results': results}) return json.dumps({'query': query, 'results': results})
return render('results.html', results=results, q=query.decode('utf-8'), selected_categories=selected_categories) template = render('results.html', results=results, q=query.decode('utf-8'), selected_categories=selected_categories)
resp = make_response(template)
resp.set_cookie('categories', ','.join(selected_categories))
return resp
return render('index.html') return render('index.html')
@app.route('/favicon.ico', methods=['GET']) @app.route('/favicon.ico', methods=['GET'])