import os
from couchdbkit.ext.django.loading import get_db
# corehq dependency is simplest of a few annoying options
# - move this code into corehq
# - extract corehq.preindex into its own project
# - be ok with intentionally introducing this dependency
#   for something that's pretty bound to our specific deploy workflow
from corehq.preindex import PreindexPlugin
from pillowtop.utils import get_all_pillows
from dimagi.utils.couch import sync_docs


class FluffPreindexPlugin(PreindexPlugin):
    def sync_design_docs(self, temp=None):
        synced = set()
        for pillow in get_all_pillows(instantiate=False):
            if hasattr(pillow, 'indicator_class'):
                app_label = pillow.indicator_class._meta.app_label
                db = get_db(app_label)
                if db not in synced:
                    sync_docs.sync_design_docs(
                        db=db,
                        design_dir=os.path.join(self.dir, "_design"),
                        design_name=self.app_label,
                        temp=temp,
                    )
                    synced.add(db)

    def copy_designs(self, temp=None, delete=True):
        copied = set()
        for pillow in get_all_pillows(instantiate=False):
            if hasattr(pillow, 'indicator_class'):
                app_label = pillow.indicator_class._meta.app_label
                db = get_db(app_label)
                if db not in copied:
                    sync_docs.copy_designs(
                        db=db,
                        design_name=self.app_label,
                        temp=temp,
                        delete=delete,
                    )
                    copied.add(db)


FluffPreindexPlugin.register('fluff', __file__)

@@ -360,16 +360,68 @@
 xPlugin%0A
+from dimagi.utils.couch.sync_docs import DesignInfo%0A
 from pil
@@ -542,24 +542,25 @@
 lugin):%0A
+%0A
     def 
 sync_des
@@ -555,65 +555,47 @@
 def 
-sync
+get
 _design
-_doc
 s(self
-, temp=None):%0A        synced = set()
+):%0A        designs = %5B%5D
 %0A   
@@ -788,601 +788,68 @@
    d
-b = get_db(app_label)%0A                if db not in synced:%0A                    sync_docs.sync_design_docs(%0A                        db=db,%0A                        design_dir=os.path.join(self.dir, %22_design%22),%0A                        design_name=self.app_label,%0A                        temp=temp,%0A                    )%0A                    synced.add(db)%0A%0A    def copy_designs(self, temp=None, delete=True):%0A        copied = set()%0A        for pillow in get_all_pillows(instantiate=False):%0A            if hasattr(pillow, 'indicator_class'):%0A                app_label = pillow.indicator_class._meta
+esigns.append(DesignInfo(%0A                    app_label=self
 .app
@@ -846,32 +846,33 @@
 l=self.app_label
+,
 %0A               
@@ -876,13 +876,15 @@
     
-db = 
+    db=
 get_
@@ -896,16 +896,17 @@
 p_label)
+,
 %0A       
@@ -918,286 +918,99 @@
     
-if db not in copied:%0A                    sync_docs.copy_designs(%0A                        db=db,%0A                        design_name=self.app_label,%0A                        temp=temp,%0A                        delete=delete,%0A                    )%0A                    copied.add(db)
+    design_path=os.path.join(self.dir, %22_design%22)%0A                ))%0A        return designs
 %0A%0A%0AF

use DesignInfo objects during preindex