Skip to content

Commit

Permalink
Merge pull request #2295 from projectcaluma/copy_speed
Browse files Browse the repository at this point in the history
fix(form: speed up copying of documents
  • Loading branch information
Yelinz authored Sep 26, 2024
2 parents c2706c4 + fac7a98 commit 5a18446
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion caluma/caluma_form/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,15 @@ def set_family(self, root_doc):

def copy(self, family=None, user=None):
"""Create a copy including all its answers."""
from caluma.caluma_form.utils import recalculate_answers_from_document

# defer calculated questions, as many unecessary recomputations will happen otherwise
meta = dict(self.meta)
meta["_defer_calculation"] = True

new_document = type(self).objects.create(
form=self.form,
meta=dict(self.meta),
meta=meta,
source=self,
family=family,
created_by_user=user.username if user else None,
Expand All @@ -416,6 +421,10 @@ def copy(self, family=None, user=None):
document_family=family, to_document=new_document, user=user
)

new_document.meta.pop("_defer_calculation", None)
new_document.save()
recalculate_answers_from_document(new_document)

return new_document

@cached_property
Expand Down

0 comments on commit 5a18446

Please sign in to comment.