def _search_matches(submission, user): patient_data = get_submission_json_for_external_match(submission) nodes_to_query = [node for node in MME_NODES.values() if node.get('url')] if not nodes_to_query: message = 'No external MME nodes are configured' return create_json_response({'message': message}, status=400, reason=message) external_results = _search_external_matches(nodes_to_query, patient_data) local_results, incoming_query = get_mme_matches(patient_data, user=user) results = local_results + external_results initial_saved_results = { result.result_data['patient']['id']: result for result in MatchmakerResult.objects.filter(submission=submission) } new_results = [] saved_results = {} for result in results: saved_result = initial_saved_results.get(result['patient']['id']) if not saved_result: saved_result = MatchmakerResult.objects.create( submission=submission, originating_query=incoming_query, result_data=result, last_modified_by=user, ) new_results.append(result) else: saved_result.result_data = result saved_result.save() saved_results[result['patient']['id']] = saved_result if new_results: try: _generate_notification_for_seqr_match(submission, new_results) except Exception as e: logger.error('Unable to create notification for new MME match: {}'.format(str(e))) logger.info('Found {} matches for {} ({} new)'.format(len(results), submission.submission_id, len(new_results))) removed_patients = set(initial_saved_results.keys()) - set(saved_results.keys()) removed_count = 0 for patient_id in removed_patients: saved_result = initial_saved_results[patient_id] if saved_result.we_contacted or saved_result.host_contacted or saved_result.comments: if not saved_result.match_removed: saved_result.match_removed = True saved_result.save() removed_count += 1 saved_results[patient_id] = saved_result else: saved_result.delete() removed_count += 1 if removed_count: logger.info('Removed {} old matches for {}'.format(removed_count, submission.submission_id)) return _parse_mme_results(submission, saved_results.values(), user)
def reverse_migrate_submissions(apps, schema_editor): MatchmakerSubmission = apps.get_model("matchmaker", "MatchmakerSubmission") db_alias = schema_editor.connection.alias for submission in MatchmakerSubmission.objects.using( db_alias).all().prefetch_related('individual'): ind = submission.individual ind.mme_deleted_by_id = submission.deleted_by_id ind.mme_deleted_date = submission.deleted_date ind.mme_submitted_date = submission.created_date ind.mme_submitted_data = get_submission_json_for_external_match( submission) ind.save()