-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
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") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
||
|
@@ -17,8 +17,10 @@ | |
|
||
|
||
class ManuscriptFolioSetView(APIView): | ||
serializer_class = SearchSerializer | ||
renderer_classes = (JSONRenderer,) | ||
renderer_classes = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
CSVRenderer, | ||
) | ||
|
||
def get(self, request, *args, **kwargs): | ||
solrconn = solr.SolrConnection(settings.SOLR_SERVER) | ||
|
@@ -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) |
There was a problem hiding this comment.
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 ourManuscriptFolioSetView
is now determining which renderer to use, it will choose theCSVRenderer
when the request is for "text/csv"