From 40724f6091ab489b9642a9c61720e831f18474fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 16 Apr 2019 09:33:00 +0200
Subject: [PATCH 3/3] misc: add site option to force lazy evaluation (#29951)

---
 tests/test_form_pages.py   |  5 +++--
 tests/utilities.py         | 19 +++++++++++++------
 wcs/qommon/substitution.py |  4 +++-
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py
index af5ae8959..0a703aa28 100644
--- a/tests/test_form_pages.py
+++ b/tests/test_form_pages.py
@@ -58,13 +58,14 @@ def assert_equal_zip(stream1, stream2):
 
 def pytest_generate_tests(metafunc):
     if 'pub' in metafunc.fixturenames:
-        metafunc.parametrize('pub', ['pickle', 'sql', 'pickle-templates'], indirect=True)
+        metafunc.parametrize('pub', ['pickle', 'sql', 'pickle-templates', 'pickle-lazy'], indirect=True)
 
 @pytest.fixture
 def pub(request, emails):
     pub = create_temporary_pub(
             sql_mode=bool('sql' in request.param),
-            templates_mode=bool('templates' in request.param)
+            templates_mode=bool('templates' in request.param),
+            lazy_mode=bool('lazy' in request.param),
             )
     pub.cfg['identification'] = {'methods': ['password']}
     pub.cfg['language'] = {'language': 'en'}
diff --git a/tests/utilities.py b/tests/utilities.py
index 615f799b8..139404dc2 100644
--- a/tests/utilities.py
+++ b/tests/utilities.py
@@ -37,10 +37,11 @@ class KnownElements(object):
     sql_app_dir = None
     sql_db_name = None
     templates_app_dir = None
+    lazy_app_dir = None
 
 known_elements = KnownElements()
 
-def create_temporary_pub(sql_mode=False, templates_mode=False):
+def create_temporary_pub(sql_mode=False, templates_mode=False, lazy_mode=False):
     if sql_mode is True:
         if pytest.config.getoption('without_postgresql_tests'):
             pytest.skip("unsupported configuration")
@@ -50,17 +51,21 @@ def create_temporary_pub(sql_mode=False, templates_mode=False):
         cleanup()
     if templates_mode and known_elements.templates_app_dir:
         APP_DIR = known_elements.templates_app_dir
-    elif (not sql_mode and not templates_mode) and known_elements.pickle_app_dir:
-        APP_DIR = known_elements.pickle_app_dir
-    elif sql_mode is True and known_elements.sql_app_dir:
+    elif lazy_mode and known_elements.lazy_app_dir:
+        APP_DIR = known_elements.lazy_app_dir
+    elif sql_mode and known_elements.sql_app_dir:
         APP_DIR = known_elements.sql_app_dir
+    elif not (templates_mode or lazy_mode or sql_mode) and known_elements.pickle_app_dir:
+        APP_DIR = known_elements.pickle_app_dir
     else:
         APP_DIR = tempfile.mkdtemp()
         if templates_mode:
             known_elements.templates_app_dir = APP_DIR
-        elif sql_mode is True:
+        elif lazy_mode:
+            known_elements.lazy_app_dir = APP_DIR
+        elif sql_mode:
             known_elements.sql_app_dir = APP_DIR
-        elif sql_mode is False:
+        else:
             known_elements.pickle_app_dir = APP_DIR
 
     compat.CompatWcsPublisher.APP_DIR = APP_DIR
@@ -105,6 +110,8 @@ def create_temporary_pub(sql_mode=False, templates_mode=False):
     fd.write('formdef-captcha-option = true\n')
     fd.write('formdef-appearance-keywords = true\n')
     fd.write('workflow-resubmit-action = true\n')
+    if lazy_mode:
+        fd.write('force-lazy-mode = true\n')
     if sql_mode:
         fd.write('postgresql = true\n')
 
diff --git a/wcs/qommon/substitution.py b/wcs/qommon/substitution.py
index 51c8472b3..df9f9b9e5 100644
--- a/wcs/qommon/substitution.py
+++ b/wcs/qommon/substitution.py
@@ -104,7 +104,9 @@ class Substitutions(object):
     def get_context_variables(self, mode=None):
         if self._forced_mode:
             mode = self._forced_mode
-        lazy = mode in get_publisher().get_lazy_variables_modes() if mode else False
+        lazy = get_publisher().has_site_option('force-lazy-mode')
+        if not lazy and mode:
+            lazy = mode in get_publisher().get_lazy_variables_modes()
         d = getattr(self, '_cache_context_variables%r' % lazy, None)
         if d is not None:
             return d
-- 
2.20.1

