Skip to content

Commit

Permalink
fix json output with custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sadnub committed Nov 7, 2023
1 parent 374ff0a commit 7d0e023
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions api/tacticalrmm/ee/reporting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,45 +327,46 @@ def build_queryset(*, data_source: Dict[str, Any], limit: Optional[int] = None)
queryset = queryset.first()

if fields_to_add:
return add_custom_fields(
queryset = add_custom_fields(
data=queryset,
fields_to_add=fields_to_add,
model_name=model_name,
dict_value=True,
)

if isJson:
return json.dumps(queryset, default=str)
elif isCsv:
import pandas as pd

df = pd.DataFrame.from_dict([queryset])
df.drop("id", axis=1, inplace=True)
if csv_columns:
df = df.rename(columns=csv_columns)
return df.to_csv(index=False)
else:
if isJson:
return json.dumps(queryset, default=str)
elif isCsv:
import pandas as pd

df = pd.DataFrame.from_dict([queryset])
df.drop("id", axis=1, inplace=True)
if csv_columns:
df = df.rename(columns=csv_columns)
return df.to_csv(index=False)
else:
return queryset
return queryset
else:
# add custom fields for list results
queryset = list(queryset)

if fields_to_add:
return add_custom_fields(
data=list(queryset), fields_to_add=fields_to_add, model_name=model_name
queryset = add_custom_fields(
data=queryset, fields_to_add=fields_to_add, model_name=model_name
)

if isJson:
return json.dumps(queryset, default=str)
elif isCsv:
import pandas as pd

df = pd.DataFrame.from_dict(queryset)
df.drop("id", axis=1, inplace=True)
if csv_columns:
df = df.rename(columns=csv_columns)
return df.to_csv(index=False)
else:
if isJson:
return json.dumps(list(queryset), default=str)
elif isCsv:
import pandas as pd

df = pd.DataFrame.from_dict(list(queryset))
df.drop("id", axis=1, inplace=True)
print(csv_columns)
if csv_columns:
df = df.rename(columns=csv_columns)
return df.to_csv(index=False)
else:
return list(queryset)
return queryset


def add_custom_fields(
Expand Down

0 comments on commit 7d0e023

Please sign in to comment.