Commit fa1624c0 authored by Daniel J. Ramirez's avatar Daniel J. Ramirez
Browse files

Fixed search.

Before this, selecting next page in image gallery would put you in general search again,
also fixed mixed video results on first page, that needs to be handled the same as first
page images (currently missing).
parent 31e0f8fc
......@@ -255,24 +255,25 @@ def get_search_query_from_webapp(preferences, form):
# query_engines
query_engines = raw_text_query.engines
# query_categories
query_category = form.get('category')
if query_category is None:
query_category = 'general'
# we only need to check the categories parameter set by the caller (not by the user)
# so we can assume it contains a list of categories
query_categories = form.get('categories')
def append_to_engines(cat):
for engine in categories[cat]:
# protect agains custom category provided by the user
engines = categories.get(cat)
if engines is None:
return
for engine in engines:
if (engine.name, cat) not in disabled_engines:
query_engines.append({'category': cat, 'name': engine.name})
if query_category == 'general':
append_to_engines(query_category)
append_to_engines('images')
append_to_engines('videos')
else:
append_to_engines(query_category)
# on top of the category field we have query_categories, which will be the engines to
# use to perform the search.
for category in query_categories:
append_to_engines(category)
return SearchQuery(query, query_engines, [query_category], query_lang, query_safesearch, query_pageno,
return SearchQuery(query, query_engines, query_categories, query_lang, query_safesearch, query_pageno,
query_time_range)
......
{% macro category_widget(category) -%}
<input class="hidden" type="radio" id="checkbox_{{ category|replace(' ', '_') }}" name="category" value="{{ category }}" {% if category in selected_categories %}checked="checked"{% endif %} />
<input class="hidden" type="radio" id="checkbox_{{ category|replace(' ', '_') }}" name="category" value="{{ category }}" {% if category == selected_category %}checked="checked"{% endif %} />
<label for="checkbox_{{ category|replace(' ', '_') }}">{{ _(category) }}</label>
{%- endmacro -%}
......@@ -7,7 +7,7 @@
<div id="categories" {% if rtl %} class="rtl" {% endif %}>
{% for category in categories %}
{% if category in main_categories or category in selected_categories %}
{% if category in main_categories + [selected_category] %}
{{ category_widget(category) }}
{% endif %}
{% endfor %}
......@@ -20,7 +20,7 @@
<span id="show_less_categories_" class="hidden">{{_("Less")}}</span>
<div id="more_categories">
{% for category in categories %}
{% if not (category in main_categories + ["map"] or category in selected_categories) %}
{% if not (category in main_categories + ["map"] or category == selected_category) %}
{{ category_widget(category) }}
{% endif %}
{% endfor %}
......
......@@ -11,7 +11,7 @@
{% include 'eelo/search_full.html' %}
</div>
{% set image_gallery = 'images' in selected_categories %}
{% set image_gallery = selected_category == 'images' %}
<div class="container contents {% if image_gallery %} full {% endif %}">
<div>
{% if corrections %}
......@@ -81,7 +81,7 @@
{% if paging %}
{% macro search_form_attrs(pageno) -%}
{% for category in selected_categories %}<input type="hidden" name="category_{{ category }}" value="1"/>{% endfor %}
<input type="hidden" name="category" value="{{selected_category}}">
<input type="hidden" name="q" value="{{ q|e }}" />
<input type="hidden" name="pageno" value="{{ pageno }}" />
<input type="hidden" name="time_range" value="{{ time_range }}" />
......
......@@ -477,21 +477,22 @@ def index():
'index.html',
)
selected_category = request.form.get('category') or 'general'
first_page = request.form.get('pageno')
is_general_first_page = selected_category == 'general' and (first_page is None or first_page == u'1')
images = []
videos = []
# search
search_data = None
try:
# we dont want users to select multiple categories, this simplifies the experience.
if request.form.get("categories"):
request.form["categories"] = "general"
if request.form.get("category"):
for k, v in request.form.items():
if k.startswith("category_"):
request.form.pop(k, None)
request.form["category_images"] = u"off"
request.form["category_" + request.form['category']] = u"On"
print(request.form)
if is_general_first_page:
request.form['categories'] = ['general', 'videos', 'images']
else:
request.form['categories'] = [selected_category]
search_data = search(request, settings['redis']['host'])
except Exception as e:
......@@ -504,7 +505,7 @@ def index():
else:
return index_error(), 500
if search_data.categories == ['general'] and search_data.pageno == 1:
if is_general_first_page:
result_copy = copy.copy(search_data.results)
for res in result_copy:
if res.get('category') == 'images':
......@@ -527,7 +528,7 @@ def index():
'results.html',
results=search_data.results,
q=search_data.query.decode('utf-8'),
selected_categories=search_data.categories,
selected_category=selected_category,
pageno=search_data.pageno,
time_range=search_data.time_range,
number_of_results=format_decimal(search_data.results_number),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment