Esempio n. 1
0
def _queryset_to_xlsx_worksheet(queryset, workbook, exclude_fields=None):
    multivalues_separator = "\n"

    model_class = queryset.model
    model_name = model_class._meta.model_name

    fieldnames = get_serializer_fields(model_class)
    exclude_fields = exclude_fields or []
    fieldnames = [field for field in fieldnames if field not in exclude_fields]

    worksheet = workbook.add_worksheet(model_name)
    worksheet.write_row(row=0, col=0, data=fieldnames)

    for row_index, record in enumerate(queryset.iterator(), start=1):
        for col_index, field in enumerate(fieldnames):
            value = getattr(record, field)
            if not value:
                continue
            elif field == "license_expressions":
                value = combine_expressions(value)
            elif isinstance(value, list):
                value = [
                    list(entry.values())[0]
                    if isinstance(entry, dict) else str(entry)
                    for entry in value
                ]
                value = multivalues_separator.join(ordered_unique(value))
            elif isinstance(value, dict):
                value = json.dumps(value) if value else ""

            worksheet.write_string(row_index, col_index, str(value))
Esempio n. 2
0
def to_csv(project):
    """
    Generate results output for the provided `project` as csv format.
    Since the csv format does not support multiple tabs, one file is created
    per object type.
    The output files are created in the `project` output/ directory.
    Return the list of path of the generated output files.
    """
    querysets = [
        project.discoveredpackages.all(),
        project.codebaseresources.without_symlinks(),
    ]

    output_files = []
    for queryset in querysets:
        model_class = queryset.model
        fieldnames = get_serializer_fields(model_class)

        model_name = model_class._meta.model_name
        output_filename = project.get_output_file_path(f"{model_name}", "csv")

        with output_filename.open("w") as output_file:
            queryset_to_csv_file(queryset, fieldnames, output_file)

        output_files.append(output_filename)

    return output_files
Esempio n. 3
0
 def test_scanpipe_api_serializer_get_serializer_fields(self):
     self.assertEqual(28, len(get_serializer_fields(DiscoveredPackage)))
     self.assertEqual(21, len(get_serializer_fields(CodebaseResource)))
     with self.assertRaises(LookupError):
         get_serializer_fields(None)
Esempio n. 4
0
 def __init__(self, model_class, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.fields["include_fields"].choices = [
         (field, field) for field in get_serializer_fields(model_class)
     ]