[odrs-web] Add some primitive search functionality



commit 36ea01e52685ca7b9f1555aeb892d7dae31e3b7a
Author: Richard Hughes <richard hughsie com>
Date:   Wed Jul 3 16:10:57 2019 +0100

    Add some primitive search functionality

 app_data/odrs/templates/default.html |  1 +
 app_data/odrs/tests/odrs_test.py     |  9 +++++++++
 app_data/odrs/views_admin.py         | 26 +++++++++++++++++++++++++-
 3 files changed, 35 insertions(+), 1 deletion(-)
---
diff --git a/app_data/odrs/templates/default.html b/app_data/odrs/templates/default.html
index bb61a36..8a7a4a7 100644
--- a/app_data/odrs/templates/default.html
+++ b/app_data/odrs/templates/default.html
@@ -41,6 +41,7 @@
             <li><a href="{{url_for('.admin_moderator_show_all')}}">Moderators</a></li>
             <li><a href="{{url_for('.admin_distros')}}">Distributions</a></li>
             <li><a href="{{url_for('.admin_graph_month')}}">Usage</a></li>
+            <li><a href="{{url_for('.admin_search')}}">Search</a></li>
 {% endif %}
             <li><a href="{{url_for('.odrs_moderator_show', 
moderator_id=g.user.moderator_id)}}">Profile</a></li>
 {% endif %}
diff --git a/app_data/odrs/tests/odrs_test.py b/app_data/odrs/tests/odrs_test.py
index 0474cf5..c757c06 100644
--- a/app_data/odrs/tests/odrs_test.py
+++ b/app_data/odrs/tests/odrs_test.py
@@ -206,6 +206,15 @@ class OdrsTest(unittest.TestCase):
         rv = self.app.get('/admin/moderators/all')
         assert self.user_hash.encode() in rv.data, rv.data
 
+    def test_admin_search(self):
+
+        self.review_submit()
+        self.login()
+        rv = self.app.get('/admin/search?value=notgoingtoexist')
+        assert b'There are no results for this query' in rv.data, rv.data
+        rv = self.app.get('/admin/search?value=inkscape+notgoingtoexist')
+        assert b'Somebody Import' in rv.data, rv.data
+
     def test_api_submit_when_banned(self):
 
         # submit abusive review
diff --git a/app_data/odrs/views_admin.py b/app_data/odrs/views_admin.py
index b3ecc2a..cf605f2 100644
--- a/app_data/odrs/views_admin.py
+++ b/app_data/odrs/views_admin.py
@@ -11,7 +11,7 @@ import datetime
 import calendar
 from math import ceil
 
-from sqlalchemy import text
+from sqlalchemy import text, or_
 
 from flask import abort, request, flash, render_template, redirect, url_for
 from flask_login import login_required, current_user
@@ -418,6 +418,30 @@ def admin_show_all(page=1):
                            pagination=pagination,
                            reviews=reviews)
 
+def _review_filter_keys(keys):
+    cond = []
+    for key in keys:
+        cond.append(Review.user_display.like('%{}%'.format(key)))
+        cond.append(Review.summary.like('%{}%'.format(key)))
+        cond.append(Review.description.like('%{}%'.format(key)))
+    return or_(*cond)
+
+@app.route('/admin/search')
+@app.route('/admin/search/<int:max_results>')
+def admin_search(max_results=19):
+
+    # no search results
+    if 'value' not in request.args:
+        return render_template('search.html')
+
+    keys = request.args['value'].split(' ')
+    reviews = db.session.query(Review).\
+                    filter(_review_filter_keys(keys)).\
+                    order_by(Review.date_created.desc()).\
+                    limit(max_results).all()
+    return render_template('show-all.html',
+                           reviews=reviews)
+
 @app.route('/admin/show/unmoderated')
 @login_required
 def odrs_show_unmoderated():


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]