Added adapter lookup while creating search form
authorThierry Florac <tflorac@ulthar.net>
Sun, 19 Jul 2020 01:56:27 +0200
changeset 209 3f96f1c09c46
parent 208 db681bb0380b
child 210 821b37b156ac
Added adapter lookup while creating search form
src/pyams_form/search.py
--- a/src/pyams_form/search.py	Sun Jul 19 01:55:38 2020 +0200
+++ b/src/pyams_form/search.py	Sun Jul 19 01:56:27 2020 +0200
@@ -15,6 +15,7 @@
 This module provides a few helpers which can be used in search forms.
 """
 
+from pyramid.decorator import reify
 from pyramid.response import Response
 from z3c.form import button, field
 from z3c.table.interfaces import IValues
@@ -22,7 +23,7 @@
 from zope.schema import TextLine
 
 from pyams_form.form import AddForm
-from pyams_form.interfaces.form import ISearchForm, IWidgetForm
+from pyams_form.interfaces.form import ISearchForm, ISearchFormFactory, IWidgetForm
 from pyams_form.schema import ResetButton
 from pyams_skin.interfaces import IContentSearch, IInnerPage, ISearchPage
 from pyams_skin.layer import IPyAMSLayer
@@ -94,12 +95,18 @@
     """Base search view"""
 
     search_form_factory = SearchForm
-    search_form = None
+
+    @reify
+    def search_form(self):
+        form = self.request.registry.queryMultiAdapter((self.context, self.request, self),
+                                                       ISearchFormFactory)
+        if form is None:
+            form = self.search_form_factory(self.context, self.request)
+        return form
 
     def update(self):
         """Update search view"""
         # pylint: disable=no-member
-        self.search_form = self.search_form_factory(self.context, self.request)
         self.search_form.update()