Use request header to store new thesaurus theme
authorThierry Florac <tflorac@ulthar.net>
Fri, 22 Mar 2019 17:46:49 +0100
changeset 1296 38e2977b726b
parent 1295 6aded60a6fe7
child 1297 393e4f1812e1
Use request header to store new thesaurus theme
src/pyams_content/component/theme/zmi/manager.py
--- a/src/pyams_content/component/theme/zmi/manager.py	Fri Mar 22 12:46:49 2019 +0100
+++ b/src/pyams_content/component/theme/zmi/manager.py	Fri Mar 22 17:46:49 2019 +0100
@@ -16,11 +16,10 @@
 from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
 from zope.interface import alsoProvides
 
-from pyams_content.component.theme.interfaces import \
-    ITagsManagerTarget, ITagsManager, \
-    IThemesManagerTarget, IThemesManager, \
-    ICollectionsManagerTarget, ICollectionsManager
-from pyams_content.interfaces import MANAGE_TOOL_PERMISSION, MANAGE_SITE_ROOT_PERMISSION
+from pyams_content import _
+from pyams_content.component.theme.interfaces import ICollectionsManager, ICollectionsManagerTarget, ITagsManager, \
+    ITagsManagerTarget, IThemesManager, IThemesManagerTarget
+from pyams_content.interfaces import MANAGE_SITE_ROOT_PERMISSION, MANAGE_TOOL_PERMISSION
 from pyams_content.zmi import pyams_content
 from pyams_form.form import ajax_config
 from pyams_form.group import NamedWidgetsGroup
@@ -34,8 +33,6 @@
 from pyams_zmi.interfaces.menu import IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
 
-from pyams_content import _
-
 
 #
 # Tags management view
@@ -73,20 +70,20 @@
         return ITagsManager(self.context)
 
     def updateWidgets(self, prefix=None):
+        # We have to store thesaurus name if request header to be able to set extract name correctly
+        # See :ref:`pyams_thesaurus.thesaurus.ThesaurusExtractsVocabulary`
+        if self.request.method == 'POST':
+            param_name = '{}widgets.thesaurus_name:list'.format(self.prefix)
+            param_value = self.request.params.get(param_name)
+            if param_value is not None:
+                self.request.headers['X-Thesaurus-Name'] = param_value
+
         super(TagsManagerEditForm, self).updateWidgets(prefix)
         widget = self.widgets['thesaurus_name']
         widget.object_data = {
             'ams-plugins': 'pyams_content',
             'ams-plugin-pyams_content-src': get_resource_path(pyams_content),
-            'ams-plugin-pyams_content-callback': 'PyAMS_content.themes.initExtracts',
-            'ams-plugin-pyams_content-async': 'false'
-        }
-        alsoProvides(widget, IObjectData)
-        widget = self.widgets['extract_name']
-        widget.object_data = {
-            'ams-events-handlers': {
-                'select2-open': 'PyAMS_content.themes.getExtracts'
-            }
+            'ams-change-handler': 'PyAMS_content.themes.updateThesaurus'
         }
         alsoProvides(widget, IObjectData)
 
@@ -140,20 +137,20 @@
         return IThemesManager(self.context)
 
     def updateWidgets(self, prefix=None):
+        # We have to store thesaurus name if request header to be able to set extract name correctly
+        # See :ref:`pyams_thesaurus.thesaurus.ThesaurusExtractsVocabulary`
+        if self.request.method == 'POST':
+            param_name = '{}widgets.thesaurus_name:list'.format(self.prefix)
+            param_value = self.request.params.get(param_name)
+            if param_value is not None:
+                self.request.headers['X-Thesaurus-Name'] = param_value
+
         super(ThemesManagerEditForm, self).updateWidgets(prefix)
         widget = self.widgets['thesaurus_name']
         widget.object_data = {
             'ams-plugins': 'pyams_content',
             'ams-plugin-pyams_content-src': get_resource_path(pyams_content),
-            'ams-plugin-pyams_content-callback': 'PyAMS_content.themes.initExtracts',
-            'ams-plugin-pyams_content-async': 'false'
-        }
-        alsoProvides(widget, IObjectData)
-        widget = self.widgets['extract_name']
-        widget.object_data = {
-            'ams-events-handlers': {
-                'select2-open': 'PyAMS_content.themes.getExtracts'
-            }
+            'ams-change-handler': 'PyAMS_content.themes.updateThesaurus'
         }
         alsoProvides(widget, IObjectData)
 
@@ -190,19 +187,19 @@
         return ICollectionsManager(self.context)
 
     def updateWidgets(self, prefix=None):
+        # We have to store thesaurus name if request header to be able to set extract name correctly
+        # See :ref:`pyams_thesaurus.thesaurus.ThesaurusExtractsVocabulary`
+        if self.request.method == 'POST':
+            param_name = '{}widgets.thesaurus_name:list'.format(self.prefix)
+            param_value = self.request.params.get(param_name)
+            if param_value is not None:
+                self.request.headers['X-Thesaurus-Name'] = param_value
+
         super(CollectionsManagerEditForm, self).updateWidgets(prefix)
         widget = self.widgets['thesaurus_name']
         widget.object_data = {
             'ams-plugins': 'pyams_content',
             'ams-plugin-pyams_content-src': get_resource_path(pyams_content),
-            'ams-plugin-pyams_content-callback': 'PyAMS_content.collections.initExtracts',
-            'ams-plugin-pyams_content-async': 'false'
+            'ams-change-handler': 'PyAMS_content.themes.updateThesaurus'
         }
         alsoProvides(widget, IObjectData)
-        widget = self.widgets['extract_name']
-        widget.object_data = {
-            'ams-events-handlers': {
-                'select2-open': 'PyAMS_content.themes.getExtracts'
-            }
-        }
-        alsoProvides(widget, IObjectData)