Skip to content

Commit

Permalink
Merge pull request #646 from pretaweb/hidewhenoptimise2
Browse files Browse the repository at this point in the history
Fix getHidewhenAsJSON creating unneeded temporarydocumetns and running formulas more than once
  • Loading branch information
ebrehault committed Jul 7, 2015
2 parents e663245 + c86ddc7 commit 0102bce
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions Products/CMFPlomino/PlominoForm.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,23 +906,43 @@ def hasDynamicHidewhen(self):
return False

security.declareProtected(READ_PERMISSION,'getHidewhen')
def getHidewhen(self,REQUEST,parent_form=None,doc=None,validation_mode=False):
def getHidewhen(self,REQUEST,parent_form=None, doc=None,validation_mode=False):
""" Return a python object to dynamically show or hide hidewhens
(works only with isDynamicHidewhen)
"""
db = self.getParentDatabase()

if parent_form is None:
parent_form = self

db = parent_form.getParentDatabase()
result = {}
target = getTemporaryDocument(
db,
parent_form or self,
parent_form,
REQUEST,
doc,
validation_mode=validation_mode).__of__(db)
for hidewhen in self.getHidewhenFormulas():
validation_mode=validation_mode)

hidewhens = parent_form.getHidewhenFormulas()
for subformname in self.getSubforms(doc=target):
form = db.getForm(subformname)
if not form:
msg = 'Missing subform: %s. Referenced on: %s' % (subformname, parent_form.id)
parent_form.writeMessageOnPage(msg, REQUEST)
logger.info(msg)
continue
hidewhens += form.getHidewhenFormulas()


for hidewhen in hidewhens:
if hidewhen.id in result:
# Previously hidewhens were run in the context of their own subform. Now we
# run then all against the parent_form so there is no need to run it more than once
continue
if getattr(hidewhen, 'isDynamicHidewhen', False):
try:
isHidden = self.runFormulaScript(
SCRIPT_ID_DELIMITER.join(['hidewhen', self.id, hidewhen.id, 'formula']),
isHidden = parent_form.runFormulaScript(
SCRIPT_ID_DELIMITER.join(['hidewhen', parent_form.id, hidewhen.id, 'formula']),
target,
hidewhen.Formula)
except PlominoScriptException, e:
Expand All @@ -931,20 +951,10 @@ def getHidewhen(self,REQUEST,parent_form=None,doc=None,validation_mode=False):
#if error, we hide anyway
isHidden = True
result[hidewhen.id] = isHidden
for subformname in self.getSubforms(doc=target):
form = db.getForm(subformname)
if not form:
msg = 'Missing subform: %s. Referenced on: %s' % (subformname, self.id)
self.writeMessageOnPage(msg, self.REQUEST)
logger.info(msg)
continue
form_hidewhens = json.loads(
form.getHidewhenAsJSON(REQUEST,
parent_form=parent_form or self, doc=target,
validation_mode=validation_mode))
result.update(form_hidewhens)

return result


security.declareProtected(READ_PERMISSION, 'getHidewhenAsJSON')
def getHidewhenAsJSON(self, REQUEST, parent_form=None, doc=None, validation_mode=False):
""" Return a JSON object to dynamically show or hide hidewhens
Expand Down

0 comments on commit 0102bce

Please sign in to comment.