<s> from collections import namedtuple <EOL> import logging <EOL> import csv <EOL> from couchdbkit . exceptions import ResourceNotFound <EOL> from django . core . management import BaseCommand <EOL> from casexml . apps . case . models import CommCareCase <EOL> from corehq . apps . hqcase . dbaccessors import get_case_ids_in_domain <EOL> from dimagi . utils . couch . database import iter_docs <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . setLevel ( '<STR_LIT>' ) <EOL> MOTECH_ID = "<STR_LIT>" <EOL> MissingParent = namedtuple ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> class CaseRow ( object ) : <EOL> headers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> save = False <EOL> def __init__ ( self , case , parent ) : <EOL> self . case = case <EOL> self . parent = parent <EOL> self . old_type = case . type <EOL> self . old_owner = case . owner_id <EOL> def update_type ( self , new_type ) : <EOL> self . case . type = new_type <EOL> self . save = True <EOL> def update_owner ( self , new_owner ) : <EOL> self . case . owner_id = new_owner <EOL> self . save = True <EOL> def to_row ( self ) : <EOL> return [ <EOL> self . case . _id , <EOL> self . old_type , <EOL> self . case . type , <EOL> self . old_owner , <EOL> self . case . owner_id , <EOL> self . parent . get_id if self . parent else '<STR_LIT>' , <EOL> self . parent . owner_id if self . parent else '<STR_LIT>' , <EOL> self . case . user_id , <EOL> self . save <EOL> ] <EOL> class Command ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self , * args , ** options ) : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:wb>' ) as f : <EOL> csv_file = csv . writer ( f ) <EOL> csv_file . writerow ( CaseRow . headers ) <EOL> blank_case_ids = get_case_ids_in_domain ( '<STR_LIT>' , <EOL> type = ( '<STR_LIT>' , None ) ) <EOL> task_case_ids = get_case_ids_in_domain ( '<STR_LIT>' , type = '<STR_LIT>' ) <EOL> case_ids = set ( blank_case_ids ) | set ( task_case_ids ) <EOL> to_save = [ ] <EOL> logger . info ( "<STR_LIT>" . format ( len ( case_ids ) ) ) <EOL> for i , doc in enumerate ( iter_docs ( CommCareCase . get_db ( ) , case_ids ) ) : <EOL> case = CommCareCase . wrap ( doc ) <EOL> if case . type and case . type != "<STR_LIT>" : <EOL> continue <EOL> parent = None <EOL> if case . indices : <EOL> parent_id = case . indices [ <NUM_LIT:0> ] . referenced_id <EOL> try : <EOL> parent = CommCareCase . get ( parent_id ) <EOL> except ResourceNotFound : <EOL> parent = MissingParent ( get_id = parent_id , owner_id = '<STR_LIT>' ) <EOL> case_row = CaseRow ( case , parent ) <EOL> if case . type != '<STR_LIT>' : <EOL> if case . user_id == MOTECH_ID : <EOL> case_row . update_type ( '<STR_LIT>' ) <EOL> if parent and not isinstance ( parent , MissingParent ) and parent . owner_id != case . owner_id : <EOL> case_row . update_owner ( parent . owner_id ) <EOL> if case_row . save : <EOL> csv_file . writerow ( case_row . to_row ( ) ) <EOL> to_save . append ( case_row . case ) <EOL> if len ( to_save ) > <NUM_LIT:100> : <EOL> CommCareCase . get_db ( ) . bulk_save ( to_save ) <EOL> to_save = [ ] <EOL> if i % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> logger . info ( "<STR_LIT>" . format ( current = i , count = len ( case_ids ) ) ) <EOL> if to_save : <EOL> CommCareCase . get_db ( ) . bulk_save ( to_save ) </s>
