-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfindings.py
354 lines (302 loc) · 11.4 KB
/
findings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from collections import defaultdict
from ..invalid_migration_tags import INVALID_MIGRATION_TAGS
from ..invalid_record import InvalidRecord
from ..change_record import (
InspectionRecord,
)
from ..transforms.xform_retrieve_uei import xform_retrieve_uei
from ..transforms.xform_string_to_string import string_to_string
from ..transforms.xform_uppercase_y_or_n import uppercase_y_or_n
from ..workbooklib.excel_creation_utils import (
get_audits,
map_simple_columns,
set_range,
set_workbook_uei,
sort_by_field,
track_invalid_records,
track_transformations,
)
from ..base_field_maps import SheetFieldMap
from ..workbooklib.templates import sections_to_template_paths
from audit.fixtures.excel import FORM_SECTIONS
from ..models import ELECAUDITFINDINGS as Findings
import openpyxl as pyxl
from django.conf import settings
import logging
logger = logging.getLogger(__name__)
# Transformation Method Change Recording
# For the purpose of recording changes, the transformation methods (i.e., xform_***)
# below track all records related to the federal_awards section that undergoes transformation and
# log these changes in a temporary array called `change_records`.
# However, we only save this data into the InspectionRecord table if at least one of the records has been
# modified by the transformation. If no records related to the given section
# were modified, then we do not save `change_records` into the InspectionRecord.
def xform_sort_compliance_requirement(findings):
"""Sorts and uppercases the compliance requirement string."""
# Transformation to be documented
for finding in findings:
value = string_to_string(finding.TYPEREQUIREMENT).upper()
finding.TYPEREQUIREMENT = "".join(sorted(value))
def xform_missing_compliance_requirement(findings):
"""Defaults missing compliance_requirement to GSA_MIGRATION."""
change_records = []
is_empty_compliance_requirement_found = False
for finding in findings:
compliance_requirement = string_to_string(finding.TYPEREQUIREMENT)
if not compliance_requirement:
is_empty_compliance_requirement_found = True
compliance_requirement = settings.GSA_MIGRATION
track_transformations(
"TYPEREQUIREMENT",
finding.TYPEREQUIREMENT,
"type_requirement",
compliance_requirement,
["xform_missing_compliance_requirement"],
change_records,
)
finding.TYPEREQUIREMENT = compliance_requirement
# See Transformation Method Change Recording comment at the top of this file
if change_records and is_empty_compliance_requirement_found:
InspectionRecord.append_finding_changes(change_records)
def xform_prior_year_findings(value):
"""
Transform the value of prior_references to N/A if empty.
"""
# Transformation to be documented
trimmed_value = string_to_string(value)
if not trimmed_value:
# See ticket #2912
return "N/A"
return trimmed_value
mappings = [
SheetFieldMap(
"compliance_requirement",
"TYPEREQUIREMENT",
"type_requirement",
None,
# FIXME - MSHD: I removed sorted_string from here because it is a transformation and
# we want to apply transformation in a more controlled way (not by default - except when required).
str,
),
SheetFieldMap("reference_number", "FINDINGREFNUMS", "reference_number", None, str),
SheetFieldMap(
"modified_opinion",
"MODIFIEDOPINION",
"is_modified_opinion",
None,
uppercase_y_or_n,
),
SheetFieldMap(
"other_matters",
"OTHERNONCOMPLIANCE",
"is_other_matters",
None,
uppercase_y_or_n,
),
SheetFieldMap(
"material_weakness",
"MATERIALWEAKNESS",
"is_material_weakness",
None,
uppercase_y_or_n,
),
SheetFieldMap(
"significant_deficiency",
"SIGNIFICANTDEFICIENCY",
"is_significant_deficiency",
None,
uppercase_y_or_n,
),
SheetFieldMap(
"other_findings", "OTHERFINDINGS", "is_other_findings", None, uppercase_y_or_n
),
SheetFieldMap(
"questioned_costs", "QCOSTS", "is_questioned_costs", None, uppercase_y_or_n
),
SheetFieldMap(
"repeat_prior_reference",
"REPEATFINDING",
"is_repeat_finding",
None,
uppercase_y_or_n,
),
SheetFieldMap(
"prior_references",
"PRIORFINDINGREFNUMS",
"prior_finding_ref_numbers",
None,
xform_prior_year_findings,
),
]
def xform_construct_award_references(audits, findings):
"""Construct award references for findings."""
# Transformation recorded.
e2a = {}
for index, audit in enumerate(audits):
e2a[audit.ELECAUDITSID] = f"AWARD-{index + 1:04d}"
award_references = []
change_records = []
for find in findings:
award_references.append(e2a[find.ELECAUDITSID])
# Tracking changes
track_transformations(
"ELECAUDITSID",
find.ELECAUDITSID,
"award_reference",
e2a[find.ELECAUDITSID],
["xform_construct_award_references"],
change_records,
)
if change_records:
InspectionRecord.append_finding_changes(change_records)
return award_references
def _get_findings_grid(findings_list):
# The original copy of allowed_combos is in audit/intakelib/checks/check_findings_grid_validation.py
allowed_combos = {
"YNNNN",
"YNYNN",
"YNNYN",
"NYNNN",
"NYYNN",
"NYNYN",
"NNYNN",
"NNNYN",
"NNNNY",
}
attributes = [
"MODIFIEDOPINION",
"OTHERNONCOMPLIANCE",
"MATERIALWEAKNESS",
"SIGNIFICANTDEFICIENCY",
"OTHERFINDINGS",
]
return [
(
"Y"
if "".join(
(getattr(finding, attr, "") or "").strip() for attr in attributes
)
in allowed_combos
else "N"
)
for finding in findings_list
]
def get_findings(dbkey, year):
# CFDAs aka ELECAUDITS (or Audits) have elecauditid (FK). Findings have elecauditfindingsid, which is unique.
# The linkage here is that a given finding will have an elecauditid.
# Multiple findings will have a given elecauditid. That's how to link them.
results = Findings.objects.filter(DBKEY=dbkey, AUDITYEAR=year)
return sort_by_field(results, "ELECAUDITFINDINGSID")
def has_duplicate_ref_numbers(award_refs, findings):
"""Check if there are duplicate ref numbers for each award."""
ref_numbers = defaultdict(set)
for award_ref, finding in zip(award_refs, findings):
ref_number = string_to_string(finding.FINDINGREFNUMS)
if ref_number in ref_numbers[award_ref]:
return True
ref_numbers[award_ref].add(ref_number)
return False
def track_invalid_records_with_repeated_ref_numbers(award_references, findings):
"""Track invalid records with repeated ref numbers."""
has_duplicate_refs = has_duplicate_ref_numbers(award_references, findings)
if has_duplicate_refs:
invalid_records = []
for finding in findings:
ref_number = string_to_string(finding.FINDINGREFNUMS)
census_data_tuples = [
("FINDINGREFNUMS", finding.FINDINGREFNUMS),
("ELECAUDITSID", finding.ELECAUDITSID),
]
track_invalid_records(
census_data_tuples,
"reference_number",
ref_number,
invalid_records,
)
if invalid_records:
InvalidRecord.append_invalid_finding_records(invalid_records)
InvalidRecord.append_validations_to_skip(
"check_finding_reference_uniqueness"
)
InvalidRecord.append_invalid_migration_tag(
INVALID_MIGRATION_TAGS.DUPLICATE_FINDING_REFERENCE_NUMBERS
)
def xform_replace_required_fields_with_gsa_migration_when_empty(findings):
"""Replace empty fields with GSA_MIGRATION."""
fields_to_check = [
("MODIFIEDOPINION", "is_modified_opinion"),
("OTHERNONCOMPLIANCE", "is_other_matters"),
("MATERIALWEAKNESS", "is_material_weakness"),
("SIGNIFICANTDEFICIENCY", "is_significant_deficiency"),
("OTHERFINDINGS", "is_other_findings"),
("QCOSTS", "is_questioned_costs"),
("FINDINGREFNUMS", "reference_number"),
]
for in_db, in_dissem in fields_to_check:
_replace_empty_field(findings, in_db, in_dissem)
def _replace_empty_field(findings, name_in_db, name_in_dissem):
"""Replace empty fields with GSA_MIGRATION."""
change_records = []
has_empty_field = False
for finding in findings:
current_value = getattr(finding, name_in_db)
if not string_to_string(current_value):
has_empty_field = True
setattr(finding, name_in_db, settings.GSA_MIGRATION)
track_transformations(
name_in_db,
current_value,
name_in_dissem,
settings.GSA_MIGRATION,
["xform_replace_required_fields_with_gsa_migration_when_empty"],
change_records,
)
if change_records and has_empty_field:
InspectionRecord.append_finding_changes(change_records)
def xform_empty_repeat_prior_reference(findings):
"""Replace empty repeat prior reference with N if prior reference is empty else Y."""
change_records = []
has_empty_field = False
for finding in findings:
prior_finding_ref_nums = string_to_string(finding.PRIORFINDINGREFNUMS)
repeat_prior_reference = string_to_string(finding.REPEATFINDING)
if not repeat_prior_reference:
has_empty_field = True
finding.REPEATFINDING = "Y" if prior_finding_ref_nums else "N"
track_transformations(
"REPEATFINDING",
repeat_prior_reference,
"is_repeat_finding",
finding.REPEATFINDING,
["xform_missing_repeat_prior_reference"],
change_records,
)
if change_records and has_empty_field:
InspectionRecord.append_finding_changes(change_records)
def generate_findings(audit_header, outfile):
"""
Generates a federal awards audit findings workbook for all findings associated with a given audit header.
"""
logger.info(
f"--- generate findings {audit_header.DBKEY} {audit_header.AUDITYEAR} ---"
)
wb = pyxl.load_workbook(
sections_to_template_paths[FORM_SECTIONS.FINDINGS_UNIFORM_GUIDANCE]
)
uei = xform_retrieve_uei(audit_header.UEI)
set_workbook_uei(wb, uei)
audits = get_audits(audit_header.DBKEY, audit_header.AUDITYEAR)
findings = get_findings(audit_header.DBKEY, audit_header.AUDITYEAR)
award_references = xform_construct_award_references(audits, findings)
xform_sort_compliance_requirement(findings)
xform_replace_required_fields_with_gsa_migration_when_empty(findings)
xform_empty_repeat_prior_reference(findings)
xform_missing_compliance_requirement(findings)
map_simple_columns(wb, mappings, findings)
set_range(wb, "award_reference", award_references)
track_invalid_records_with_repeated_ref_numbers(award_references, findings)
grid = _get_findings_grid(findings)
# We need a magic "is_valid" column, which is computed in the workbook.
set_range(wb, "is_valid", grid, conversion_fun=str)
wb.save(outfile)
return wb