misc: don't invalidate cache when evaluating conditions (#22540)
This commit is contained in:
parent
d1023b693e
commit
a6b528ab81
|
@ -767,6 +767,32 @@ def test_form_multi_page_condition_no_visible_page(pub):
|
|||
formdef.store()
|
||||
get_app(pub).get('/test/', status=404)
|
||||
|
||||
def test_form_multi_page_many_conditions(pub):
|
||||
formdef = create_formdef()
|
||||
formdef.fields = [
|
||||
fields.PageField(id='0', label='1st page', type='page'),
|
||||
fields.PageField(id='0', label='2nd page', type='page',
|
||||
condition={'type': 'python', 'value': 'True'}),
|
||||
]
|
||||
|
||||
formdef.store()
|
||||
formdef.data_class().wipe()
|
||||
|
||||
with mock.patch('qommon.publisher.Substitutions.invalidate_cache') as invalidate_cache:
|
||||
resp = get_app(pub).get('/test/')
|
||||
call_count = invalidate_cache.call_count
|
||||
|
||||
for i in range(30):
|
||||
formdef.fields.append(fields.PageField(
|
||||
id=str(i+2), label='page %s' % (i+2), type='page',
|
||||
condition={'type': 'python', 'value': 'True'}))
|
||||
formdef.store()
|
||||
|
||||
# check the cache doesn't get invalidated for every page
|
||||
with mock.patch('qommon.publisher.Substitutions.invalidate_cache') as invalidate_cache:
|
||||
resp = get_app(pub).get('/test/')
|
||||
assert invalidate_cache.call_count == call_count
|
||||
|
||||
def test_form_multi_page_post_conditions(pub):
|
||||
formdef = create_formdef()
|
||||
formdef.fields = [fields.PageField(id='0', label='1st page', type='page'),
|
||||
|
|
|
@ -1564,6 +1564,16 @@ class PageField(Field):
|
|||
def get_substitution_variables(self):
|
||||
return form_live_data
|
||||
|
||||
def __eq__(self, other):
|
||||
# Assume all ConditionVars are equal (as they are because they
|
||||
# are created using the same live data); this avoids filling
|
||||
# the substitution sources with duplicates and invalidating its
|
||||
# cache.
|
||||
try:
|
||||
return self.__class__.__name__ == other.__class__.__name__
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
get_publisher().substitutions.feed(ConditionVars())
|
||||
data = get_publisher().substitutions.get_context_variables()
|
||||
# 2) add live data as var_ variables for local evaluation only, for
|
||||
|
|
|
@ -56,14 +56,15 @@ class Substitutions(object):
|
|||
def reset(self):
|
||||
self.sources = []
|
||||
|
||||
@invalidate_substitution_cache
|
||||
def feed(self, source):
|
||||
if source is None:
|
||||
# silently ignore a None source, this is for example useful when
|
||||
# adding the current user, as it may be None if he is not logged
|
||||
# in.
|
||||
return
|
||||
self.sources.append(source)
|
||||
if not source in self.sources:
|
||||
self.sources.append(source)
|
||||
self.invalidate_cache()
|
||||
|
||||
@classmethod
|
||||
def invalidate_cache(cls):
|
||||
|
|
Loading…
Reference in New Issue