From 630cc0abefbfec369e763a49688839a186e30a89 Mon Sep 17 00:00:00 2001 From: Parbhat Puri <parbhatpuri17@gmail.com> Date: Fri, 1 Nov 2019 10:48:47 +0000 Subject: [PATCH] Save file only if changes --- opentech/apply/funds/models/mixins.py | 10 ++++++++-- opentech/apply/funds/models/submissions.py | 8 +++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/opentech/apply/funds/models/mixins.py b/opentech/apply/funds/models/mixins.py index ddf20ac4b..b6cfaeba2 100644 --- a/opentech/apply/funds/models/mixins.py +++ b/opentech/apply/funds/models/mixins.py @@ -67,12 +67,18 @@ class AccessFormData: else: return cls.stream_file(instance, field, file) - def process_file_data(self, data): + def process_file_data(self, data, current_data=None): for field in self.form_fields: if isinstance(field.block, UploadableMediaBlock): file = self.process_file(self, field, data.get(field.id, [])) try: - file.save() + if current_data: + current_file = current_data.get(field.id) + # Save file only when the file is changed + if current_file != file: + file.save() + else: + file.save() except AttributeError: for f in file: f.save() diff --git a/opentech/apply/funds/models/submissions.py b/opentech/apply/funds/models/submissions.py index 97b6c9fef..669883617 100644 --- a/opentech/apply/funds/models/submissions.py +++ b/opentech/apply/funds/models/submissions.py @@ -545,9 +545,11 @@ class ApplicationSubmission( def create_revision(self, draft=False, force=False, by=None, **kwargs): # Will return True/False if the revision was created or not - self.clean_submission() current_submission = ApplicationSubmission.objects.get(id=self.id) current_data = current_submission.form_data + self.clean_submission(current_data) + self.search_data = ' '.join(self.prepare_search_values()) + if current_data != self.form_data or force: if self.live_revision == self.draft_revision: revision = ApplicationRevision.objects.create(submission=self, form_data=self.form_data, author=by) @@ -567,10 +569,10 @@ class ApplicationSubmission( return revision return None - def clean_submission(self): + def clean_submission(self, current_data=None): self.process_form_data() self.ensure_user_has_account() - self.process_file_data(self.form_data) + self.process_file_data(self.form_data, current_data) def process_form_data(self): for field_name, field_id in self.named_blocks.items(): -- GitLab