Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide csv export option for folio_set view #784

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/public/cantusdata/renderers/csv_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List
from rest_framework.renderers import BaseRenderer


class CSVRenderer(BaseRenderer):
"""
Provides a renderer class which serializes a response to CSV.
The renderer assumes that the response data is a list of dictionaries
with a consistent set of keys (which become the header row in the csv
result).
"""

media_type = "text/csv"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we say that the CSVRenderer should be used when the requested media type is "text/csv"...so when our ManuscriptFolioSetView is now determining which renderer to use, it will choose the CSVRenderer when the request is for "text/csv"

format = "csv"

def render(self, data, media_type=None, renderer_context=None):
headers: str = ",".join([str(key) for key in data[0].keys()])
rows: List[str] = [",".join([str(val) for val in row.values()]) for row in data]
csv_str: str = "\n".join([headers] + rows)
return bytes(csv_str, encoding="utf-8")
14 changes: 8 additions & 6 deletions app/public/cantusdata/views/folio_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.renderers import JSONRenderer
from cantusdata.serializers.search import SearchSerializer
from cantusdata.renderers.csv_renderer import CSVRenderer
import solr


Expand All @@ -17,8 +17,10 @@


class ManuscriptFolioSetView(APIView):
serializer_class = SearchSerializer
renderer_classes = (JSONRenderer,)
renderer_classes = (
Copy link
Collaborator Author

@dchiller dchiller Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we define the available renderers with this view. So whereas previously we only JSONRenderer, behind the scenes, django rest framework will look at incoming requests and try to determine which of these two renderers (JSONRenderer or CSVRenderer) should be used for the request.

JSONRenderer,
CSVRenderer,
)

def get(self, request, *args, **kwargs):
solrconn = solr.SolrConnection(settings.SOLR_SERVER)
Expand Down Expand Up @@ -65,14 +67,14 @@ def get(self, request, *args, **kwargs):
fields="number",
score=False,
)
return Response(results)
return Response(results.results)
# Otherwise, simply return the given manuscript's folios.
composed_request = f'type:"cantusdata_folio" AND manuscript_id:{manuscript_id}'
results = solrconn.query(
composed_request,
sort="number asc",
rows=1000,
rows=10000,
fields=FOLIO_FIELDS,
score=False,
)
return Response(results)
return Response(results.results)