def _get_scheduled_chw_for_patient_visit(patient, visit_date): """Get the active scheduled chw for that day. returns a string of the chw username""" key = patient.couchdoc.pact_id visit_schedule_cache_key = hashlib.md5('patient_visit_schedule-%s-%s' % (key, visit_date.strftime("%Y-%m-%d"))).hexdigest() #### #Step 1, see if this has been cached before for that given chw chw_scheduled = cache.get(visit_schedule_cache_key, None) if chw_scheduled != None: #print "cache hit! %s on %s-%s" % (chw_scheduled, key, visit_date.strftime("%Y-%m-%d")) return chw_scheduled ################################ #Step 2, get the patient's entire visit schedule, cache the whole thing pt_schedule_cache_key = 'patient_schedule_key-%s' % (patient.couchdoc.pact_id) sched_dicts = cache.get(pt_schedule_cache_key, None) if sched_dicts == None: reduction = CDotWeeklySchedule.view('pactcarehq/patient_dots_schedule', key=key).first() if reduction == None: cache.set(pt_schedule_cache_key, repr({})) sched_dicts = {} else: sched_dicts = reduction['value'] sched_dicts = sorted(sched_dicts, key=lambda x: x['started']) cache.set(pt_schedule_cache_key, simplejson.dumps(sched_dicts)) else: sched_dicts = simplejson.loads(sched_dicts) ###### #Step 3 #having received the schedule dictionary, now process the scheduled chw for that visit date used_schedule=None chw_scheduled = None day_of_week = visit_date.isoweekday() %7 #isoweekday is monday 1, sunday 7, mod 7 to make sunday 0 for sched_dict in sched_dicts: sched = CDotWeeklySchedule.wrap(sched_dict) if (sched.ended == None or sched.ended > visit_date) and sched.started <= visit_date: used_schedule=sched if used_schedule != None: day_string = DAYS_OF_WEEK[day_of_week] chw_scheduled = used_schedule[day_string] cache.set(visit_schedule_cache_key, str(chw_scheduled)) #print "setting cache: %s for %s-%s" %(chw_scheduled, key, visit_date.strftime("%Y-%m-%d")) return chw_scheduled
def patient_view(request, patient_id, template_name="pactcarehq/patient.html"): schedule_show = request.GET.get("schedule", "active") schedule_edit = request.GET.get("edit_schedule", False) address_edit = request.GET.get("edit_address", False) address_edit_id = request.GET.get("address_id", None) new_address = True if request.GET.get("new_address", False) == "True" else False phone_edit = request.GET.get("edit_phone", False) patient_edit = request.GET.get('edit_patient', None) show_all_schedule = request.GET.get('allschedules', None) patient = Patient.objects.get(id=patient_id) context = RequestContext(request) context['patient']=patient context['pdoc']=patient.couchdoc context['schedule_show'] = schedule_show context['schedule_edit'] = schedule_edit context['phone_edit'] = phone_edit context['address_edit'] = address_edit context['patient_edit'] = patient_edit context['submit_arr'] = _get_submissions_for_patient(patient) last_bw = patient.couchdoc.last_bloodwork context['last_bloodwork'] = last_bw if last_bw == None: context['bloodwork_missing'] = True else: context['since_bw'] = (datetime.utcnow() - last_bw.get_date).days if (datetime.utcnow() - last_bw.get_date).days > 90: context['bloodwork_overdue'] = True else: context['bloodwork_overdue'] = False if address_edit and not new_address: if len(patient.couchdoc.address) > 0: #if there's an existing address out there, then use it, else, make a new one context['address_form'] = AddressForm(instance=patient.couchdoc.get_address(address_edit_id)) else: context['address_form'] = AddressForm() if new_address: context['address_form'] = AddressForm() context['address_edit'] = True #print "new address damn it!" if schedule_edit: context['schedule_form'] = ScheduleForm() if phone_edit: context['phone_form'] = PhoneForm() if patient_edit: context['patient_form'] = PactPatientForm(patient_edit, instance=patient.couchdoc) if show_all_schedule != None: context['past_schedules'] = patient.couchdoc.past_schedules if request.method == 'POST': if schedule_edit: form = ScheduleForm(data=request.POST) if form.is_valid(): sched = CDotWeeklySchedule() for day in DAYS_OF_WEEK: if form.cleaned_data[day] != None: setattr(sched, day, form.cleaned_data[day].username) if form.cleaned_data['active_date'] == None: sched.started=datetime.utcnow() else: sched.started = datetime.combine(form.cleaned_data['active_date'], time.min) sched.comment=form.cleaned_data['comment'] sched.created_by = request.user.username sched.deprecated=False patient.couchdoc.set_schedule(sched) return HttpResponseRedirect(reverse('pactcarehq.views.patient_view', kwargs={'patient_id':patient_id})) else: context['schedule_form'] = form elif address_edit or new_address: form = AddressForm(data=request.POST) if form.is_valid(): is_new_addr = False if form.cleaned_data['address_id'] != '': is_new_addr=False address_edit_id = form.cleaned_data['address_id'] instance = patient.couchdoc.get_address(address_edit_id) else: is_new_addr=True instance=CAddress() instance.created_by = request.user.username instance.description = form.cleaned_data['description'] instance.street = form.cleaned_data['street'] instance.city = form.cleaned_data['city'] instance.state = form.cleaned_data['state'] instance.postal_code = form.cleaned_data['postal_code'] if is_new_addr == False: index = patient.couchdoc.address_index(address_edit_id) patient.couchdoc.address[index] = instance else: patient.couchdoc.set_address(instance) patient.couchdoc.save() return HttpResponseRedirect(reverse('pactcarehq.views.patient_view', kwargs={'patient_id':patient_id})) else: context['address_form'] = form elif phone_edit: form = PhoneForm(data=request.POST) if form.is_valid(): new_phone = CPhone() new_phone.description = form.cleaned_data['description'] new_phone.number = form.cleaned_data['number'] new_phone.notes = form.cleaned_data['notes'] new_phone.created_by = request.user.username patient.couchdoc.phones.append(new_phone) patient.couchdoc.save() return HttpResponseRedirect(reverse('pactcarehq.views.patient_view', kwargs={'patient_id':patient_id})) else: logging.error("Error, invalid phone form data") elif patient_edit: form = PactPatientForm(patient_edit, instance=patient.couchdoc, data=request.POST) if form.is_valid(): instance = form.save(commit=True) return HttpResponseRedirect(reverse('pactcarehq.views.patient_view', kwargs={'patient_id':patient_id})) return render_to_response(template_name, context_instance=context)