<s> from datetime import datetime , timedelta , time <EOL> from dateutil . parser import parser <EOL> from django . core . cache import cache <EOL> import json <EOL> from casexml . apps . case . models import CommCareCase <EOL> from corehq . apps . api . es import ReportXFormES <EOL> from corehq . util . dates import iso_string_to_datetime , iso_string_to_date <EOL> from dimagi . utils . dates import force_to_datetime <EOL> from dimagi . utils . parsing import json_format_date <EOL> from pact . enums import PACT_DOMAIN <EOL> from pact . lib . quicksect import IntervalNode <EOL> from pact . utils import get_patient_display_cache , get_report_script_field <EOL> import logging <EOL> cached_schedules = { } <EOL> def get_seconds ( d ) : <EOL> import time <EOL> return time . mktime ( force_to_datetime ( d ) . utctimetuple ( ) ) <EOL> class CHWPatientSchedule ( object ) : <EOL> def __init__ ( self , username , intervaltrees , raw_schedule ) : <EOL> self . username = username <EOL> self . intervals = intervaltrees <EOL> self . raw_schedule = raw_schedule <EOL> def scheduled_for_date ( self , date_val ) : <EOL> """<STR_LIT>""" <EOL> day_of_week = date_val . isoweekday ( ) % <NUM_LIT:7> <EOL> if not self . intervals . has_key ( day_of_week ) : <EOL> return [ ] <EOL> else : <EOL> pass <EOL> day_tree = self . intervals [ day_of_week ] <EOL> results = [ ] <EOL> day_tree . intersect ( get_seconds ( date_val ) - <NUM_LIT> , get_seconds ( date_val ) , <EOL> lambda x : results . append ( x . other ) ) <EOL> return results <EOL> @ classmethod <EOL> def get_schedule ( cls , chw_username , override_date = None ) : <EOL> """<STR_LIT>""" <EOL> cached_schedules = None <EOL> if override_date == None : <EOL> nowdate = datetime . utcnow ( ) <EOL> else : <EOL> nowdate = override_date <EOL> day_intervaltree = { } <EOL> if cached_schedules == None : <EOL> db = CommCareCase . get_db ( ) <EOL> chw_schedules = db . view ( '<STR_LIT>' , key = chw_username ) . all ( ) <EOL> to_cache = [ ] <EOL> for item in chw_schedules : <EOL> single_sched = item [ '<STR_LIT:value>' ] <EOL> to_cache . append ( single_sched ) <EOL> cache . set ( "<STR_LIT>" % ( chw_username ) , json . dumps ( to_cache ) , <NUM_LIT> ) <EOL> cached_arr = to_cache <EOL> else : <EOL> cached_arr = json . loads ( cached_schedules ) <EOL> for single_sched in cached_arr : <EOL> day_of_week = int ( single_sched [ '<STR_LIT>' ] ) <EOL> if day_intervaltree . has_key ( day_of_week ) : <EOL> daytree = day_intervaltree [ day_of_week ] <EOL> else : <EOL> daytree = IntervalNode ( get_seconds ( datetime . min ) , <EOL> get_seconds ( nowdate + timedelta ( days = <NUM_LIT:10> ) ) ) <EOL> if single_sched [ '<STR_LIT>' ] == None : <EOL> enddate = nowdate + timedelta ( days = <NUM_LIT:9> ) <EOL> else : <EOL> enddate = iso_string_to_datetime ( single_sched [ '<STR_LIT>' ] ) <EOL> startdate = iso_string_to_datetime ( single_sched [ '<STR_LIT>' ] ) <EOL> case_id = single_sched [ '<STR_LIT>' ] <EOL> if single_sched . has_key ( '<STR_LIT:error>' ) : <EOL> logging . error ( "<STR_LIT>" % single_sched [ '<STR_LIT:error>' ] ) <EOL> daytree . insert ( get_seconds ( startdate ) , get_seconds ( enddate ) , other = case_id ) <EOL> day_intervaltree [ day_of_week ] = daytree <EOL> return cls ( chw_username , day_intervaltree , cached_arr ) <EOL> def dots_submissions_by_case ( case_id , query_date , username = None ) : <EOL> """<STR_LIT>""" <EOL> xform_es = ReportXFormES ( PACT_DOMAIN ) <EOL> script_fields = { <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' , is_known = True ) , <EOL> "<STR_LIT>" : get_report_script_field ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT:username>" : get_report_script_field ( '<STR_LIT>' , is_known = True ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : get_report_script_field ( '<STR_LIT>' ) , <EOL> } <EOL> term_block = { '<STR_LIT>' : '<STR_LIT>' } <EOL> if username is not None : <EOL> term_block [ '<STR_LIT>' ] = username <EOL> query = xform_es . by_case_id_query ( PACT_DOMAIN , case_id , terms = term_block , <EOL> date_field = '<STR_LIT>' , startdate = query_date , <EOL> enddate = query_date ) <EOL> query [ '<STR_LIT>' ] = { '<STR_LIT>' : '<STR_LIT>' } <EOL> query [ '<STR_LIT>' ] = script_fields <EOL> query [ '<STR_LIT:size>' ] = <NUM_LIT:1> <EOL> query [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> res = xform_es . run_query ( query ) <EOL> print json . dumps ( res , indent = <NUM_LIT:2> ) <EOL> return res <EOL> def get_schedule_tally ( username , total_interval , override_date = None ) : <EOL> """<STR_LIT>""" <EOL> if override_date is None : <EOL> nowdate = datetime . utcnow ( ) <EOL> chw_schedule = CHWPatientSchedule . get_schedule ( username ) <EOL> else : <EOL> nowdate = override_date <EOL> chw_schedule = CHWPatientSchedule . get_schedule ( username , override_date = nowdate ) <EOL> patient_case_ids = set ( [ x [ '<STR_LIT>' ] for x in chw_schedule . raw_schedule ] ) <EOL> patient_cache = get_patient_display_cache ( list ( patient_case_ids ) ) <EOL> ret = [ ] <EOL> total_scheduled = <NUM_LIT:0> <EOL> total_visited = <NUM_LIT:0> <EOL> for n in range ( <NUM_LIT:0> , total_interval ) : <EOL> td = timedelta ( days = n ) <EOL> visit_date = nowdate - td <EOL> scheduled_case_ids = chw_schedule . scheduled_for_date ( visit_date ) <EOL> patient_case_ids = set ( filter ( lambda x : x is not None , scheduled_case_ids ) ) <EOL> dereferenced_patient_info = [ patient_cache . get ( x , { } ) for x in patient_case_ids ] <EOL> visited = [ ] <EOL> dp = parser ( ) <EOL> for case_id in patient_case_ids : <EOL> total_scheduled += <NUM_LIT:1> <EOL> search_results = dots_submissions_by_case ( case_id , visit_date , username = username ) <EOL> submissions = search_results [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if len ( submissions ) > <NUM_LIT:0> : <EOL> pillbox_check_str = submissions [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if len ( pillbox_check_str ) > <NUM_LIT:0> : <EOL> pillbox_check_data = json . loads ( pillbox_check_str ) <EOL> anchor_date = dp . parse ( pillbox_check_data . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> pillbox_check_data = { } <EOL> anchor_date = datetime . min <EOL> encounter_date = dp . parse ( submissions [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> submissions [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' if anchor_date . date ( ) == encounter_date . date ( ) else '<STR_LIT>' <EOL> visited . append ( submissions [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> total_visited += <NUM_LIT:1> <EOL> else : <EOL> search_results = dots_submissions_by_case ( case_id , visit_date ) <EOL> other_submissions = search_results [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if len ( other_submissions ) > <NUM_LIT:0> : <EOL> visited . append ( other_submissions [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> total_visited += <NUM_LIT:1> <EOL> else : <EOL> visited . append ( None ) <EOL> ret . append ( ( visit_date , zip ( dereferenced_patient_info , visited ) ) ) <EOL> return ret , patient_case_ids , total_scheduled , total_visited <EOL> def chw_calendar_submit_report ( request , username , interval = <NUM_LIT:7> ) : <EOL> """<STR_LIT>""" <EOL> return_context = { } <EOL> return_context [ '<STR_LIT:username>' ] = username <EOL> total_interval = interval <EOL> if '<STR_LIT>' in request . GET : <EOL> try : <EOL> total_interval = int ( request . GET [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> pass <EOL> if '<STR_LIT>' in request . GET : <EOL> end_date_str = request . GET . get ( '<STR_LIT>' , json_format_date ( datetime . utcnow ( ) ) ) <EOL> end_date = iso_string_to_date ( end_date_str ) <EOL> else : <EOL> end_date = datetime . utcnow ( ) . date ( ) <EOL> if '<STR_LIT>' in request . GET : <EOL> start_date_str = request . GET . get ( '<STR_LIT>' , json_format_date ( datetime . utcnow ( ) ) ) <EOL> start_date = iso_string_to_date ( start_date_str ) <EOL> total_interval = ( end_date - start_date ) . days <EOL> ret , patients , total_scheduled , total_visited = get_schedule_tally ( username , <EOL> total_interval , <EOL> override_date = end_date ) <EOL> if len ( ret ) > <NUM_LIT:0> : <EOL> return_context [ '<STR_LIT>' ] = ret <EOL> return_context [ '<STR_LIT>' ] = total_scheduled <EOL> return_context [ '<STR_LIT>' ] = total_visited <EOL> return_context [ '<STR_LIT>' ] = ret [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> return_context [ '<STR_LIT>' ] = ret [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> return_context [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> return_context [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> return return_context </s>
