def update_case(lasc, clean_data): """Update an existing case with new data Method currently deletes and replaces the data on the system except for lasc_docket and connections for older json and pdf files. :param lasc: A LASCSearch object :param clean_data: A normalized data dictionary :return: None """ case_id = make_case_id(clean_data) with transaction.atomic(): docket = Docket.objects.filter(case_id=case_id)[0] docket.__dict__.update(clean_data["Docket"]) docket.save() skipped_models = [ "Docket", "QueuedPDF", "QueuedCase", "LASCPDF", "LASCJSON", "DocumentImage", ] models = [ x for x in apps.get_app_config("lasc").get_models() if x.__name__ not in skipped_models ] while models: mdl = models.pop() while clean_data[mdl.__name__]: row = clean_data[mdl.__name__].pop() row["docket"] = docket mdl.objects.create(**row).save() documents = clean_data["DocumentImage"] for row in documents: dis = DocumentImage.objects.filter(doc_id=row["doc_id"]) dis_count = dis.count() if dis_count == 1: di = dis[0] row["is_available"] = di.is_available di.__dict__.update(**row) elif dis_count == 0: row["docket"] = docket di = DocumentImage(**row) di.save() logger.info("Finished updating lasc case '%s'", case_id) save_json(lasc.case_data, content_obj=docket)
def add_case_from_filepath(filepath): """Add case to database from filepath :param self: The celery object :param filepath: Filepath string to where the item is stored :return: None """ query = LASCSearch(None) with open(filepath, 'r') as f: original_data = f.read() case_data = query._parse_case_data(json.loads(original_data)) case_id = make_case_id(case_data) ds = Docket.objects.filter(case_id=case_id) if ds.count() == 0: add_case(case_id, case_data, original_data) elif ds.count() == 1: logger.warn("LASC case on file system at '%s' is already in " "the database ", filepath)