Merge pull request #804 from dalf/flask_perimeter

[mod] searx uses flask framework only in webapp.py
This commit is contained in:
Adam Tauber 2017-01-27 15:28:01 +01:00 committed by GitHub
commit 3ecde5bc3a
4 changed files with 21 additions and 24 deletions

View File

@ -63,9 +63,9 @@ class PluginStore():
plugin.id = plugin.name.replace(' ', '_') plugin.id = plugin.name.replace(' ', '_')
self.plugins.append(plugin) self.plugins.append(plugin)
def call(self, plugin_type, request, *args, **kwargs): def call(self, ordered_plugin_list, plugin_type, request, *args, **kwargs):
ret = True ret = True
for plugin in request.user_plugins: for plugin in ordered_plugin_list:
if hasattr(plugin, plugin_type): if hasattr(plugin, plugin_type):
ret = getattr(plugin, plugin_type)(request, *args, **kwargs) ret = getattr(plugin, plugin_type)(request, *args, **kwargs)
if not ret: if not ret:

View File

@ -414,19 +414,20 @@ class SearchWithPlugins(Search):
"""Similar to the Search class but call the plugins.""" """Similar to the Search class but call the plugins."""
def __init__(self, search_query, request): def __init__(self, search_query, ordered_plugin_list, request):
super(SearchWithPlugins, self).__init__(search_query) super(SearchWithPlugins, self).__init__(search_query)
self.ordered_plugin_list = ordered_plugin_list
self.request = request self.request = request
def search(self): def search(self):
if plugins.call('pre_search', self.request, self): if plugins.call(self.ordered_plugin_list, 'pre_search', self.request, self):
super(SearchWithPlugins, self).search() super(SearchWithPlugins, self).search()
plugins.call('post_search', self.request, self) plugins.call(self.ordered_plugin_list, 'post_search', self.request, self)
results = self.result_container.get_ordered_results() results = self.result_container.get_ordered_results()
for result in results: for result in results:
plugins.call('on_result', self.request, self, result) plugins.call(self.ordered_plugin_list, 'on_result', self.request, self, result)
return self.result_container return self.result_container

View File

@ -456,7 +456,7 @@ def index():
try: try:
search_query = get_search_query_from_webapp(request.preferences, request.form) 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.user_plugins, request)
result_container = search.search() result_container = search.search()
except Exception as e: except Exception as e:
# log exception # log exception

View File

@ -28,14 +28,12 @@ class PluginStoreTest(SearxTestCase):
testplugin = plugins.Plugin() testplugin = plugins.Plugin()
store.register(testplugin) store.register(testplugin)
setattr(testplugin, 'asdf', Mock()) setattr(testplugin, 'asdf', Mock())
request = Mock(user_plugins=[]) request = Mock()
store.call('asdf', request, Mock()) store.call([], 'asdf', request, Mock())
self.assertFalse(testplugin.asdf.called) self.assertFalse(testplugin.asdf.called)
request.user_plugins.append(testplugin) store.call([testplugin], 'asdf', request, Mock())
store.call('asdf', request, Mock())
self.assertTrue(testplugin.asdf.called) self.assertTrue(testplugin.asdf.called)
@ -48,42 +46,40 @@ class SelfIPTest(SearxTestCase):
self.assertTrue(len(store.plugins) == 1) self.assertTrue(len(store.plugins) == 1)
# IP test # IP test
request = Mock(user_plugins=store.plugins, request = Mock(remote_addr='127.0.0.1')
remote_addr='127.0.0.1')
request.headers.getlist.return_value = [] request.headers.getlist.return_value = []
search = get_search_mock(query='ip', pageno=1) search = get_search_mock(query='ip', pageno=1)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertTrue('127.0.0.1' in search.result_container.answers) self.assertTrue('127.0.0.1' in search.result_container.answers)
search = get_search_mock(query='ip', pageno=2) search = get_search_mock(query='ip', pageno=2)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertFalse('127.0.0.1' in search.result_container.answers) self.assertFalse('127.0.0.1' in search.result_container.answers)
# User agent test # User agent test
request = Mock(user_plugins=store.plugins, request = Mock(user_agent='Mock')
user_agent='Mock')
request.headers.getlist.return_value = [] request.headers.getlist.return_value = []
search = get_search_mock(query='user-agent', pageno=1) search = get_search_mock(query='user-agent', pageno=1)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertTrue('Mock' in search.result_container.answers) self.assertTrue('Mock' in search.result_container.answers)
search = get_search_mock(query='user-agent', pageno=2) search = get_search_mock(query='user-agent', pageno=2)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertFalse('Mock' in search.result_container.answers) self.assertFalse('Mock' in search.result_container.answers)
search = get_search_mock(query='user-agent', pageno=1) search = get_search_mock(query='user-agent', pageno=1)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertTrue('Mock' in search.result_container.answers) self.assertTrue('Mock' in search.result_container.answers)
search = get_search_mock(query='user-agent', pageno=2) search = get_search_mock(query='user-agent', pageno=2)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertFalse('Mock' in search.result_container.answers) self.assertFalse('Mock' in search.result_container.answers)
search = get_search_mock(query='What is my User-Agent?', pageno=1) search = get_search_mock(query='What is my User-Agent?', pageno=1)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertTrue('Mock' in search.result_container.answers) self.assertTrue('Mock' in search.result_container.answers)
search = get_search_mock(query='What is my User-Agent?', pageno=2) search = get_search_mock(query='What is my User-Agent?', pageno=2)
store.call('post_search', request, search) store.call(store.plugins, 'post_search', request, search)
self.assertFalse('Mock' in search.result_container.answers) self.assertFalse('Mock' in search.result_container.answers)