def METHOD_NAME(  # noqa: PLR0912, PLR0915, C901
    deployments_report_dict, request
):
    """Create deployments report csv."""
    deployments_report_dict = deepcopy(deployments_report_dict)
    mask_report = request.query_params.get("mask", False)
    source_headers = {SOURCES_KEY, *_get_detection_keys()}
    report_id = deployments_report_dict.get("report_id")
    if report_id is None:
        return None
    deployment_report = DeploymentsReport.objects.filter(report_id=report_id).first()
    if deployment_report is None:
        return None
    # Check for a cached copy of csv
    cached_csv = deployment_report.cached_csv
    if validate_query_param_bool(mask_report):
        cached_csv = deployment_report.cached_masked_csv
    if cached_csv:
        logger.info("Using cached csv results for deployment report %d", report_id)
        return cached_csv
    logger.info("No cached csv results for deployment report %d", report_id)
    csv_helper = CSVHelper()
    deployment_report_buffer = StringIO()
    csv_writer = csv.writer(deployment_report_buffer, delimiter=",")
    csv_writer.writerow(
        ["Report ID", "Report Type", "Report Version", "Report Platform ID"]
    )
    csv_writer.writerow(
        [
            report_id,
            deployment_report.report_type,
            deployment_report.report_version,
            deployment_report.report_platform_id,
        ]
    )
    csv_writer.writerow([])
    csv_writer.writerow([])
    systems_list = deployments_report_dict.get("system_fingerprints")
    if not systems_list:
        return None
    csv_writer.writerow(["System Fingerprints:"])
    valid_fact_attributes = {
        field.name for field in SystemFingerprint._meta.get_fields()
    }
    # Add fields to just one fingerprint
    system = systems_list[0]
    for attr in valid_fact_attributes:
        if not system.get(attr, None):
            system[attr] = None
    headers = csv_helper.generate_headers(
        systems_list,
        exclude={
            "id",
            "report_id",
            "metadata",
            "deployment_report",
            "cpu_core_per_socket",
            "system_purpose",
        },
    )
    if SOURCES_KEY in headers:
        headers += source_headers
        headers = sorted(list(set(headers)))
    # Add source headers
    csv_writer.writerow(headers)
    for system in systems_list:
        row = []
        system_sources = system.get(SOURCES_KEY)
        if system_sources is not None:
            sources_info = compute_source_info(system_sources)
        else:
            sources_info = None
        for header in headers:
            fact_value = None
            if header in source_headers:
                if sources_info is not None:
                    fact_value = sources_info.get(header)
            elif header == "entitlements":
                fact_value = system.get(header)
                if fact_value:
                    for entitlement in fact_value:
                        entitlement.pop("metadata")
            else:
                fact_value = system.get(header)
            row.append(csv_helper.serialize_value(header, fact_value))
        csv_writer.writerow(sanitize_row(row))
    csv_writer.writerow([])
    logger.info("Caching csv results for deployment report %d", report_id)
    cached_csv = deployment_report_buffer.getvalue()
    if validate_query_param_bool(mask_report):
        deployment_report.cached_masked_csv = cached_csv
    else:
        deployment_report.cached_csv = cached_csv
    deployment_report.save()
    return cached_csv