def communicate_results( sourcerecords: List[SourceRecord], analysis: Analysis, output_json_path: str, output_html_path: str, email_address, ) -> None: """ Communicate the results of the record screening. Right now, this just means print them out. """ sources = [] for sr in to_serializable(sourcerecords): sr.pop("raw_source") sr.pop("parsed_source") sources.append(sr) results = {"sourcerecords": sources, "analysis": to_serializable(analysis)} message_builder = EmailBuilder(sources, analysis) if output_json_path is not None: with open(output_json_path, "w") as f: f.write(json.dumps(results, indent=4)) logger.info(f" Analysis written to {output_json_path}.") if output_html_path is not None: with open(output_html_path, "w") as f: html_message = message_builder.html() f.write(html_message) if email_address is not None: message_builder.email(email_address)
def post(self, request): """ Analyze a Criminal Record for expungeable and sealable cases and charges. POST body should be json-endoded CRecord object. Return, if not an error, will be a json-encoded Decision that explains the expungements and sealings that can be generated for this record. """ try: serializer = CRecordSerializer(data=request.data) if serializer.is_valid(): rec = CRecord.from_dict(serializer.validated_data) analysis = (Analysis(rec).rule(expunge_deceased).rule( expunge_over_70).rule(expunge_nonconvictions).rule( expunge_summary_convictions).rule(seal_convictions)) return Response(to_serializable(analysis)) return Response( {"validation_errors": serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR, ) except Exception as err: logger.error(err) return Response("Something went wrong", status=status.HTTP_400_BAD_REQUEST)
def test_serializing_person(example_person): ser = to_serializable(example_person) assert ser["first_name"] == example_person.first_name assert ser["aliases"] == example_person.aliases deser = PersonSerializer(data=ser) assert deser.is_valid(), deser.error_messages deser = deser.validated_data deser = Person.from_dict(deser) assert isinstance(deser, Person) assert deser == example_person
def test_serialize(): """ A sourcerecord should serialize to friendly json-style object """ path = "tests/data/summaries" files = os.listdir(path) if not os.path.exists(path) or len(files) == 0: pytest.fail(f"Need to put example summary files at {path}") first = files[0] try: src = SourceRecord(os.path.join(path, first), parser=parse_pdf) src = to_serializable(src) except Exception as err: pytest.fail(str(err))
def test_person_todict(): per = Person("John", "Smeth", date(2010, 1, 1), aliases=["JJ", "Smelly"], ssn="999-99-9999", address=Address(line_one="1234 Main St.", city_state_zip="Philadelphia, PA 19103")) assert to_serializable(per) == { "first_name": "John", "last_name": "Smeth", "date_of_birth": date(2010, 1, 1).isoformat(), "aliases": ["JJ", "Smelly"], "ssn": "999-99-9999", "address": { "line_one": "1234 Main St.", "city_state_zip": "Philadelphia, PA 19103" } }
def test_case_serialize(example_case): """ Serialize a case.""" serialized = to_serializable(example_case) assert "docket_number" in serialized.keys() assert "otn" in serialized.keys()
def test_serialize_summary(example_summary): ser = to_serializable(example_summary) assert "_cases" in ser.keys()
def test_serialize_docket(example_docket): ser = to_serializable(example_docket) assert "_case" in ser.keys()
def test_serialize_none(): serialized = to_serializable(None) assert serialized == ""
def test_case_todict(example_case): """ to_serializable transforms a RecordLib case into a json object""" assert to_serializable(example_case)["county"] == example_case.county
def test_from_dict(example_case): assert Case.from_dict({"invalid": "stuff"}) is None ser = to_serializable(example_case) c2 = Case.from_dict(ser) assert c2.docket_number == example_case.docket_number
def test_person_from_dict(example_person): ser = to_serializable(example_person) per2 = Person.from_dict(ser) assert example_person.last_name == per2.last_name assert isinstance(per2.date_of_birth, date)
def test_from_dict(example_crecord): serialized = to_serializable(example_crecord) crec2 = CRecord.from_dict(serialized) assert example_crecord.person.last_name == crec2.person.last_name