from couchdbkit import push
from couchdbkit.exceptions import ResourceNotFound


def sync_design_docs(db, design_dir, design_name, temp=None):
    """
    pushes design documents and brings new index up to date if temp

    for example it can be used to push new changes to the myapp design doc to
    _design/myapp-tmp

    and then trigger an index of _design/myapp-tmp

    """
    design_name_ = '%s-%s' % (design_name, temp) if temp else design_name
    docid = "_design/%s" % design_name_
    push(design_dir, db, force=True, docid=docid)
    print "synced '%s' in couchdb" % design_name
    if temp:
        # found in the innards of couchdbkit
        try:
            view_names = db[docid].get('views', {}).keys()
            if len(view_names) > 0:
                print 'Triggering view rebuild'
                view = '%s/%s' % (design_name_, view_names[0])
                list(db.view(view, limit=0))
        except Exception, ex:
            print "\tError trying to sync couchapp %s, but ignoring %s" % (docid, ex)


def copy_designs(db, design_name, temp='tmp', delete=True):
    print "Copy prepared design docs for '%s' in couchdb" % design_name
    tmp_name = '%s-%s' % (design_name, temp)
    from_id = '_design/%s' % tmp_name
    to_id = '_design/%s' % design_name
    try:
        db.copy_doc(from_id, to_id)
        if delete:
            del db[from_id]

    except ResourceNotFound:
        print '%s not found.' % (from_id, )
@@ -1,20 +1,30 @@
+import os%0A
 from couchdbkit impo
@@ -82,16 +82,104 @@
 otFound%0A
+from couchdbkit.ext.django.loading import couchdbkit_handler%0Aimport sys%0Aimport settings%0A
 %0A%0Adef sy
@@ -1501,16 +1501,16 @@
 tFound:%0A
-
         
@@ -1544,8 +1544,1498 @@
 om_id, )
+%0A%0A%0Adef sync(app, verbosity=2, temp=None):%0A    %22%22%22%0A    All of this is copied from couchdbkit.ext.django.loading%0A%0A    but replaces%0A    %22%22%22%0A    app_name = app.__name__.rsplit('.', 1)%5B0%5D%0A    app_labels = set()%0A    schema_list = couchdbkit_handler.app_schema.values()%0A    for schema_dict in schema_list:%0A        for schema in schema_dict.values():%0A            app_module = schema.__module__.rsplit(%22.%22, 1)%5B0%5D%0A            if app_module == app_name and not schema._meta.app_label in app_labels:%0A                app_labels.add(schema._meta.app_label)%0A    for app_label in app_labels:%0A        if not app_label in couchdbkit_handler._databases:%0A            continue%0A        if verbosity %3E=1:%0A            print %22sync %60%25s%60 in CouchDB%22 %25 app_name%0A        db = couchdbkit_handler.get_db(app_label)%0A%0A        app_path = os.path.abspath(os.path.join(sys.modules%5Bapp.__name__%5D.__file__, %22..%22))%0A        design_path = %22%25s/%25s%22 %25 (app_path, %22_design%22)%0A        if not os.path.isdir(design_path):%0A            if settings.DEBUG:%0A                print %3E%3Esys.stderr, %22%25s don't exists, no ddoc synchronized%22 %25 design_path%0A            return%0A%0A        # these lines differ from the original%0A        # and simply pass on the responsibility of syncing to our%0A        # improved method%0A        design_name = app_label%0A        design_dir = os.path.join(app_path, %22_design%22)%0A        sync_design_docs(%0A            db=db,%0A            design_dir=design_dir,%0A            design_name=design_name,%0A            temp=temp,%0A        )%0A

replace default couchdbhandler.sync