コード例 #1
0
def download_data(request):
    try:
        data_id = request.GET.get('data_id')
        user_id = request.GET.get('user_id')
        data_path = data_id + '.csv'
        data_cont_query = Datasets.objects.filter(
            data_id=data_id, user_id=user_id).values('data_cont')
        if len(data_cont_query) == 0:
            raise Exception('Oops! No access!')
        data_cont = list(data_cont_query)[0]['data_cont']
        pd.read_json(data_cont).to_csv(data_path, index=False)
        data_file = open(data_path, 'rb')

        response = FileResponse(data_file)
        response['Content-Type'] = 'application/octet-stream'
        response[
            'Content-Disposition'] = 'attachment;filename=\"' + data_id + '.csv\"'
    except Exception as e:
        response_content = {}
        response = HttpResponse()
        response_content['msg'] = str(e)
        response_content['error_num'] = 1
        response.write(json.dumps(response_content))

    return response
コード例 #2
0
def download_data(request):
    try:
        data_id = request.GET.get('data_id')
        user_id = request.GET.get('user_id')
        data_path = data_id + '.csv'

        fetched = Datasets.objects.filter(data_id=data_id,
                                          user_id=user_id).values(
                                              'hdfs_path', 'data_name')
        if len(fetched) == 0:
            raise Exception('Oops! No access!')
        if list(fetched)[0]['hdfs_path']:
            client = InsecureClient("http://hdfs.neurolearn.com:50070",
                                    user="******")
            client.download(list(fetched)[0]['hdfs_path'] + '/' +
                            list(fetched)[0]['data_name'],
                            data_path,
                            overwrite=True)
        else:
            data_cont_query = Datasets.objects.filter(
                data_id=data_id, user_id=user_id).values('data_cont')
            if len(data_cont_query) == 0:
                raise Exception('Oops! No access!')
            data_cont = list(data_cont_query)[0]['data_cont']
            pd.read_json(data_cont).to_csv(data_path, index=False)

        data_file = open(data_path, 'rb')

        response = FileResponse(data_file)
        response['Content-Type'] = 'application/octet-stream'
        response[
            'Content-Disposition'] = 'attachment;filename=\"' + data_id + '.csv\"'
    except Exception as e:
        traceback.print_exc()
        response_content = {}
        response = HttpResponse()
        response_content['msg'] = str(e)
        response_content['error_num'] = 1
        response.write(json.dumps(response_content))

    return response