Beispiel #1
0
def couch_blob_helper(doc, *args, **kw):
    obj = BlobHelper(doc, *args, **kw)
    get_domain = DOMAIN_MAP.get(obj.doc_type)
    if get_domain is not None:
        assert not hasattr(obj, "domain"), obj
        obj.domain = get_domain(doc)
    assert hasattr(obj, "domain"), obj.doc_type
    return obj
Beispiel #2
0
def couch_blob_helper(doc, *args, **kw):
    obj = BlobHelper(doc, *args, **kw)
    get_domain = DOMAIN_MAP.get(obj.doc_type)
    if get_domain is not None:
        assert not hasattr(obj, "domain"), obj
        obj.domain = get_domain(doc)
    elif not hasattr(obj, "domain"):
        obj.domain = None  # will trigger "unknown-domain" error
    return obj
Beispiel #3
0
 def _prepare_doc(self, doc):
     if self.load_attachments:
         obj = BlobHelper(doc, self.couchdb)
         doc["_attachments"] = {
             name: {
                 "content_type": meta["content_type"],
                 "content": obj.fetch_attachment(name),
             }
             for name, meta in doc["_attachments"].items()
         }
Beispiel #4
0
 def _do_migration(self, doc):
     obj = BlobHelper(doc, self.couchdb)
     bucket = obj._blobdb_bucket()
     assert obj.external_blobs and obj.external_blobs == obj.blobs, doc
     for name, meta in obj.blobs.iteritems():
         self.total_blobs += 1
         try:
             content = self.db.old_db.get(meta.id, bucket)
         except NotFound:
             self.not_found += 1
         else:
             with content:
                 self.db.copy_blob(content, meta.info, bucket)
     return True
Beispiel #5
0
        def return_iterator():
            yield "<restoredata>"
            for result in res['hits']['hits']:
                data_row = result['fields']

#                if data_row['script_case_id'] not in active_patients:
#                    continue
                try:
                    xml_str = (BlobHelper(data_row, db)
                        .fetch_attachment('form.xml')
                        .replace("<?xml version=\'1.0\' ?>", '')
                        .replace("<?xml version='1.0' encoding='UTF-8' ?>", ''))
                    yield xml_str
                except Exception, ex:
                    logging.error("for downloader: error fetching attachment: %s" % ex)
Beispiel #6
0
 def __init__(self, doc, database, exclude_attachments=False):
     self._attachments = {}
     self.attachments = {}
     self.database = database
     _attachments = doc.get("_attachments", None) or {}
     _attachments.update(doc.get("external_blobs", None) or {})
     if _attachments:
         if not exclude_attachments:
             self._attachments = _attachments
             obj = BlobHelper(doc, database, None)
             self.attachments = {k: obj.fetch_attachment(k) for k in _attachments}
         if doc.get("_attachments"):
             doc["_attachments"] = {}
         if "external_blobs" in doc:
             doc["external_blobs"] = {}
     self.doc = doc
     del self.doc['_rev']
Beispiel #7
0
 def _do_migration(self, doc):
     attachments = doc.pop("_attachments")
     external_blobs = doc.setdefault("external_blobs", {})
     obj = BlobHelper(doc, self.couchdb)
     try:
         with obj.atomic_blobs():
             for name, data in list(attachments.iteritems()):
                 if name in external_blobs:
                     continue  # skip attachment already in blob db
                 obj.put_attachment(name=name, **data)
     except ResourceConflict:
         # Do not migrate document if `atomic_blobs()` fails.
         # This is an unlikely state, but could happen if the
         # document is (externally) modified between when the
         # migration fetches and processes the document.
         return False
     return True
def broken_suite_files(build):
    db = Application.get_db()
    error = None
    try:
        suite = BlobHelper(build, db).fetch_attachment('files/suite.xml')
    except ResourceNotFound:
        error = 'build has no attachment files/suite.xml'
    else:
        try:
            validate_suite(suite)
        except SuiteValidationError as error:
            pass
    if error:
        yield '%s\t%s\t%s\t%s\t%s\n' % (
            build.get('built_on'),
            build.get('domain'),
            build['_id'],
            build.get('copy_of'),
            error,
        )
Beispiel #9
0
def save(transform, database):
    # this is a fancy save method because we do some special casing
    # with the attachments and with deleted documents
    def save():
        try:
            database.save_doc(transform.doc, force_update=True)
        except ResourceNotFound:
            # this is likely a document that was deleted locally that
            # you later want to copy back over there is a wacky hack
            # that you can use to handle this
            rev = get_deleted_doc_rev(database, transform.doc['_id'])
            transform.doc['_rev'] = rev
            database.save_doc(transform.doc)
    if transform.attachments:
        obj = BlobHelper(transform.doc, database)
        with obj.atomic_blobs(save):
            for name, attach in transform.attachments.items():
                content_type = transform._attachments[name]["content_type"]
                obj.put_attachment(attach, name, content_type=content_type)
    else:
        save()
Beispiel #10
0
def _get_submission_xml(xform, db):
    return BlobHelper(xform, db, CODES.form_xml).fetch_attachment('form.xml')
Beispiel #11
0
def _get_submission_xml(xform, db):
    xml = BlobHelper(xform, db).fetch_attachment('form.xml')
    if isinstance(xml, unicode):
        xml = xml.encode('utf-8')
    return xml
Beispiel #12
0
def _get_submission_xml(xform, db):
    xml = BlobHelper(xform, db, CODES.form_xml).fetch_attachment('form.xml')
    if isinstance(xml, six.text_type):
        xml = xml.encode('utf-8')
    return xml