Skip to content

Commit

Permalink
dataset download json
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaya224 committed May 29, 2024
1 parent 9c087a2 commit 0f27121
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions backend/dataset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,14 @@ def download(self, request, pk):
Accepted methods: GET
"""
export_type = request.GET.get("export_type", "csv")
print(f"Export type: {export_type}")
try:
# Get the dataset instance for the id
dataset_instance = DatasetInstance.objects.get(instance_id=pk)
except DatasetInstance.DoesNotExist:
return Response(status=status.HTTP_400_BAD_REQUEST)

dataset_model = apps.get_model("dataset", dataset_instance.dataset_type)
data_items = dataset_model.objects.filter(instance_id=pk)
data_items = dataset_model.objects.filter(instance_id=pk)
dataset_resource = resources.RESOURCE_MAP[dataset_instance.dataset_type]
exported_items = dataset_resource().export_as_generator(export_type, data_items)
if export_type == "TSV":
Expand All @@ -324,10 +323,9 @@ def download(self, request, pk):
# Convert each data item to a dictionary dynamically
exported_items = [model_to_dict(item) for item in data_items]
json_data = json.dumps(exported_items, default=str)

# Create a StreamingHttpResponse with the JSON data
response = HttpResponse(json_data, content_type="application/json")
response["Content-Disposition"] = 'attachment; filename="data.json"'
response = StreamingHttpResponse(json_data, content_type="application/json")
response["Content-Disposition"] = f'attachment; filename="{dataset_instance}.json"'
return response
return StreamingHttpResponse(
exported_items, status=status.HTTP_200_OK, content_type=content_type
Expand Down

0 comments on commit 0f27121

Please sign in to comment.