def get_docs(schema_index, previous_export=None, filter=None): def _filter(results): if filter is None: return results return [doc for doc in results if filter(doc)] db = Database(settings.COUCH_DATABASE) if previous_export is not None: consumer = Consumer(db) view_results = consumer.fetch(since=previous_export.seq) if view_results: try: include_ids = set([res["id"] for res in view_results["results"]]) possible_ids = set([result['id'] for result in \ db.view("couchexport/schema_index", key=schema_index).all()]) ids_to_use = include_ids.intersection(possible_ids) return _filter(res["doc"] for res in \ db.all_docs(keys=list(ids_to_use), include_docs=True).all()) except Exception: import logging logging.exception("export failed! results: %s" % view_results) raise else: # sometimes this comes back empty. I think it might be a bug # in couchdbkit, but it's impossible to consistently reproduce. # For now, just assume this is fine. return [] else: return _filter([result['doc'] for result in \ db.view("couchexport/schema_index", key=schema_index, include_docs=True).all()])
def handle(self, *args, **options): db = Database(settings.BHOMA_NATIONAL_DATABASE) results = db.view("chw/by_clinic", include_docs=True).all() chws = [CommunityHealthWorker.wrap(res['doc']) for res in results] map = defaultdict(lambda: 0) def _fmt_date(dt): return "%s-%s" % (dt.year, dt.month) for chw in chws: print chw.username, chw.created_on map[_fmt_date(chw.created_on)] = map[_fmt_date(chw.created_on)] + 1 for k in sorted(map.keys()): print "%s, %s" % (k, map[k])
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)