<s> import datetime <EOL> import logging <EOL> from django . utils . translation import ugettext_noop as _ <EOL> from custom . bihar . calculations . pregnancy import BirthPlace <EOL> from custom . bihar . calculations . types import TotalCalculator , AddCalculator <EOL> from custom . bihar . calculations . utils . calculations import get_actions , get_forms <EOL> from custom . bihar . calculations . utils . filters import is_pregnant_mother , A_MONTH , is_newborn_child <EOL> from custom . bihar . calculations . utils . xmlns import DELIVERY , PNC <EOL> import fluff <EOL> def latest_action_date ( actions ) : <EOL> dates = [ action . date for action in actions ] <EOL> if dates : <EOL> return max ( dates ) <EOL> else : <EOL> return None <EOL> def is_stillborn ( case ) : <EOL> properties = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> for xform in get_forms ( case , action_filter = lambda a : a . xform_xmlns == DELIVERY ) : <EOL> if xform . form . get ( '<STR_LIT>' ) != '<STR_LIT:yes>' : <EOL> continue <EOL> for p in properties : <EOL> value = xform . get_data ( '<STR_LIT>' % p ) <EOL> if not value : <EOL> child_infos = xform . get_data ( '<STR_LIT>' ) <EOL> if not child_infos : <EOL> continue <EOL> for child_info in child_infos : <EOL> child_info . get ( p ) <EOL> logging . debug ( '<STR_LIT>' % ( p , value ) ) <EOL> if value != '<STR_LIT>' : <EOL> return False <EOL> else : <EOL> if value != '<STR_LIT>' : <EOL> return False <EOL> return True <EOL> class MMCalculator ( TotalCalculator ) : <EOL> """<STR_LIT>""" <EOL> _ ( '<STR_LIT>' ) <EOL> window = datetime . timedelta ( days = <NUM_LIT> ) <EOL> def filter ( self , case ) : <EOL> return is_pregnant_mother ( case ) <EOL> @ fluff . date_emitter <EOL> def total ( self , case ) : <EOL> def mother_died ( a ) : <EOL> return ( <EOL> a . updated_known_properties . get ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> and a . xform_xmlns in ( DELIVERY , PNC ) <EOL> ) <EOL> for xform in get_forms ( case , action_filter = mother_died , reverse = True ) : <EOL> yield xform . form . get ( '<STR_LIT>' ) <EOL> break <EOL> class IMCalculator ( TotalCalculator ) : <EOL> """<STR_LIT>""" <EOL> _ ( '<STR_LIT>' ) <EOL> window = datetime . timedelta ( days = <NUM_LIT> ) <EOL> def filter ( self , case ) : <EOL> return is_newborn_child ( case ) <EOL> @ fluff . date_emitter <EOL> def total ( self , case ) : <EOL> def child_died ( a ) : <EOL> return a . updated_known_properties . get ( '<STR_LIT>' , None ) == "<STR_LIT>" <EOL> date = latest_action_date ( get_actions ( case , action_filter = child_died ) ) <EOL> if date : <EOL> yield date <EOL> class StillBirth ( TotalCalculator , AddCalculator ) : <EOL> window = A_MONTH <EOL> include_closed = True <EOL> @ fluff . filter_by <EOL> def is_stillborn ( self , case ) : <EOL> return is_stillborn ( case ) <EOL> class StillBirthPlace ( StillBirth , BirthPlace ) : <EOL> _ ( '<STR_LIT>' ) <EOL> _ ( '<STR_LIT>' ) <EOL> window = A_MONTH <EOL> class LiveBirth ( TotalCalculator , AddCalculator ) : <EOL> _ ( '<STR_LIT>' ) <EOL> window = A_MONTH <EOL> @ fluff . filter_by <EOL> def not_stillborn ( self , case ) : <EOL> return not is_stillborn ( case ) </s>
