<s> import logging <EOL> from couchdbkit import ResourceNotFound <EOL> import redis <EOL> from casexml . apps . case . dbaccessors . related import get_reverse_indexed_cases <EOL> from casexml . apps . case . exceptions import IllegalCaseId <EOL> from casexml . apps . case . models import CommCareCase <EOL> from casexml . apps . case . util import iter_cases <EOL> from corehq . form_processor . backends . couch . update_strategy import CouchCaseUpdateStrategy <EOL> from corehq . form_processor . casedb_base import AbstractCaseDbCache <EOL> from corehq . form_processor . exceptions import CouchSaveAborted <EOL> class CaseDbCacheCouch ( AbstractCaseDbCache ) : <EOL> case_model_classes = ( dict , CommCareCase ) <EOL> case_update_strategy = CouchCaseUpdateStrategy <EOL> def _validate_case ( self , doc ) : <EOL> if self . domain and doc [ '<STR_LIT>' ] != self . domain : <EOL> raise IllegalCaseId ( "<STR_LIT>" ) <EOL> elif doc [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if not self . deleted_ok : <EOL> raise IllegalCaseId ( "<STR_LIT>" % doc [ '<STR_LIT>' ] ) <EOL> elif doc [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> raise IllegalCaseId ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( doc [ '<STR_LIT>' ] ) <EOL> ) <EOL> def _get_case ( self , case_id ) : <EOL> try : <EOL> if self . strip_history : <EOL> case_doc = CommCareCase . get_lite ( case_id , wrap = self . wrap ) <EOL> elif self . lock : <EOL> try : <EOL> case_doc , lock = CommCareCase . get_locked_obj ( _id = case_id ) <EOL> except redis . RedisError : <EOL> case_doc = CommCareCase . get ( case_id ) <EOL> else : <EOL> self . locks . append ( lock ) <EOL> else : <EOL> if self . wrap : <EOL> case_doc = CommCareCase . get ( case_id ) <EOL> else : <EOL> case_doc = CommCareCase . get_db ( ) . get ( case_id ) <EOL> except ResourceNotFound : <EOL> return None <EOL> return case_doc <EOL> def _iter_cases ( self , case_ids ) : <EOL> for case in iter_cases ( case_ids , self . strip_history , self . wrap ) : <EOL> yield case <EOL> def get_cases_for_saving ( self , now ) : <EOL> cases = self . get_changed ( ) <EOL> for case in cases : <EOL> case . initial_processing_complete = True <EOL> case . server_modified_on = now <EOL> try : <EOL> rev = CommCareCase . get_db ( ) . get_rev ( case . case_id ) <EOL> except ResourceNotFound : <EOL> pass <EOL> else : <EOL> if rev != case . get_rev : <EOL> raise CouchSaveAborted ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> case . get_id , case . get_rev , rev <EOL> ) <EOL> ) <EOL> return cases <EOL> def post_process_case ( self , case , xform ) : <EOL> self . case_update_strategy ( case ) . reconcile_actions_if_necessary ( xform ) <EOL> action_xforms = { action . xform_id for action in case . actions if action . xform_id } <EOL> mismatched_forms = action_xforms ^ set ( case . xform_ids ) <EOL> if mismatched_forms : <EOL> logging . warning ( <EOL> "<STR_LIT>" . format ( <EOL> xform . domain , <EOL> case . case_id <EOL> ) <EOL> ) <EOL> def get_reverse_indexed_cases ( self , case_ids ) : <EOL> return get_reverse_indexed_cases ( self . domain , case_ids ) </s>
