Exemplo n.º 1
0
	def save_model(self, request, obj, form, change):
		form.save()
		obj.file_name = str(obj.upload_file)
		with transaction.atomic():
			obj.save()
		
		result = processCustomerMeterTrackImportFile.delay('utf-8', obj, request.user)
		task_id = result.id
		if result.traceback:
			obj.error_log = obj.error_log + ('\nTask ID:%s Error:%s'%(task_id, result.traceback))
		obj.save()
Exemplo n.º 2
0
def customer_csv_import_file(request, format=None):
    parser_classes = (MultiPartParser, FormParser,FileUploadParser,)
    if request.method == 'POST':
        try:
            upload_file = request.FILES['upload_file']
            file_name = get_file_path(None, request.DATA['file_name'])
            
            print("Processing customer_csv_import_file upload_file:%s, file_name:%s"%(upload_file, file_name))
            with default_storage.open(file_name, 'wb+') as temp_file:
                print("About to upload file chunks")
                for chunk in upload_file.chunks():
                    print("Uploading chunk")
                    temp_file.write(chunk)
            print("Finished uploading file: %s"%file_name)    
            cmti = CSVImportCustomerMeterTrack.objects.create(upload_file=file_name, 
                file_name=file_name,
                import_user=request.user,
                import_date=timezone.now(),
                upload_method='rest-api',)

            result = processCustomerMeterTrackImportFile.delay('utf-8', cmti, request.user.username)
            task_id = result.id
            if result.traceback:
                cmti.error_log = cmti.error_log + ('\nTask ID:%s Error:%s'%(task_id, result.traceback))
            cmti.save()
            
            serializer = CSVImportCustomerMeterTrackSerializer(cmti, many=False)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        except Exception as err:
            tb = traceback.format_exc()
            print (tb) 
            return Response(tb , status=status.HTTP_400_BAD_REQUEST)   
    elif request.method == 'GET':
        try:
            csv_customer_import_files = CSVImportCustomerMeterTrack.objects.all()
            serializer = CSVImportCustomerMeterTrackSerializer(csv_customer_import_files, many=True)
            return Response(serializer.data)
        except Exception as err:
            tb = traceback.format_exc()
            print (tb) 
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)