Пример #1
0
 def handle(self, *args, **options):
     
     if len(args) != 2:
         raise CommandError('Usage is copy_domain %s' % self.args)
     
     sourcedb = Database(args[0])
     domain = args[1].strip()
     
     all_docs = sourcedb.view("domain/docs", startkey=[domain], 
                              endkey=[domain, {}], reduce=False)
     
     total = len(all_docs)
     count = 0
     targetdb = get_db()
     print "found %s matching documents in domain: %s" % (total, domain)
     for row in all_docs:
         try:
             count += 1
             dt = DocumentTransform(sourcedb.get(row["id"]), sourcedb)
             save(dt, targetdb)
             print "Synced %s/%s docs (%s: %s)" % (count, total, row["key"][1], row["id"])
         except Exception, e:
             print "Document %s failed! Error is: %s" % (row["id"], e)
Пример #2
0
class CouchDBDocStorage(Storage):
    """
    CouchDBDocStorage - a Django Storage class for CouchDB.

    Uses the couchdb url for the database you want to connect to
    """


    def __init__(self, **kwargs):
        self.couchdb_url = kwargs.get('db_url', settings.COUCH_DATABASE)
        self.db = Database(self.couchdb_url)

    def _put_file(self, name, content):
        doc_id, attachment_key=_split_file(name)
        self.db.put_attachment(doc_id, content, name=attachment_key)
        return doc_id

    def get_document(self, doc_id):
        return self.db.get(doc_id)

    def _open(self, name, mode='rb'):
        doc_id, attachment_key=_split_file(name)
        couchdb_file = CouchDBAttachmentFile(doc_id, attachment_key, self, mode=mode)
        return couchdb_file

    def _save(self, name, content):
        doc_id, attachment_key=_split_file(name)
        content.open()
        if hasattr(content, 'chunks'):
            content_str = ''.join(chunk for chunk in content.chunks())
        else:
            content_str = content.read()
        #name = name.replace('/', '-')
        return self._put_file(name, content_str)

    def exists(self, name):
        doc_id, attachment_key=_split_file(name)
        if self.db.doc_exist(doc_id):
            return self.db.open_doc(doc_id)['_attachments'].has_key(attachment_key)
        else:
            return False

    def size(self, name):
        doc_id, attachment_key=_split_file(name)
        doc = self.get_document(doc_id)
        if doc:
            return doc['_attachments'][attachment_key]['length']
        return 0

    def url(self, name):
        doc_id, attachment_key=_split_file(name)
#        return urljoin(self.base_url,
#                       os.path.join(quote_plus(self.db.name),
#                       quote_plus(name),
#                       'content'))
        return reverse('hutch.views.image_proxy', kwargs={'doc_id': doc_id, 'attachment_key': attachment_key})

    def delete(self, name):
        doc_id, attachment_key=_split_file(name)
        try:
            doc = self.get_document(doc_id)
            del doc['_attachments'][attachment_key]
        except Exception, ex:
            print "Ex: %s" % ex
            raise IOError("File not found: %s" % name)