def verify_chw_schedules(): #username = '******' #username = '******' #username = '******' #username = '******' #username = '******' #username='******' #username='******' username='******' usr_schedule = schedule.get_schedule(username) print usr_schedule for item in usr_schedule.raw_schedule: data = item['value'] #print "For day: %s" % (data) row = [username, DAYS_OF_WEEK[data['day_of_week']], data['pact_id']] if data['ended_date'] == None: end_date = datetime.utcnow() else: end_date = datetime.strptime(data['ended_date'], "%Y-%m-%dT%H:%M:%SZ") nowdelta = datetime.utcnow() - end_date duration_delta = end_date - datetime.strptime(data['active_date'], "%Y-%m-%dT%H:%M:%SZ") startx = ['0' for x in range(nowdelta.days/7)] durationx = ['1' for x in range(duration_delta.days/7)] row = row + startx row = row + durationx print ','.join(row)
def _get_schedule_tally(username, total_interval, override_date=None): """ For a given username and interval, get a simple array of the username and scheduled visit (whether a submission is there or not) exists. returns (schedule_tally_array, patient_array, total_scheduled (int), total_visited(int)) schedul_tally_array = [visit_date, [(patient1, visit1), (patient2, visit2), (patient3, None), (patient4, visit4), ...]] where visit = XFormInstance """ if override_date == None: nowdate = datetime.now() chw_schedule = schedule.get_schedule(username) else: nowdate = override_date chw_schedule = schedule.get_schedule(username, override_date = nowdate) #got the chw schedule #now let's walk through the date range, and get the scheduled CHWs per this date.visit_dates = [] ret = [] #where it's going to be an array of tuples: #(date, scheduled[], submissions[] - that line up with the scheduled) total_scheduled=0 total_visited=0 for n in range(0, total_interval): td = timedelta(days=n) visit_date = nowdate-td scheduled_pactids = chw_schedule.get_scheduled(visit_date) patients = [] visited = [] for pact_id in scheduled_pactids: if pact_id == None: continue try: total_scheduled += 1 cpatient = getpatient(pact_id) #TODO: this is a total waste of queries, doubly getting the cpatient, then getting the django object again # patients.append(Patient.objects.get(id=cpatient.django_uuid)) patients.append(cpatient) except: #print "skipping patient %s: %s, %s" % (cpatient.pact_id, cpatient.last_name, cpatient.first_name) continue #inefficient, but we need to get the patients in alpha order patients = sorted(patients, key=lambda x: x.last_name) for patient in patients: pact_id = patient.pact_id searchkey = [str(username), str(pact_id), visit_date.year, visit_date.month, visit_date.day] #print searchkey submissions = XFormInstance.view('pactcarehq/submits_by_chw_per_patient_date', key=searchkey, include_docs=True).all() #print len(submissions) if len(submissions) > 0: visited.append(submissions[0]) total_visited+= 1 else: #ok, so no submission from this chw, let's see if there's ANY from anyone on this day. other_submissions = XFormInstance.view('pactcarehq/all_submits_by_patient_date', key=[str(pact_id), visit_date.year, visit_date.month, visit_date.day, 'http://dev.commcarehq.org/pact/dots_form' ], include_docs=True).all() if len(other_submissions) > 0: visited.append(other_submissions[0]) total_visited+= 1 else: visited.append(None) #print (visit_date, patients, visited) ret.append((visit_date, zip(patients, visited))) return ret, patients, total_scheduled, total_visited