Skip to content

Commit

Permalink
adjusting exportTableAsGzipFileMixin so that it actually exports as a…
Browse files Browse the repository at this point in the history
… gzip; fixes #41
  • Loading branch information
cmatKhan committed Jul 7, 2024
1 parent be1c421 commit 11de34c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gzip
from io import BytesIO

import pandas as pd
from django.http import HttpResponse
from rest_framework.decorators import action
Expand All @@ -13,17 +16,23 @@ def export(self, request):
# Get the queryset and apply any filters
queryset = self.filter_queryset(self.get_queryset())

# Convert the filtered queryset to a DataFrame
df = pd.DataFrame.from_records(queryset.values())
# Serialize the queryset
serializer = self.get_serializer(queryset, many=True)
serialized_data = serializer.data

# Create a HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = f'attachment; filename="{self.queryset.model.__name__}.csv.gz"'
# Convert the serialized data to a DataFrame
df = pd.DataFrame.from_records(serialized_data)

# Write the DataFrame to a CSV string and compress it
csv_data = df.to_csv(index=False, compression="gzip")
# Create a CSV string
csv_data = df.to_csv(index=False)

# Set the compressed CSV data as the content of the response
response.content = csv_data
# Compress the CSV data using gzip
gzip_buffer = BytesIO()
with gzip.GzipFile(fileobj=gzip_buffer, mode="w") as f:
f.write(csv_data.encode("utf-8"))

# Create a HttpResponse object with the appropriate CSV header.
response = HttpResponse(gzip_buffer.getvalue(), content_type="application/gzip")
response["Content-Disposition"] = f'attachment; filename="{self.queryset.model.__name__}.csv.gz"'

return response
9 changes: 6 additions & 3 deletions yeastregulatorydb/regulatory_data/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def test_genomic_feature_filter():
filter_params = [
{"chr": "chr1"},
{"start_min": 1, "start_max": 50}, # Test range filter for start
{"end_min": 50, "end_max": 150}, # Test range filter for end
{"end_min": 50, "end_max": 150}, # Test range filter for end
{"strand": "+"},
{"type": "type1"},
{"locus_tag": "tag1"},
Expand All @@ -452,15 +452,18 @@ def test_genomic_feature_filter():
# Additional test cases for range filters
range_filter_params = [
{"start_min": 0, "start_max": 200}, # Both genomic_feature1 and genomic_feature2 should be included
{"end_min": 1, "end_max": 150}, # Only genomic_feature1 should be included
{"end_min": 1, "end_max": 150}, # Only genomic_feature1 should be included
]

# Apply each range filter and check the expected results
for params in range_filter_params:
f = GenomicFeatureFilter(params, queryset=GenomicFeature.objects.all())
if "start_min" in params or "start_max" in params:
assert genomic_feature1 in f.qs, f"Failed for range filter params: {params}"
assert genomic_feature2 in f.qs, f"Failed for range filter params: {params}" if params["start_max"] == 200 else assert genomic_feature2 not in f.qs, f"Failed for range filter params: {params}"
if params.get("start_max", None) == 200:
assert genomic_feature2 in f.qs, f"Failed for range filter params: {params}"
else:
assert genomic_feature2 not in f.qs, f"Failed for range filter params: {params}"
if "end_min" in params or "end_max" in params:
assert genomic_feature1 in f.qs, f"Failed for range filter params: {params}"
assert genomic_feature2 not in f.qs, f"Failed for range filter params: {params}"
Expand Down

0 comments on commit 11de34c

Please sign in to comment.