def export_response(request, filename, fields, data):
 output = request.GET.get('format', 'json')
 if (output not in ('json', 'csv')):
  output = 'json'
 if (output == 'csv'):
  response = HttpResponse(content_type='text/csv; charset=utf-8')
  response['Content-Disposition'] = ('attachment; filename=%s' % filename)
  writer = csv.DictWriter(response, fields)
  writer.writeheader()
  if six.PY2:
   for row in data:
    for item in row:
     if isinstance(row[item], six.text_type):
      row[item] = row[item].encode('utf-8')
    writer.writerow(row)
  else:
   for row in data:
    writer.writerow(row)
  return response
 return JsonResponse(data=data, safe=False, json_dumps_params={'indent': 2})

def export_response ( request , filename , fields , data ) : output = request . GET . get ( 'format' , 'json' ) if ( output not in ( 'json' , 'csv' ) ) : output = 'json' if ( output == 'csv' ) : response = HttpResponse ( content_type = 'text/csv; charset=utf-8' ) response [ 'Content-Disposition' ] = ( 'attachment; filename=%s' % filename ) writer = csv . DictWriter ( response , fields ) writer . writeheader ( ) if six . PY2 : for row in data : for item in row : if isinstance ( row [ item ] , six . text_type ) : row [ item ] = row [ item ] . encode ( 'utf-8' ) writer . writerow ( row ) else : for row in data : writer . writerow ( row ) return response return JsonResponse ( data = data , safe = False , json_dumps_params = { 'indent' : 2 } )
generic handler for stats exports .