def perform_exit(request): try: ovcid = request.POST.get('ovc_id') exit_date = convert_date(request.POST.get('exit_date')) exit_reason = request.POST.get('exit_reason') exit_org_name = request.POST.get('exit_org_name') # ovc_details = OVCRegistration.objects.get(person_id=ovcid) ovc_details.exit_date = exit_date ovc_details.exit_reason = exit_reason if exit_org_name: # ovc_details.exit_org_name = exit_org_name org, created = OVCExit.objects.update_or_create( person_id=ovcid, defaults={ 'person_id': ovcid, 'org_unit_name': exit_org_name }, ) ovc_details.is_active = False ovc_details.save( update_fields=["exit_date", "exit_reason", "is_active"]) except Exception as e: print 'error exiting - %s' % (str(e)) raise e else: pass
def save_form1b(request, person_id, edit=0): """Method to save form 1B.""" try: user_id = request.user.id domains = {'SC': 'DSHC', 'PS': 'DPSS', 'PG': 'DPRO', 'HE': 'DHES', 'HG': 'DHNU', 'EG': 'DEDU'} if edit: print 'F1B edit' else: f1b_date = request.POST.get('olmis_service_date') caretaker_id = request.POST.get('caretaker_id') f1bs = request.POST.getlist('f1b[]') print 'save', f1b_date, f1bs hh = get_house_hold(caretaker_id) hhid = hh.id if hh else None event_date = convert_date(f1b_date) newev = OVCCareEvents( event_type_id='FM1B', created_by=user_id, person_id=caretaker_id, house_hold_id=hhid, date_of_event=event_date) newev.save() # Attach services for f1bitm in f1bs: f1b = str(f1bitm) did = f1b[:2] domain = domains[did] OVCCareF1B(event_id=newev.pk, domain=domain, entity=f1b).save() except Exception as e: print 'error saving form 1B - %s' % (str(e)) return None else: return True
def save_viral_load(request): try: ovcid = request.POST.get('ovc_id') viral_date = convert_date(request.POST.get('viral_date')) ldl = request.POST.get('ldl') viral_value = request.POST.get('viral_value') viral_load = None if ldl == 'true' else viral_value # OVC Viral load org, created = OVCViralload.objects.update_or_create( person_id=ovcid, viral_date=viral_date, defaults={ 'person_id': ovcid, 'viral_load': viral_load }, ) except Exception as e: print 'error exiting - %s' % (str(e)) raise e else: pass
def save_bursary(request, person_id): """Method to save bursary details.""" try: adm_school = request.POST.get('in_school') school_id = request.POST.get('school_id') county_id = request.POST.get('child_county') constituency_id = request.POST.get('child_constituency') sub_county = request.POST.get('child_sub_county') location = request.POST.get('child_location') sub_location = request.POST.get('child_sub_location') village = request.POST.get('child_village') nearest_school = request.POST.get('nearest_school') nearest_worship = request.POST.get('nearest_worship') val_in_school = request.POST.get('in_school') in_school = True if val_in_school == 'AYES' else False school_class = request.POST.get('school_class') primary_school = request.POST.get('pri_school_name') school_marks = request.POST.get('kcpe_marks') father_names = request.POST.get('father_name') val_father_alive = request.POST.get('father_alive') father_alive = True if val_father_alive == 'AYES' else False father_telephone = request.POST.get('father_contact') mother_names = request.POST.get('mother_name') val_mother_alive = request.POST.get('mother_alive') mother_alive = True if val_mother_alive == 'AYES' else False mother_telephone = request.POST.get('mother_contact') guardian_names = request.POST.get('guardian_name') guardian_telephone = request.POST.get('guardian_contact') # guardian_relation = request.POST.get('guardian_relation') val_same_household = request.POST.get('living_with') same_household = True if val_same_household == 'AYES' else False val_father_chronic_ill = request.POST.get('father_ill') father_chronic_ill = True if val_father_chronic_ill == 'AYES' else False father_chronic_illness = request.POST.get('father_illness') val_father_disabled = request.POST.get('father_disabled') father_disabled = True if val_father_disabled == 'AYES' else False father_disability = request.POST.get('father_disability') val_father_pension = request.POST.get('father_pension') father_pension = True if val_father_pension == 'AYES' else False father_occupation = request.POST.get('father_occupation') val_mother_chronic_ill = request.POST.get('mother_ill') mother_chronic_ill = True if val_mother_chronic_ill == 'AYES' else False mother_chronic_illness = request.POST.get('mother_illness') val_mother_disabled = request.POST.get('mother_disabled') mother_disabled = True if val_mother_disabled == 'AYES' else False mother_disability = request.POST.get('mother_disability') val_mother_pension = request.POST.get('mother_pension') mother_pension = True if val_mother_pension == 'AYES' else False mother_occupation = request.POST.get('mother_occupation') fees_amount = request.POST.get('fees_amount') fees_balance = request.POST.get('balance_amount') school_secondary = request.POST.get('school_name') school_county_id = request.POST.get('school_county') school_constituency_id = request.POST.get('school_constituency') school_sub_county = request.POST.get('school_sub_county') school_location = request.POST.get('school_location') school_sub_location = request.POST.get('school_sub_location') school_village = request.POST.get('school_village') school_email = request.POST.get('school_email') school_telephone = request.POST.get('school_telephone') school_type = request.POST.get('school_type') school_category = request.POST.get('school_category') school_enrolled = request.POST.get('school_enrolled') school_bank_id = request.POST.get('bank') school_bank_branch = request.POST.get('bank_branch') school_bank_account = request.POST.get('bank_account') school_recommend_by = request.POST.get('recommend_principal') school_recommend_date = convert_date( request.POST.get('recommend_principal_date')) chief_recommend_by = request.POST.get('recommend_chief') chief_recommend_date = convert_date( request.POST.get('recommend_chief_date')) chief_telephone = request.POST.get('chief_telephone') csac_approved = request.POST.get('approved_csac') approved_amount = request.POST.get('approved_amount') scco_name = request.POST.get('scco_name') val_scco_signed = request.POST.get('signed_scco') scco_signed = True if val_scco_signed == 'AYES' else False scco_sign_date = convert_date(request.POST.get('date_signed_scco')) csac_chair_name = request.POST.get('csac_chair_name') val_csac_signed = request.POST.get('signed_csac') csac_signed = True if val_csac_signed == 'AYES' else False csac_sign_date = convert_date(request.POST.get('date_signed_csac')) application_date = convert_date(request.POST.get('application_date')) app_user_id = request.user.id obj, created = OVCEducationFollowUp.objects.get_or_create( school_id=school_id, person_id=person_id, defaults={'admitted_to_school': adm_school}, ) # Save all details from the Bursary form gok_bursary = OVCGokBursary( person_id=person_id, county_id=county_id, constituency_id=constituency_id, sub_county=sub_county, location=location, sub_location=sub_location, village=village, nearest_school=nearest_school, nearest_worship=nearest_worship, in_school=in_school, school_class=school_class, primary_school=primary_school, school_marks=school_marks, father_names=father_names, father_alive=father_alive, father_telephone=father_telephone, mother_names=mother_names, mother_alive=mother_alive, mother_telephone=mother_telephone, guardian_names=guardian_names, guardian_telephone=guardian_telephone, guardian_relation=guardian_relation, same_household=same_household, father_chronic_ill=father_chronic_ill, father_chronic_illness=father_chronic_illness, father_disabled=father_disabled, father_disability=father_disability, father_pension=father_pension, father_occupation=father_occupation, mother_chronic_ill=mother_chronic_ill, mother_chronic_illness=mother_chronic_illness, mother_disabled=mother_disabled, mother_disability=mother_disability, mother_pension=mother_pension, mother_occupation=mother_occupation, fees_amount=fees_amount, fees_balance=fees_balance, school_secondary=school_secondary, school_county_id=school_county_id, school_constituency_id=school_constituency_id, school_sub_county=school_sub_county, school_location=school_location, school_sub_location=school_sub_location, school_village=school_village, school_telephone=school_telephone, school_email=school_email, school_type=school_type, school_category=school_category, school_enrolled=school_enrolled, school_bank_id=school_bank_id, school_bank_branch=school_bank_branch, school_bank_account=school_bank_account, school_recommend_by=school_recommend_by, school_recommend_date=school_recommend_date, chief_recommend_by=chief_recommend_by, chief_recommend_date=chief_recommend_date, chief_telephone=chief_telephone, csac_approved=csac_approved, approved_amount=approved_amount, ssco_name=scco_name, scco_signed=scco_signed, scco_sign_date=scco_sign_date, csac_chair_name=csac_chair_name, csac_signed=csac_signed, csac_sign_date=csac_sign_date, app_user_id=app_user_id, application_date=application_date) gok_bursary.save() except Exception as e: print 'Error saving bursary - %s' % (str(e)) else: return True
def ovc_registration(request, ovc_id, edit=0): """Method to complete ovc registration.""" try: reg_date = request.POST.get('reg_date') reg_date = convert_date(reg_date) bcert = request.POST.get('has_bcert') disabled = request.POST.get('disb') hh_members = request.POST.getlist('hh_member') cbo_id = request.POST.get('cbo_id') has_bcert = True if bcert else False is_disabled = True if disabled else False bcert_no = request.POST.get('bcert_no') ncpwd_no = request.POST.get('ncpwd_no') ext_ids = {} if bcert_no: ext_ids['ISOV'] = bcert_no if ncpwd_no: ext_ids['IPWD'] = ncpwd_no if ext_ids: save_person_extids(ext_ids, ovc_id) hiv_status = request.POST.get('hiv_status') immmune = request.POST.get('immunization') org_uid = request.POST.get('cbo_uid') org_uid_check = request.POST.get('cbo_uid_check') caretaker = request.POST.get('caretaker') school_level = request.POST.get('school_level') is_exited = request.POST.get('is_exited') exit_reason = request.POST.get('exit_reason') criterias = request.POST.getlist('eligibility') exit_date = datetime.now().strftime("%Y-%m-%d") if edit == 0: cbo_uid = gen_cbo_id(cbo_id, ovc_id) org_cid = cbo_uid if org_uid == org_uid_check else org_uid else: org_cid = org_uid is_active = False if is_exited else True ovc_detail = get_object_or_404(OVCRegistration, person_id=ovc_id) ovc_detail.registration_date = reg_date ovc_detail.has_bcert = has_bcert ovc_detail.is_disabled = is_disabled if edit == 0 or not ovc_detail.hiv_status: ovc_detail.hiv_status = str(hiv_status) ovc_detail.immunization_status = str(immmune) ovc_detail.org_unique_id = org_cid ovc_detail.caretaker_id = caretaker ovc_detail.school_level = school_level ovc_detail.is_active = is_active ovc_detail.exit_reason = exit_reason if exit_reason: ovc_detail.exit_date = exit_date ovc_detail.save( update_fields=["registration_date", "has_bcert", "is_disabled", "immunization_status", "org_unique_id", "caretaker_id", "school_level", "hiv_status", "is_active", "exit_reason", "exit_date"]) # Update eligibility for criteria_id in criterias: eligibility, created = OVCEligibility.objects.update_or_create( person_id=ovc_id, criteria=criteria_id, defaults={'person_id': ovc_id, 'criteria': criteria_id},) # Update Health status if hiv_status == 'HSTP': facility = request.POST.get('facility_id') art_status = request.POST.get('art_status') link_date = request.POST.get('link_date') date_linked = convert_date(link_date) ccc_no = request.POST.get('ccc_number') health, created = OVCHealth.objects.update_or_create( person_id=ovc_id, defaults={'person_id': ovc_id, 'facility_id': facility, 'art_status': art_status, 'date_linked': date_linked, 'ccc_number': ccc_no, 'is_void': False},) # Update School details if school_level != 'SLNS': school_class = request.POST.get('school_class') school_id = request.POST.get('school_id') school_adm = request.POST.get('admission_type') health, created = OVCEducation.objects.update_or_create( person_id=ovc_id, school_class=school_class, defaults={'person_id': ovc_id, 'school_id': school_id, 'school_level': school_level, 'school_class': school_class, 'admission_type': school_adm, 'is_void': False},) cgs = extract_post_params(request, naming='cg_') hhrs = extract_post_params(request, naming='hhr_') # Alive status, HIV status and Death cause for Guardian ast = extract_post_params(request, naming='astatus_') hst = extract_post_params(request, naming='gstatus_') cst = extract_post_params(request, naming='cstatus_') # Alive status, HIV status and Death cause for Sibling sast = extract_post_params(request, naming='sastatus_') shst = extract_post_params(request, naming='sgstatus_') todate = timezone.now() if edit == 0: # Create House Hold and populate members caretaker_id = int(cgs[caretaker][0]) new_hh = OVCHouseHold( head_person_id=caretaker, head_identifier=caretaker_id) new_hh.save() hh_id = new_hh.pk # Add members to HH hh_members.append(ovc_id) for hh_m in hh_members: oid = int(ovc_id) hh_head = True if int(hh_m) == caretaker_id else False m_type = hhrs[hh_m][0] if hh_m in hhrs else 'TBVC' member_type = 'TOVC' if oid == int(hh_m) else m_type if member_type == 'TBVC' or member_type == 'TOVC': hh_hiv = shst[hh_m][0] if hh_m in shst else None hh_alive = sast[hh_m][0] if hh_m in sast else 'AYES' hh_death = None else: hh_hiv = hst[hh_m][0] if hh_m in hst else None hh_alive = ast[hh_m][0] if hh_m in ast else 'AYES' hh_death = cst[hh_m][0] if hh_m in cst else None if oid == hh_m: hh_hiv, hh_alive, hh_death = hiv_status, 'AYES', None OVCHHMembers( house_hold_id=hh_id, person_id=hh_m, hh_head=hh_head, member_type=member_type, death_cause=hh_death, member_alive=hh_alive, hiv_status=hh_hiv, date_linked=todate).save() else: # Update HH details hhid = request.POST.get('hh_id') caretaker_id = cgs[caretaker][0] hh_detail = get_object_or_404(OVCHouseHold, id=hhid) hh_detail.head_person_id = caretaker hh_detail.head_identifier = caretaker_id hh_detail.save(update_fields=["head_identifier", "head_person"]) # Update HH Members for hh_m in hhrs: oid = int(ovc_id) hh_head = True if hh_m == caretaker else False member_type = hhrs[hh_m][0] if member_type == 'TBVC' or member_type == 'TOVC': hh_hiv = shst[hh_m][0] if hh_m in shst else None hh_alive = sast[hh_m][0] if hh_m in sast else 'AYES' hh_death = None else: hh_hiv = hst[hh_m][0] if hh_m in hst else None hh_alive = ast[hh_m][0] if hh_m in ast else 'AYES' hh_death = cst[hh_m][0] if hh_m in cst else None if oid == hh_m: hh_hiv, hh_alive, hh_death = hiv_status, 'AYES', None hhm, created = OVCHHMembers.objects.update_or_create( person_id=hh_m, house_hold_id=hhid, defaults={'person_id': hh_m, 'hh_head': hh_head, 'member_type': member_type, 'is_void': False, 'death_cause': hh_death, 'member_alive': hh_alive, 'date_linked': todate, 'hiv_status': hh_hiv},) except Exception, e: raise e
def dq_data(request): """Main home method and view.""" try: cases = [] sdate, edate = None, None sts = {0: 'Pending', 1: 'Open', 2: 'Closed'} # Conditions qa = request.GET.get('q_aspect') va = request.GET.get('variance') age = request.GET.get('age') from_date = request.GET.get('from_date') to_date = request.GET.get('to_date') org_unit = request.GET.get('org_unit') if from_date and to_date: sdate = convert_date(from_date) edate = convert_date(to_date) cage = int(age) if age else 0 vid = int(va) if va else 0 qid = int(qa) if qa else 0 q2 = Q(case_category_id__in=('CTRF', 'CCCT'), age__lt=6) q3 = Q(case_category_id__in=('CSAB', 'CSHV', 'CCCM', 'CORP'), age__lt=11) if qa: acases = RPTCaseLoad.objects.filter(is_void=False) if qid == 1: acases = acases.filter( Q(age__gte=25) | Q(dob__isnull=True) | Q(age__lt=0)) elif qid == 2: acases = acases.filter( Q(case_category_id='CDIS', age__gt=15) | Q(case_category_id='CSIC', age__gt=18) | q2 | q3) elif qid == 3: acases = acases.filter( case_category_id__in=('CSHV', 'CSCS'), sex_id='SMAL') elif qid == 4: acases = acases.filter( case_status=1, intervention__isnull=True) else: acases = RPTCaseLoad.objects.filter( Q(age__gte=25) | Q(dob__isnull=True)) if vid == 1: acases = acases.filter(age=cage) elif vid == 2: acases = acases.filter(age__gt=cage) elif vid == 3: acases = acases.filter(age__lt=cage) if edate and sdate: acases = acases.filter(case_date__range=(sdate, edate)) if org_unit: acases = acases.filter(org_unit_id=org_unit) else: if not request.user.is_superuser: acases = acases.filter(org_unit_id=org_unit) for case in acases[:1000]: cs = case.case_status fname = case.case.person.first_name sname = case.case.person.surname[0] o_name = case.case.person.other_names oname = o_name[0] if o_name else '' dt = {"cpims_id": case.case.person_id} dt['age'] = case.age dt['case_category'] = case.case_category dt['case_date'] = case.case_date dt['sex'] = case.sex dt['case_status'] = sts[cs] if cs in sts else 'Open' dt['dob'] = case.dob dt['org_unit'] = case.org_unit_name dt['intervention'] = case.intervention dt['org_unit'] = case.org_unit_name dt['names'] = '%s %s%s' % (fname, sname, oname) cases.append(dt) result = {"data": cases} return JsonResponse(result, content_type='application/json', safe=False) except Exception as e: print('error - %s' % (e)) raise e else: pass
def edit_travel(request, id): """Main home method and view.""" try: ChaperonFormset = formset_factory(ChaperonForm, extra=0) ChildrenFormset = formset_factory(ChildrenForm, extra=0) if request.method == 'POST': travel = NOTTTravel.objects.get(is_void=False, id=id) tdate = request.POST.get('travel_date') return_date = request.POST.get('return_date') no_applied = request.POST.get('no_applied') no_cleared = request.POST.get('no_cleared') no_returned = request.POST.get('no_returned') comments = request.POST.get('comments') contacts = request.POST.get('contacts') sponsor = request.POST.get('sponsor') reason = request.POST.get('reason') status_id = request.POST.get('status') status = 1 if status_id == 'on' else 0 institution_name = request.POST.get('institution_name') country_name = request.POST.get('country_name') travel_date = convert_date(tdate) if return_date: return_date = convert_date(return_date) travel.travel_date = travel_date travel.return_date = return_date travel.contacts = contacts travel.comments = comments travel.sponsor = sponsor travel.reason = reason travel.status = status travel.institution_name = institution_name travel.country_name = country_name # travel.save() # Chaperon formset = ChaperonFormset(request.POST, prefix='chap') cformset = ChildrenFormset(request.POST, prefix='child') print(request.POST) clear_count, return_count = 0, 0 if formset.is_valid(): if formset.has_changed(): for echap in formset.cleaned_data: ops = OvcCasePersons.objects.get(pk=echap['person_id']) ops.person_sex = echap['sex'] ops.person_first_name = echap['first_name'] ops.person_other_names = echap['other_names'] ops.person_surname = echap['surname'] ops.person_identifier = echap['passport_no'] ops.save() else: print(formset.errors) if cformset.is_valid(): if cformset.has_changed(): no_applied = len(cformset.cleaned_data) for echild in cformset.cleaned_data: cid = echild['person_id'] cidc = echild['cleared'] cidr = echild['returned'] cid_cleared = True if cidc == 'True' else False cid_returned = True if cidr == 'True' else False if cid_cleared: clear_count += 1 if cid_returned: return_count += 1 opc = RegPerson.objects.get(pk=cid) opc.sex_id = echild['sex'] opc.first_name = echild['first_name'] opc.other_names = echild['other_names'] opc.surname = echild['surname'] opc.save() # Update passport Number cpp = RegPersonsExternalIds.objects.get( person_id=cid, is_void=False, identifier_type_id='IPPN') cpp.identifier = echild['passport_no'] cpp.save() # Update Returned / Cleared details ch = NOTTChild.objects.get(travel_id=id, person_id=cid) ch.returned = cid_returned ch.cleared = cid_cleared ch.save() print(echild) no_returned = return_count no_cleared = clear_count else: print(cformset.errors) travel.no_applied = no_applied travel.no_cleared = no_cleared travel.no_returned = no_returned travel.save() url = reverse(view_travel, kwargs={'id': id}) return HttpResponseRedirect(url) travel = NOTTTravel.objects.filter(is_void=False, id=id).values()[0] travel_date = travel['travel_date'].strftime('%d-%b-%Y') return_date = None if travel['return_date']: return_date = travel['return_date'].strftime('%d-%b-%Y') travel['travel_date'] = travel_date travel['return_date'] = return_date nott_form = NOTTForm(travel) # Chaperons chaps = [] chaperons = NOTTChaperon.objects.filter(travel_id=id) for chap in chaperons: chap_details = {'first_name': chap.other_person.person_first_name} chap_details['surname'] = chap.other_person.person_surname chap_details['other_names'] = chap.other_person.person_other_names chap_details['sex'] = chap.other_person.person_sex chap_details['passport_no'] = chap.other_person.person_identifier chap_details['person_id'] = chap.other_person_id chap_details['chaperon_id'] = chap.id chaps.append(chap_details) chap_formset = ChaperonFormset(initial=chaps, prefix='chap') # Children tchildren = [] children = NOTTChild.objects.filter(travel_id=id) for child in children: child_details = {'first_name': child.person.first_name} child_details['surname'] = child.person.surname child_details['other_names'] = child.person.other_names child_details['sex'] = child.person.sex_id child_details['passport_no'] = child.passport child_details['person_id'] = child.person_id child_details['child_id'] = child.id child_details['cleared'] = child.cleared child_details['returned'] = child.returned tchildren.append(child_details) child_formset = ChildrenFormset(initial=tchildren, prefix='child') return render(request, 'management/edit_travel.html', {'form': nott_form, 'travel': travel, 'chap_formset': chap_formset, 'child_formset': child_formset}) except Exception as e: raise e else: pass