[mod] replace references request.request_data (GET or POST parameters) by request.form (based on merge of POST and GET parameters)

This commit is contained in:
Alexandre Flament 2016-11-02 14:52:22 +01:00
parent fbb080f358
commit 58a6c045c8
2 changed files with 13 additions and 23 deletions

View File

@ -128,7 +128,7 @@ def make_callback(engine_name, callback, params, result_container):
return process_callback return process_callback
def get_search_query_from_webapp(preferences, request_data): def get_search_query_from_webapp(preferences, form):
query = None query = None
query_engines = [] query_engines = []
query_categories = [] query_categories = []
@ -147,11 +147,11 @@ def get_search_query_from_webapp(preferences, request_data):
query_safesearch = preferences.get_value('safesearch') query_safesearch = preferences.get_value('safesearch')
# TODO better exceptions # TODO better exceptions
if not request_data.get('q'): if not form.get('q'):
raise Exception('noquery') raise Exception('noquery')
# set pagenumber # set pagenumber
pageno_param = request_data.get('pageno', '1') pageno_param = form.get('pageno', '1')
if not pageno_param.isdigit() or int(pageno_param) < 1: if not pageno_param.isdigit() or int(pageno_param) < 1:
pageno_param = 1 pageno_param = 1
@ -159,7 +159,7 @@ def get_search_query_from_webapp(preferences, request_data):
# parse query, if tags are set, which change # parse query, if tags are set, which change
# the serch engine or search-language # the serch engine or search-language
raw_text_query = RawTextQuery(request_data['q'], disabled_engines) raw_text_query = RawTextQuery(form['q'], disabled_engines)
raw_text_query.parse_query() raw_text_query.parse_query()
# set query # set query
@ -170,7 +170,7 @@ def get_search_query_from_webapp(preferences, request_data):
if len(raw_text_query.languages): if len(raw_text_query.languages):
query_lang = raw_text_query.languages[-1] query_lang = raw_text_query.languages[-1]
query_time_range = request_data.get('time_range') query_time_range = form.get('time_range')
query_engines = raw_text_query.engines query_engines = raw_text_query.engines
@ -185,7 +185,7 @@ def get_search_query_from_webapp(preferences, request_data):
else: else:
# set categories/engines # set categories/engines
load_default_categories = True load_default_categories = True
for pd_name, pd in request_data.items(): for pd_name, pd in form.items():
if pd_name == 'categories': if pd_name == 'categories':
query_categories.extend(categ for categ in map(unicode.strip, pd.split(',')) if categ in categories) query_categories.extend(categ for categ in map(unicode.strip, pd.split(',')) if categ in categories)
elif pd_name == 'engines': elif pd_name == 'engines':

View File

@ -364,16 +364,6 @@ def render(template_name, override_theme=None, **kwargs):
@app.before_request @app.before_request
def pre_request(): def pre_request():
# request.request_data
if request.method == 'POST':
request_data = request.form
elif request.method == 'GET':
request_data = request.args
else:
request_data = {}
request.request_data = request_data
# merge GET, POST vars # merge GET, POST vars
preferences = Preferences(themes, categories.keys(), engines, plugins) preferences = Preferences(themes, categories.keys(), engines, plugins)
try: try:
@ -416,7 +406,7 @@ def index():
search_query = None search_query = None
result_container = None result_container = None
try: try:
search_query = get_search_query_from_webapp(request.preferences, request.request_data) search_query = get_search_query_from_webapp(request.preferences, request.form)
# search = Search(search_query) # without plugins # search = Search(search_query) # without plugins
search = SearchWithPlugins(search_query, request) search = SearchWithPlugins(search_query, request)
result_container = search.search() result_container = search.search()
@ -428,8 +418,8 @@ def index():
results = result_container.get_ordered_results() results = result_container.get_ordered_results()
# UI # UI
advanced_search = request.request_data.get('advanced_search', None) advanced_search = request.form.get('advanced_search', None)
output_format = request.request_data.get('format', 'html') output_format = request.form.get('format', 'html')
if output_format not in ['html', 'csv', 'json', 'rss']: if output_format not in ['html', 'csv', 'json', 'rss']:
output_format = 'html' output_format = 'html'
@ -490,7 +480,7 @@ def index():
response_rss = render( response_rss = render(
'opensearch_response_rss.xml', 'opensearch_response_rss.xml',
results=results, results=results,
q=request.request_data['q'], q=request.form['q'],
number_of_results=number_of_results, number_of_results=number_of_results,
base_url=get_base_url() base_url=get_base_url()
) )
@ -499,7 +489,7 @@ def index():
return render( return render(
'results.html', 'results.html',
results=results, results=results,
q=request.request_data['q'], q=request.form['q'],
selected_categories=search_query.categories, selected_categories=search_query.categories,
pageno=search_query.pageno, pageno=search_query.pageno,
time_range=search_query.time_range, time_range=search_query.time_range,
@ -531,7 +521,7 @@ def autocompleter():
disabled_engines = request.preferences.engines.get_disabled() disabled_engines = request.preferences.engines.get_disabled()
# parse query # parse query
raw_text_query = RawTextQuery(request.request_data.get('q', '').encode('utf-8'), disabled_engines) raw_text_query = RawTextQuery(request.form.get('q', '').encode('utf-8'), disabled_engines)
raw_text_query.parse_query() raw_text_query.parse_query()
# check if search query is set # check if search query is set
@ -564,7 +554,7 @@ def autocompleter():
results.append(raw_text_query.getFullQuery()) results.append(raw_text_query.getFullQuery())
# return autocompleter results # return autocompleter results
if request.request_data.get('format') == 'x-suggestions': if request.form.get('format') == 'x-suggestions':
return Response(json.dumps([raw_text_query.query, results]), return Response(json.dumps([raw_text_query.query, results]),
mimetype='application/json') mimetype='application/json')