def get_attachment_by_identifier(case_id, identifier): try: return CaseAttachmentSQL.objects.raw( 'select * from get_case_attachment_by_identifier(%s, %s)', [case_id, identifier])[0] except IndexError: raise AttachmentNotFound(identifier)
def get_attachment_by_name(form_id, attachment_name): try: return XFormAttachmentSQL.objects.raw( 'select * from get_form_attachment_by_name(%s, %s)', [form_id, attachment_name])[0] except IndexError: raise AttachmentNotFound(attachment_name)
def _get_attachment_content(doc_class, doc_id, attachment_id): try: doc = doc_class.get(doc_id) resp = doc.fetch_attachment(attachment_id, stream=True) content_type = doc.blobs[attachment_id].content_type except ResourceNotFound: raise AttachmentNotFound(attachment_id) return AttachmentContent(content_type, resp)
def _get_attachment_content(doc_class, doc_id, attachment_id): try: if issubclass(doc_class, BlobMixin): doc = doc_class.get(doc_id) resp = doc.fetch_attachment(attachment_id, stream=True) content_type = doc.blobs[attachment_id].content_type else: resp = doc_class.get_db().fetch_attachment(doc_id, attachment_id, stream=True) headers = resp.resp.headers content_type = headers.get('Content-Type', None) except ResourceNotFound: raise AttachmentNotFound(attachment_id) return AttachmentContent(content_type, resp)
# NOTE `attachment_action` is a # `casexml.apps.case.xml.parser.CaseAttachmentAction` and # `attachment_action.attachments` is a dict with values of # `casexml.apps.case.xml.parser.CaseAttachment` current_attachments = self.case.case_attachments for name, att in attachment_action.attachments.items(): if att.is_delete: if name in current_attachments: self.case.track_delete(current_attachments[name]) elif att.attachment_src: form_attachment = xform.get_attachment_meta(att.attachment_src) if form_attachment is None: # Probably an improperly configured form. We need a way to # convey errors like this to domain admins. raise AttachmentNotFound( "%s: %r" % (xform.form_id, att.attachment_src)) if name in current_attachments: existing_attachment = current_attachments[name] existing_attachment.from_form_attachment( form_attachment, att.attachment_src) self.case.track_update(existing_attachment) else: new_attachment = CaseAttachmentSQL.new(att.identifier) new_attachment.from_form_attachment( form_attachment, att.attachment_src) new_attachment.case = self.case self.case.track_create(new_attachment) def _apply_close_action(self, case_update): self.case.closed = True self.case.closed_on = case_update.guess_modified_on()