Skip to content

Commit 5a18446

Browse files
authored
Merge pull request #2295 from projectcaluma/copy_speed
fix(form: speed up copying of documents
2 parents c2706c4 + fac7a98 commit 5a18446

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

caluma/caluma_form/models.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,15 @@ def set_family(self, root_doc):
396396

397397
def copy(self, family=None, user=None):
398398
"""Create a copy including all its answers."""
399+
from caluma.caluma_form.utils import recalculate_answers_from_document
400+
401+
# defer calculated questions, as many unecessary recomputations will happen otherwise
402+
meta = dict(self.meta)
403+
meta["_defer_calculation"] = True
399404

400405
new_document = type(self).objects.create(
401406
form=self.form,
402-
meta=dict(self.meta),
407+
meta=meta,
403408
source=self,
404409
family=family,
405410
created_by_user=user.username if user else None,
@@ -416,6 +421,10 @@ def copy(self, family=None, user=None):
416421
document_family=family, to_document=new_document, user=user
417422
)
418423

424+
new_document.meta.pop("_defer_calculation", None)
425+
new_document.save()
426+
recalculate_answers_from_document(new_document)
427+
419428
return new_document
420429

421430
@cached_property

0 commit comments

Comments
 (0)