[GMG-Devel] [PATCH 26/83] Add view to handle search queries.

Alon Levy alon at pobox.com
Tue Feb 25 14:58:09 EST 2014


From: Praveen Kumar <praveen97uma at gmail.com>

---
 mediagoblin/plugins/search/views.py | 50 +++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 mediagoblin/plugins/search/views.py

diff --git a/mediagoblin/plugins/search/views.py b/mediagoblin/plugins/search/views.py
new file mode 100644
index 0000000..028c91d
--- /dev/null
+++ b/mediagoblin/plugins/search/views.py
@@ -0,0 +1,50 @@
+import logging
+
+from mediagoblin.plugins.search import forms
+from mediagoblin.plugins.search import registry
+
+from mediagoblin.tools.response import render_to_response, redirect
+
+
+_log = logging.getLogger(__name__)
+
+
+def search_in_indices(request, query):
+    indices = registry.IndexRegistry.indices()
+    all_results = []
+    for index in indices.itervalues():
+        results = index.search(query)
+        if len(results)>0:
+            for result in results:
+                obj_id = result['id']
+                obj = index.model.query.get(obj_id)
+                all_results.append({
+                    'slug': obj.slug,
+                    'url': obj.url_for_self(request.urlgen),
+                })
+    return all_results
+
+def search(request):
+    form = forms.SearchForm(request.form)
+    query = None
+    
+    context = {
+        'form': form,
+        'results': None,
+        'query': query,
+        'results_found': False,
+    }
+
+    if request.method == 'POST' and form.validate():
+        query = form.query.data
+        all_results = search_in_indices(request, query)
+        context.update({
+            'results': all_results,
+            'results_found': True,
+            'query': query,
+        })
+
+        return render_to_response(request, 'mediagoblin/search/search.html',
+                context)
+
+    render_to_response(request, 'mediagoblin/search/search.html', context)
-- 
1.8.5.3



More information about the devel mailing list