Coverage for apps/report/bulk_import/child_family_participation.py : 97%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import copy
3from report.models import Gender, ChildFamilyParticipation as CFP
4from report.utils import convert_to_int
6from .common import get_or_create_project
8TYPE_INITIAL = {
9 Gender.MALE: {},
10 Gender.FEMALE: {},
11}
13INITIAL_PARTICIPATION = {
14 CFP.CHILD_PARTICIPATION: copy.deepcopy(TYPE_INITIAL),
15 CFP.FAMILY_PARTICIPATION: copy.deepcopy(TYPE_INITIAL),
16 CFP.CHILD_SUPPORT: copy.deepcopy(TYPE_INITIAL),
17 CFP.FAMILY_SUPPORT: copy.deepcopy(TYPE_INITIAL),
18 CFP.BENEFIT_SUPPORT: copy.deepcopy(TYPE_INITIAL),
19}
22def increment(dict_value, key, inc_value=1):
23 if dict_value.get(key) is None:
24 dict_value[key] = 0
25 dict_value[key] += inc_value
28def extract(xml_data, generated_on):
29 """
30 ('@Textbox2', '1'),
31 ('@Project', '194278'),
32 ('@CommunityName', 'AL0016 - 194278-Chandannath-10'),
33 ('@SdChildId', 'NPL-194278-1384'),
34 ('@ChildName', 'KAMI, Nani'),
35 ('@Gender', 'F'),
36 ('@AGE', '11 Y'),
37 ('@ChildParticipation', '1'),
38 ('@FamilyParticipation', '0'),
39 ('@ChildSupport', '0'),
40 ('@FamilySupport', '0')
41 """
42 collection = xml_data['Report']['Tablix1']['Details_Collection']['Details']
43 import_data = {}
44 for pj_data in collection:
45 pj_number = convert_to_int(pj_data['@Project'])
46 gender_raw = pj_data['@Gender']
47 gender = Gender.FEMALE if gender_raw == 'F' else Gender.MALE
49 child_participation = convert_to_int(pj_data['@ChildParticipation'])
50 family_participation = convert_to_int(pj_data['@FamilyParticipation'])
51 child_support = convert_to_int(pj_data['@ChildSupport'])
52 family_support = convert_to_int(pj_data['@FamilySupport'])
53 benefit_support = convert_to_int(pj_data['@BenefitSupport'])
55 if import_data.get(pj_number) is None:
56 import_data[pj_number] = copy.deepcopy(INITIAL_PARTICIPATION)
58 for type_value, type_number in [
59 (CFP.CHILD_PARTICIPATION, child_participation),
60 (CFP.FAMILY_PARTICIPATION, family_participation),
61 (CFP.CHILD_SUPPORT, child_support),
62 (CFP.FAMILY_SUPPORT, family_support),
63 (CFP.BENEFIT_SUPPORT, benefit_support),
64 ]:
65 if type_number == 0:
66 continue
67 increment(import_data[pj_number][type_value][gender], type_number)
69 # Clear older record for that date
70 CFP.objects.filter(date=generated_on).all().delete()
72 for pj_number, participation_data in import_data.items():
73 project = get_or_create_project(pj_number)
74 for participation_type, gender_data in participation_data.items():
75 for gender_type, number_data in gender_data.items():
76 for participation_number, count in number_data.items():
77 if count == 0 or participation_number == 0: 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true
78 continue
79 cfp, _ = CFP.objects.get_or_create(
80 project=project,
81 date=generated_on,
82 type=participation_type,
83 gender=gender_type,
84 participation=participation_number,
85 )
86 cfp.count = count
87 cfp.save()