Exemple #1
0
    def test_extract_form_attachment_info(self):
        image_1_name = "1234.jpg"
        image_2_name = "5678.jpg"
        form = {
            "name": "foo",
            "color": "bar",
            "image_1": image_1_name,
            "my_group": {
                "image_2": image_2_name
            }
        }
        with mock.patch.object(XFormInstanceSQL, 'form_data') as form_data_mock:
            form_data_mock.__get__ = mock.MagicMock(return_value=form)
            couch_xform = XFormInstance(
                received_on=datetime.datetime.now(),
                form=form,
                _attachments={
                    image_1_name: {
                        "content_type": "image/jpeg",
                        "length": 1024,
                    },
                    image_2_name: {
                        "content_type": "image/jpeg",
                        "length": 2048,
                    },
                    "form.xml": {
                        "content_type": "text/xml",
                        "length": 2048,
                    }
                }
            )
            sql_xform = XFormInstanceSQL(
                received_on=datetime.datetime.now(),
            )
            sql_xform.unsaved_attachments = [
                XFormAttachmentSQL(
                    name=image_1_name,
                    content_type="image/jpeg",
                    content_length=1024,
                ),
                XFormAttachmentSQL(
                    name=image_2_name,
                    content_type="image/jpeg",
                    content_length=1024,
                ),
                XFormAttachmentSQL(
                    name="form.xml",
                    content_type="text/xml",
                    content_length=1024,
                ),
            ]

            for xform in (couch_xform, sql_xform):
                form_info = _extract_form_attachment_info(xform, {"my_group-image_2", "image_1"})
                attachments = {a['name']: a for a in form_info['attachments']}
                self.assertTrue(image_1_name in attachments)
                self.assertTrue(image_2_name in attachments)
                self.assertEqual(attachments[image_1_name]['question_id'], "image_1")
                self.assertEqual(attachments[image_2_name]['question_id'], "my_group-image_2")
Exemple #2
0
    def store_attachments(cls, xform, attachments):
        xform_attachments = []
        for attachment in attachments:
            xform_attachment = XFormAttachmentSQL(
                name=attachment.name,
                attachment_id=uuid.uuid4(),
                content_type=attachment.content_type,
                md5=attachment.md5,
            )
            xform_attachment.write_content(attachment.content)
            xform_attachments.append(xform_attachment)

        xform.unsaved_attachments = xform_attachments
Exemple #3
0
    def store_attachments(cls, xform, attachments):
        xform_attachments = []
        for attachment in attachments:
            xform_attachment = XFormAttachmentSQL(
                name=attachment.name,
                attachment_uuid=unicode(uuid.uuid4()),
                content_type=attachment.content_type,
                md5=hashlib.md5(attachment.content).hexdigest(),
            )
            xform_attachment.write_content(attachment.content)
            xform_attachments.append(xform_attachment)

        xform.unsaved_attachments = xform_attachments
Exemple #4
0
    def store_attachments(cls, xform, attachments):
        xform_attachments = []
        for attachment in attachments:
            xform_attachment = XFormAttachmentSQL(
                name=attachment.name,
                attachment_id=uuid.uuid4(),
                content_type=attachment.content_type,
            )
            xform_attachment.write_content(attachment.content_as_file())
            if xform_attachment.is_image:
                img_size = Image.open(attachment.content_as_file()).size
                xform_attachment.properties = dict(width=img_size[0], height=img_size[1])
            xform_attachments.append(xform_attachment)

        xform.unsaved_attachments = xform_attachments
Exemple #5
0
 def copy_attachments(cls, from_form, to_form):
     to_form.unsaved_attachments = getattr(to_form, 'unsaved_attachments', [])
     for name, att in from_form.attachments.items():
         to_form.unsaved_attachments.append(XFormAttachmentSQL(
             name=att.name,
             attachment_id=uuid.uuid4(),
             content_type=att.content_type,
             content_length=att.content_length,
             properties=att.properties,
             blob_id=att.blob_id,
             blob_bucket=att.blobdb_bucket(),
             md5=att.md5,
         ))
def _migrate_form_attachments(sql_form, couch_form):
    """Copy over attachment meta - includes form.xml"""
    attachments = []
    for name, blob in six.iteritems(couch_form.blobs):
        attachments.append(
            XFormAttachmentSQL(name=name,
                               form=sql_form,
                               attachment_id=uuid.uuid4().hex,
                               content_type=blob.content_type,
                               content_length=blob.content_length,
                               blob_id=blob.id,
                               blob_bucket=couch_form._blobdb_bucket(),
                               md5=blob.info.md5_hash))
    sql_form.unsaved_attachments = attachments
Exemple #7
0
def _migrate_form_attachments(sql_form, couch_form):
    """Copy over attachment meta - includes form.xml"""
    attachments = []
    for name, blob in six.iteritems(couch_form.blobs):
        attachments.append(
            XFormAttachmentSQL(
                name=name,
                form=sql_form,
                attachment_id=uuid.uuid4().hex,
                content_type=blob.content_type,
                content_length=blob.content_length,
                blob_id=blob.key,
                blob_bucket="",  # temporary/special value -> new blob db API
                md5=blob.info.md5_hash or "unknown-md5",
            ))
    sql_form.unsaved_attachments = attachments
def copy_attachments(from_form, to_form):
    to_form_attachments = to_form.attachments
    for name, att in from_form.attachments.items():
        if name in to_form_attachments:
            # populate missing fields
            print(('updating attachment, name, ', name, 'form_id, ',
                   to_form.form_id))
            att_new = to_form_attachments[name]
            att_new.content_length = att.content_length
            att_new.blob_bucket = att.blobdb_bucket()
            att_new.save()
        else:
            print(('creating attachment, name, ', name, 'form_id, ',
                   to_form.form_id))
            XFormAttachmentSQL(name=att.name,
                               attachment_id=uuid.uuid4(),
                               content_type=att.content_type,
                               content_length=att.content_length,
                               properties=att.properties,
                               blob_id=att.blob_id,
                               blob_bucket=att.blobdb_bucket(),
                               md5=att.md5,
                               form_id=to_form.form_id).save()