#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2017 EMBL - European Bioinformatics Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from rest_framework_json_api.renderers import JSONRenderer


class DefaultJSONRenderer(JSONRenderer):
    media_type = 'application/json'
    format = 'application/json'

@@ -639,16 +639,27 @@
 cense.%0A%0A
+import csv%0A
 %0Afrom re
@@ -710,16 +710,110 @@
 enderer%0A
+from rest_framework_csv.renderers import CSVRenderer%0Afrom rest_framework_csv.misc import Echo%0A
 %0A%0Aclass 
@@ -895,28 +895,922 @@
 format = 'application/json'%0A
+%0A%0Aclass CSVStreamingRenderer(CSVRenderer):%0A    %22%22%22%0A    Based on rest_framework_csv.renderers.CSVStreamingRenderer,%0A    tabilize() call is not iterator-friendly.%0A    %22%22%22%0A%0A    def render(self, data, media_type=None, renderer_context=%7B%7D):%0A%0A        queryset = data%5B'queryset'%5D%0A        serializer = data%5B'serializer'%5D%0A        context = data%5B'context'%5D%0A%0A        csv_buffer = Echo()%0A        csv_writer = csv.writer(csv_buffer)%0A%0A        header_fields = list()%0A        for item in queryset:%0A            if len(header_fields) %3C 1:%0A                header_fields = list(serializer(item, context=context).fields)%0A                yield csv_writer.writerow(header_fields)%0A            items = serializer(item, context=context).data%0A            ordered = %5Bitems%5Bcolumn%5D for column in header_fields%5D%0A            yield csv_writer.writerow(%5B%0A                elem.encode('utf-8') for elem in ordered%0A            %5D)%0A

implement csv renderer