Handle redirection when search results are made of a single element
authorThierry Florac <thierry.florac@onf.fr>
Fri, 29 Sep 2017 16:23:09 +0200
changeset 187 a94f92bb4839
parent 186 dc1201385a36
child 188 4a7679f7f16c
Handle redirection when search results are made of a single element
src/pyams_content/shared/common/zmi/search.py
--- a/src/pyams_content/shared/common/zmi/search.py	Fri Sep 29 14:15:41 2017 +0200
+++ b/src/pyams_content/shared/common/zmi/search.py	Fri Sep 29 16:23:09 2017 +0200
@@ -45,7 +45,9 @@
 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
 from pyams_utils.list import unique
 from pyams_utils.registry import get_utility
+from pyams_utils.url import absolute_url
 from pyams_zmi.view import AdminView
+from pyramid.decorator import reify
 from pyramid.response import Response
 from pyramid.view import view_config
 from z3c.form import field
@@ -59,12 +61,19 @@
 # Quick search adapters
 #
 
-@view_config(name='quick-search.html', context=ISharedTool, request_type=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
+@view_config(name='quick-search.html', context=ISharedTool, request_type=IPyAMSLayer,
+             permission=VIEW_SYSTEM_PERMISSION, renderer='json', xhr=True)
 def shared_tool_quick_search_view(request):
     """Shared tool quick search view"""
     results = SharedToolQuickSearchResults(request.context, request)
-    results.update()
-    return Response(results.render())
+    if len(results.values) == 1:
+        result = results.values[0]
+        return {'status': 'redirect',
+                'location': absolute_url(result, request, 'admin')}
+    else:
+        results.update()
+        return {'status': 'info',
+                'content': {'html': results.render()}}
 
 
 @implementer(ISharedToolDashboardTable)