Esempio n. 1
0
 def put(self, request, pk, format=None):
   dataset = self.get_object(pk)
   serializer = DatasetSerializer(dataset, data=request.DATA)
   if serializer.is_valid():
     serializer.object.owner = User.objects.get(username=request.user.username)
     if request.FILES:
       file_obj = request.FILES['file']
       file_obj = read_csv(file_obj, sep=',', header=0)
       serializer.save()
       Variable.objects.filter(dataset=dataset).delete()
       for column in file_obj:
         #todo: make this detect the datatype instead of saving everything as string.
         (datatype, values) = return_type_and_format_values(file_obj[column])
         values = values if values is not None else file_obj[column]
         datatype = datatype if datatype else 'undefined'
         subtype = return_default_subtype(datatype)
         v = Variable(name=column, dataset=serializer.object, datatype=datatype, values=values)
         v.save()
     else:
       serializer.save()
     return Response(serializer.data, status=status.HTTP_200_OK)
   else:
     return Response(status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
  def post(self, request, format=None):
    serializer = DatasetSerializer(data=request.DATA)

    if serializer.is_valid():
      serializer.object.owner = User.objects.get(username=request.user.username)
      if request.FILES:
        file_obj = request.FILES['file']
        file_obj = read_csv(file_obj, sep=',', header=0)
        serializer.save()

        for column in file_obj:
          (datatype, values) = return_type_and_format_values(file_obj[column])
          values = values if values is not None else file_obj[column]
          datatype = datatype if datatype else 'undefined'
          subtype = return_default_subtype(datatype)
          v = Variable(name=column, dataset=serializer.object, datatype=datatype, subtype=subtype, values=values)
          v.save()

      else:
        serializer.save()
      return Response(serializer.data, status=status.HTTP_201_CREATED)
    else:
      return Response(status=status.HTTP_400_BAD_REQUEST)