def queryset_to_csv(self): """Override nautobot default to account for using Device model for GoldenConfig data.""" csv_data = [] custom_fields = [] # Start with the column headers headers = models.GoldenConfig.csv_headers.copy() # Add custom field headers, if any if hasattr(models.GoldenConfig, "_custom_field_data"): for custom_field in CustomField.objects.get_for_model( models.GoldenConfig): headers.append(custom_field.name) custom_fields.append(custom_field.name) csv_data.append(",".join(headers)) # Iterate through the queryset appending each object for obj in models.GoldenConfig.objects.all(): data = obj.to_csv() for custom_field in custom_fields: data += (obj.cf.get(custom_field, ""), ) csv_data.append(csv_format(data)) return "\n".join(csv_data)
def queryset_to_csv(self): """Export queryset of objects as comma-separated value (CSV).""" def conver_to_str(val): if val is False: # pylint: disable=no-else-return return "non-compliant" elif val is True: return "compliant" return "N/A" csv_data = [] headers = sorted(list(ConfigCompliance.objects.values_list("feature", flat=True).distinct())) csv_data.append(",".join(list(["Device name"] + headers))) for obj in self.alter_queryset(None).values(): # From all of the unique fields, obtain the columns, using list comprehension, add values per column, # as some fields may not exist for every device. row = [Device.objects.get(id=obj["device_id"]).name] + [ conver_to_str(obj.get(header)) for header in headers ] csv_data.append(csv_format(row)) return "\n".join(csv_data)
def queryset_to_csv(self): """Export queryset of objects as comma-separated value (CSV).""" def convert_to_str(val): if val is None: return "N/A" if bool(val) is False: return "non-compliant" if bool(val) is True: return "compliant" raise ValueError(f"Expecting one of 'N/A', 0, or 1, got {val}") csv_data = [] headers = sorted( list( models.ComplianceFeature.objects.values_list( "name", flat=True).distinct())) csv_data.append(",".join(list(["Device name"] + headers))) for obj in self.alter_queryset(None): # From all of the unique fields, obtain the columns, using list comprehension, add values per column, # as some fields may not exist for every device. row = [obj.get("device__name") ] + [convert_to_str(obj.get(header)) for header in headers] csv_data.append(csv_format(row)) return "\n".join(csv_data)