Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
course-discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
course-discovery
Commits
11da3045
Commit
11da3045
authored
May 22, 2018
by
muhammad-ammar
Committed by
Muhammad Ammar
May 24, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use CSVStreamingRenderer
ENT-824
parent
86ee1ab7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
13 deletions
+26
-13
course_discovery/apps/api/renderers.py
+2
-2
course_discovery/apps/api/v1/tests/test_views/test_catalogs.py
+22
-9
course_discovery/apps/api/v1/views/catalogs.py
+2
-2
No files found.
course_discovery/apps/api/renderers.py
View file @
11da3045
from
rest_framework_csv.renderers
import
CSVRenderer
from
rest_framework_csv.renderers
import
CSV
Streaming
Renderer
from
rest_framework_xml.renderers
import
XMLRenderer
...
...
@@ -12,7 +12,7 @@ class AffiliateWindowXMLRenderer(XMLRenderer):
root_tag_name
=
'merchant'
class
CourseRunCSVRenderer
(
CSVRenderer
):
class
CourseRunCSVRenderer
(
CSV
Streaming
Renderer
):
""" CSV renderer for course runs. """
header
=
[
'key'
,
...
...
course_discovery/apps/api/v1/tests/test_views/test_catalogs.py
View file @
11da3045
# pylint: disable=redefined-builtin,no-member
import
csv
import
datetime
import
urllib
from
io
import
StringIO
import
ddt
import
pytest
...
...
@@ -228,7 +230,7 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi
response
=
self
.
client
.
get
(
url
)
course_run
=
self
.
serialize_catalog_flat_course_run
(
self
.
course_run
)
expected
=
','
.
join
(
[
expected
=
[
course_run
[
'key'
],
course_run
[
'title'
],
course_run
[
'pacing_type'
],
...
...
@@ -269,17 +271,28 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi
str
(
course_run
[
'seats'
][
'verified'
][
'price'
]),
course_run
[
'seats'
][
'verified'
][
'currency'
],
course_run
[
'seats'
][
'verified'
][
'upgrade_deadline'
],
'
"{}"
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'type'
]),
'
"{}"
'
.
format
(
str
(
course_run
[
'seats'
][
'credit'
][
'price'
])),
'
"{}"
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'currency'
]),
'
"{}"
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'upgrade_deadline'
]),
'
"{}"
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'credit_provider'
]),
'
"{}"
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'credit_hours'
]),
'
{}
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'type'
]),
'
{}
'
.
format
(
str
(
course_run
[
'seats'
][
'credit'
][
'price'
])),
'
{}
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'currency'
]),
'
{}
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'upgrade_deadline'
]),
'
{}
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'credit_provider'
]),
'
{}
'
.
format
(
course_run
[
'seats'
][
'credit'
][
'credit_hours'
]),
course_run
[
'modified'
],
])
course_run
[
'course_key'
],
]
# collect streamed content
received_content
=
b
''
for
item
in
response
.
streaming_content
:
received_content
+=
item
# convert received content to csv for comparison
f
=
StringIO
(
received_content
.
decode
(
'utf-8'
))
reader
=
csv
.
reader
(
f
)
content
=
list
(
reader
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assert
In
(
expected
,
response
.
content
.
decode
(
'utf-8'
))
self
.
assert
Equal
(
set
(
expected
),
set
(
content
[
1
]
))
def
test_get
(
self
):
""" Verify the endpoint returns the details for a single catalog. """
...
...
course_discovery/apps/api/v1/views/catalogs.py
View file @
11da3045
import
datetime
from
django.db
import
transaction
from
django.http
import
HttpResponse
from
django.http
import
Streaming
HttpResponse
from
dry_rest_permissions.generics
import
DRYPermissions
from
rest_framework
import
status
,
viewsets
from
rest_framework.decorators
import
detail_route
...
...
@@ -167,7 +167,7 @@ class CatalogViewSet(viewsets.ModelViewSet):
)
data
=
CourseRunCSVRenderer
()
.
render
(
serializer
.
data
)
response
=
HttpResponse
(
data
,
content_type
=
'text/csv'
)
response
=
Streaming
HttpResponse
(
data
,
content_type
=
'text/csv'
)
response
[
'Content-Disposition'
]
=
'attachment; filename="catalog_{id}_{date}.csv"'
.
format
(
id
=
id
,
date
=
datetime
.
datetime
.
utcnow
()
.
strftime
(
'
%
Y-
%
m-
%
d-
%
H-
%
M'
)
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment