<s> import datetime <EOL> import warnings <EOL> from casexml . apps . case . models import CommCareCase <EOL> from casexml . apps . case . signals import case_post_save <EOL> from casexml . apps . case . xform import get_and_check_xform_domain , process_cases_with_casedb <EOL> from collections import defaultdict <EOL> from couchforms . models import XFormInstance <EOL> from dimagi . utils . parsing import string_to_datetime <EOL> from django . conf import settings <EOL> from django . core . management . base import BaseCommand , CommandError , LabelCommand <EOL> from optparse import make_option <EOL> from corehq . apps . cleanup . xforms import iter_problem_forms <EOL> from corehq . form_processor . backends . couch . casedb import CaseDbCacheCouch <EOL> def _process_cases ( xform , config = None ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> DeprecationWarning , <EOL> ) <EOL> assert getattr ( settings , '<STR_LIT>' , False ) <EOL> domain = get_and_check_xform_domain ( xform ) <EOL> with CaseDbCacheCouch ( domain = domain , lock = True , deleted_ok = True ) as case_db : <EOL> case_result = process_cases_with_casedb ( [ xform ] , case_db , config = config ) <EOL> cases = case_result . cases <EOL> docs = [ xform ] + cases <EOL> now = datetime . datetime . utcnow ( ) <EOL> for case in cases : <EOL> case . server_modified_on = now <EOL> XFormInstance . get_db ( ) . bulk_save ( docs ) <EOL> for case in cases : <EOL> case_post_save . send ( CommCareCase , case = case ) <EOL> case_result . commit_dirtiness_flags ( ) <EOL> return cases <EOL> def reprocess_form_cases ( form ) : <EOL> """<STR_LIT>""" <EOL> _process_cases ( form ) <EOL> if form . doc_type != '<STR_LIT>' : <EOL> form = XFormInstance . get ( form . _id ) <EOL> form . doc_type = '<STR_LIT>' <EOL> form . save ( ) <EOL> class Command ( BaseCommand ) : <EOL> args = '<STR_LIT>' <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> option_list = LabelCommand . option_list + ( make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = "<STR_LIT>" ) , ) <EOL> def handle ( self , * args , ** options ) : <EOL> domain = since = None <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> domain = args [ <NUM_LIT:0> ] <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> domain = args [ <NUM_LIT:0> ] <EOL> since = string_to_datetime ( args [ <NUM_LIT:1> ] ) <EOL> else : <EOL> raise CommandError ( '<STR_LIT>' % ( self . args , self . help ) ) <EOL> succeeded = [ ] <EOL> failed = [ ] <EOL> error_messages = defaultdict ( lambda : <NUM_LIT:0> ) <EOL> for form in iter_problem_forms ( domain , since ) : <EOL> print "<STR_LIT>" % ( form . _id , form . received_on , <EOL> form . xmlns , <EOL> form . get_data ( '<STR_LIT>' ) , <EOL> form . problem . strip ( ) ) <EOL> if not options [ "<STR_LIT>" ] : <EOL> try : <EOL> reprocess_form_cases ( form ) <EOL> except Exception , e : <EOL> failed . append ( form . _id ) <EOL> error_messages [ str ( e ) ] += <NUM_LIT:1> <EOL> else : <EOL> succeeded . append ( form . _id ) <EOL> print "<STR_LIT>" % ( len ( succeeded ) , len ( succeeded ) + len ( failed ) , len ( failed ) ) <EOL> if error_messages : <EOL> print "<STR_LIT>" % ( "<STR_LIT:\n>" . join ( "<STR_LIT>" % ( v , k ) for k , v in error_messages . items ( ) ) ) </s>
