incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [2/4] git commit: [#4504] ticket:311 Replace $USER with actual username in a search query
Date Fri, 12 Apr 2013 17:08:57 GMT
[#4504] ticket:311 Replace $USER with actual username in a search query


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/f8c30d49
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/f8c30d49
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/f8c30d49

Branch: refs/heads/master
Commit: f8c30d4909cbadd3c27c64867ae78d0888e8cd19
Parents: 6f04547
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Tue Apr 9 14:45:13 2013 +0000
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Fri Apr 12 17:07:49 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/search.py         |    7 +++++++
 Allura/allura/tests/test_helpers.py |   13 +++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f8c30d49/Allura/allura/lib/search.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/search.py b/Allura/allura/lib/search.py
index a428c53..89d1d6e 100644
--- a/Allura/allura/lib/search.py
+++ b/Allura/allura/lib/search.py
@@ -22,7 +22,14 @@ def solarize(obj):
 class SearchError(SolrError):
     pass
 
+def inject_user(q, user=None):
+    '''Replace $USER with current user's name.'''
+    if user is None:
+        user = c.user
+    return q.replace('$USER', user.username) if q else q
+
 def search(q,short_timeout=False,ignore_errors=True,**kw):
+    q = inject_user(q)
     try:
         if short_timeout:
             return g.solr_short_timeout.search(q, **kw)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f8c30d49/Allura/allura/tests/test_helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index 2e80b56..8a7065f 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -7,6 +7,7 @@ from nose.tools import eq_, assert_equals
 
 from allura import model as M
 from allura.lib import helpers as h
+from allura.lib.search import inject_user
 from allura.tests import decorators as td
 from alluratest.controller import setup_basic_test
 
@@ -185,3 +186,15 @@ def test_get_first():
     assert_equals(h.get_first({'title': ['Value']}, 'title'), 'Value')
     assert_equals(h.get_first({'title': []}, 'title'), None)
     assert_equals(h.get_first({'title': ['Value']}, 'title'), 'Value')
+
+@patch('allura.lib.search.c')
+def test_inject_user(context):
+    user = Mock(username='user01')
+    assert_equals(inject_user(None, user), None)
+    assert_equals(inject_user('', user), '')
+    assert_equals(inject_user('query', user), 'query')
+    result = inject_user('reported_by_s:$USER OR assigned_to_s:$USER', user)
+    assert_equals(result, 'reported_by_s:user01 OR assigned_to_s:user01')
+    context.user = Mock(username='admin1')
+    result = inject_user('reported_by_s:$USER OR assigned_to_s:$USER')
+    assert_equals(result, 'reported_by_s:admin1 OR assigned_to_s:admin1')


Mime
View raw message