From ec2781f240f9ef857ac63d500d057d8e149680b6 Mon Sep 17 00:00:00 2001 From: Simon Kelly Date: Tue, 13 Sep 2016 12:13:11 +0200 Subject: [PATCH] make case attachments with S3 work --- corehq/form_processor/models.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/corehq/form_processor/models.py b/corehq/form_processor/models.py index 7a8da7a21dc2..e629725baffb 100644 --- a/corehq/form_processor/models.py +++ b/corehq/form_processor/models.py @@ -408,15 +408,17 @@ class AbstractAttachment(DisabledDbMixin, models.Model, SaveStateMixin): properties = JSONField(default=dict) def write_content(self, content): + info = self._put_blob(content) + self.md5 = info.md5_hash + self.content_length = info.length + self.blob_id = info.identifier + + def _put_blob(self, content): if not self.name: raise InvalidAttachment("cannot save attachment without name") - db = get_blob_db() bucket = self._blobdb_bucket() - info = db.put(content, self.name, bucket) - self.md5 = info.md5_hash - self.content_length = info.length - self.blob_id = info.identifier + return db.put(content, self.name, bucket) def read_content(self, stream=False): db = get_blob_db() @@ -900,7 +902,12 @@ def copy_content(self, attachment): self._blobdb_bucket(), self.blob_id ) content = attachment.read_content(stream=True) - self.write_content(content) + with content: + info = self._put_blob(content) + self.blob_id = info.identifier + assert self.content_length == info.length + if self.md5 and info.md5_hash: + assert self.md5 == info.md5_hash @classmethod def from_case_update(cls, attachment):