def _apply_attachments_action(self, attachment_action, xform=None): """ if xform is provided it will be used to fetch attachments """ # the actions and attachment must be added before the first saves can happen # todo attach cached attachment info def fetch_attachment(name): if fetch_attachment.form is None: fetch_attachment.form = XFormInstance.get(attachment_action.xform_id) return fetch_attachment.form.fetch_attachment(name) fetch_attachment.form = xform attach_dict = {} # cache all attachment streams from xform for k, v in attachment_action.attachments.items(): if v.is_present: # fetch attachment, update metadata, get the stream attach_data = fetch_attachment(v.attachment_src) attach_dict[k] = attach_data v.attachment_size = len(attach_data) if v.is_image: img = Image.open(StringIO(attach_data)) img_size = img.size props = dict(width=img_size[0], height=img_size[1]) v.attachment_properties = props update_attachments = {} for k, v in self.case.case_attachments.items(): if v.is_present: update_attachments[k] = v if self.case._attachments: attachment_builder = CouchAttachmentsBuilder( self.case['_attachments']) else: attachment_builder = CouchAttachmentsBuilder() for k, v in attachment_action.attachments.items(): # grab xform_attachments # copy over attachments from form onto case update_attachments[k] = v if v.is_present: #fetch attachment from xform identifier = v.identifier attach = attach_dict[identifier] attachment_builder.add(name=k, content=attach, content_type=v.server_mime) else: try: attachment_builder.remove(k) except KeyError: pass del update_attachments[k] self.case._attachments = attachment_builder.to_json() self.case.case_attachments = update_attachments
def _apply_attachments_action(self, attachment_action, xform=None): """ if xform is provided it will be used to fetch attachments """ # the actions and attachment must be added before the first saves can happen # todo attach cached attachment info def fetch_attachment(name): if fetch_attachment.form is None: fetch_attachment.form = XFormInstance.get(attachment_action.xform_id) return fetch_attachment.form.fetch_attachment(name) fetch_attachment.form = xform attach_dict = {} # cache all attachment streams from xform for k, v in attachment_action.attachments.items(): if v.is_present: # fetch attachment, update metadata, get the stream attach_data = fetch_attachment(v.attachment_src) attach_dict[k] = attach_data v.attachment_size = len(attach_data) if v.is_image: img = Image.open(StringIO(attach_data)) img_size = img.size props = dict(width=img_size[0], height=img_size[1]) v.attachment_properties = props update_attachments = {} for k, v in self.case.case_attachments.items(): if v.is_present: update_attachments[k] = v if self.case._attachments: attachment_builder = CouchAttachmentsBuilder( self.case['_attachments']) else: attachment_builder = CouchAttachmentsBuilder() for k, v in attachment_action.attachments.items(): # grab xform_attachments # copy over attachments from form onto case update_attachments[k] = v if v.is_present: #fetch attachment from xform identifier = v.identifier attach = attach_dict[identifier] attachment_builder.add(name=k, content=attach, content_type=v.server_mime) else: try: attachment_builder.remove(k) except KeyError: pass del update_attachments[k] self.case._attachments = attachment_builder.to_json() self.case.case_attachments = update_attachments
def _apply_attachments_action(self, attachment_action, xform=None): """ if xform is provided, attachments will be looked for in the xform's _attachments. They should be base64 encoded under _attachments[name]['data'] """ # the actions and _attachment must be added before the first saves can happen # todo attach cached attachment info def fetch_attachment(name): if xform and 'data' in xform._attachments[name]: assert xform.form_id == attachment_action.xform_id return base64.b64decode(xform._attachments[name]['data']) else: return XFormInstance.get_db().fetch_attachment(attachment_action.xform_id, name) stream_dict = {} # cache all attachment streams from xform for k, v in attachment_action.attachments.items(): if v.is_present: # fetch attachment, update metadata, get the stream attach_data = fetch_attachment(v.attachment_src) stream_dict[k] = attach_data v.attachment_size = len(attach_data) if v.is_image: img = Image.open(StringIO(attach_data)) img_size = img.size props = dict(width=img_size[0], height=img_size[1]) v.attachment_properties = props update_attachments = {} for k, v in self.case.case_attachments.items(): if v.is_present: update_attachments[k] = v if self.case._attachments: attachment_builder = CouchAttachmentsBuilder( self.case['_attachments']) else: attachment_builder = CouchAttachmentsBuilder() for k, v in attachment_action.attachments.items(): # grab xform_attachments # copy over attachments from form onto case update_attachments[k] = v if v.is_present: #fetch attachment from xform attachment_key = v.attachment_key attach = stream_dict[attachment_key] attachment_builder.add(name=k, content=attach, content_type=v.server_mime) else: try: attachment_builder.remove(k) except KeyError: pass del update_attachments[k] self.case._attachments = attachment_builder.to_json() self.case.case_attachments = update_attachments
def apply_attachments(self, attachment_action, xform=None): """ if xform is provided, attachments will be looked for in the xform's _attachments. They should be base64 encoded under _attachments[name]['data'] """ # the actions and _attachment must be added before the first saves can happen # todo attach cached attachment info def fetch_attachment(name): if xform and 'data' in xform._attachments[name]: assert xform._id == attachment_action.xform_id return base64.b64decode(xform._attachments[name]['data']) else: return XFormInstance.get_db().fetch_attachment( attachment_action.xform_id, name) stream_dict = {} # cache all attachment streams from xform for k, v in attachment_action.attachments.items(): if v.is_present: # fetch attachment, update metadata, get the stream attach_data = fetch_attachment(v.attachment_src) stream_dict[k] = attach_data v.attachment_size = len(attach_data) if v.is_image: img = Image.open(StringIO(attach_data)) img_size = img.size props = dict(width=img_size[0], height=img_size[1]) v.attachment_properties = props update_attachments = {} for k, v in self.case_attachments.items(): if v.is_present: update_attachments[k] = v if self._attachments: attachment_builder = CouchAttachmentsBuilder(self['_attachments']) else: attachment_builder = CouchAttachmentsBuilder() for k, v in attachment_action.attachments.items(): # grab xform_attachments # copy over attachments from form onto case update_attachments[k] = v if v.is_present: #fetch attachment from xform attachment_key = v.attachment_key attach = stream_dict[attachment_key] attachment_builder.add(name=k, content=attach, content_type=v.server_mime) else: try: attachment_builder.remove(k) except KeyError: pass del update_attachments[k] self._attachments = attachment_builder.to_json() self.case_attachments = update_attachments