Skip to content

Commit

Permalink
fix some mypy type errors and pdf-is-not-empty errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wittejm committed May 19, 2024
1 parent 35e5ddb commit b4ded00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
Binary file modified src/backend/expungeservice/files/OSP_Form.pdf
Binary file not shown.
25 changes: 13 additions & 12 deletions src/backend/expungeservice/form_filling.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class UserInfo:
zip_code: str
phone_number: str
counties_with_cases_to_expunge: List[str]
has_eligible_convictions: str
has_eligible_convictions: bool


@dataclass
Expand All @@ -160,7 +160,7 @@ def build(cls, case: Case, user_info_dict: Dict[str, str], sid: str):
has_no_balance=case.summary.balance_due_in_cents == 0,
da_number=case.summary.district_attorney_number,
counties_with_cases_to_expunge=[],
has_eligible_convictions="",
has_eligible_convictions=False,
**user_info_dict,
)

Expand Down Expand Up @@ -394,9 +394,9 @@ def extra_mappings(self):
s = self.source_data
if not isinstance(s, CaseResults):
# Is the OSP Form
osp_fields = {}
for i in range(len(s.counties_with_cases_to_expunge[:10])):
osp_fields[f"(Court {i+1})"] = f"Circuit Court for {s.counties_with_cases_to_expunge[i]} County"
osp_fields: Dict[str, object] = {}
for i in range(10):
osp_fields[f"(Court {i+1})"] = f"Circuit Court for {s.counties_with_cases_to_expunge[i]} County" if i<len(s.counties_with_cases_to_expunge) else ""
if s.has_eligible_convictions:
osp_fields["(Include a Conviction Yes)"] = True
else:
Expand Down Expand Up @@ -555,10 +555,10 @@ def validate_initial_state(self):
not_blank_message = lambda elem, type: f"[PDF] PDF {type} not blank: {elem.T} - {elem.V}"

for field in self._pdf.Root.AcroForm.Fields:
assert field.V is None, not_blank_message(field, "field")
assert field.V in [None, "/Off", "()"], not_blank_message(field, "field")

for annotation in self.annotations:
assert annotation.V is None, not_blank_message(annotation, "annotation")
assert annotation.V in [None, "/Off", "()"], not_blank_message(annotation, "annotation")

assert set(self.get_field_dict()) == set(
self.get_annotation_dict()
Expand Down Expand Up @@ -592,11 +592,12 @@ def build_zip(record_summary: RecordSummary, user_information_dict: Dict[str, st
if case_results.is_expungeable_now:
file_info = FormFilling._create_and_write_pdf(case_results, temp_dir)
zip_file.write(*file_info)
user_information_dict["counties_with_cases_to_expunge"] = FormFilling.counties_with_cases_to_expunge(
user_information_dict_2: Dict[str, object] = {**user_information_dict}
user_information_dict_2["counties_with_cases_to_expunge"] = FormFilling.counties_with_cases_to_expunge(
all_case_results
)
user_information_dict["has_eligible_convictions"] = "True" if has_eligible_convictions else ""
osp_file_info = FormFilling._create_and_write_pdf(user_information_dict, temp_dir)
user_information_dict_2["has_eligible_convictions"] = has_eligible_convictions
osp_file_info = FormFilling._create_and_write_pdf(user_information_dict_2, temp_dir)
zip_file.write(*osp_file_info)
zip_file.close()

Expand Down Expand Up @@ -676,7 +677,7 @@ def _create_pdf(source_data: UserInfo, validate_initial_pdf_state=False) -> PDF:

@staticmethod
def _create_and_write_pdf(
data: Union[Dict[str, str], UserInfo], dir: str, validate_initial_pdf_state=False
data: Union[Dict, UserInfo], dir: str, validate_initial_pdf_state=False
) -> Tuple[str, str]:
if isinstance(data, UserInfo):
source_data = data
Expand All @@ -696,7 +697,7 @@ def _create_and_write_pdf(

@staticmethod
def counties_with_cases_to_expunge(all_case_results: List[CaseResults]):
counties = []
counties: List[str] = []
for case_result in all_case_results:
if case_result.has_eligible_charges and case_result.case.summary.location not in counties:
counties.append(case_result.case.summary.location)
Expand Down
10 changes: 7 additions & 3 deletions src/backend/tests/test_form_filling.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from tempfile import mkdtemp
from zipfile import ZipFile
from typing import Dict, List, Callable, Any
from typing import Dict, List, Callable, Any, Union
from pathlib import Path
import pickle
from datetime import datetime
Expand Down Expand Up @@ -53,7 +53,7 @@ def assert_pdf_values(pdf: PDF, expected: Dict[str, str]):
for key in set(annotation_dict) - set(expected):
value = annotation_dict[key].V
if annotation_dict[key].FT == PDF.TEXT_TYPE:
assert value is None, key
assert value in [None, "<>"], key
if annotation_dict[key].FT == PDF.BUTTON_TYPE:
assert value != PDF.BUTTON_ON, key

Expand Down Expand Up @@ -310,7 +310,11 @@ def test_user_info_is_placed_in_osp_form(self):
"(State)": "(OR)",
"(Zip Code)": "(97111)",
}
user_info = UserInfo(**user_data)
user_info = UserInfo(
counties_with_cases_to_expunge=[],
has_eligible_convictions=False,
**user_data,
)
pdf = FormFilling._create_pdf(user_info, validate_initial_pdf_state=True)
assert_pdf_values(pdf, expected_values)

Expand Down

0 comments on commit b4ded00

Please sign in to comment.