Browse Source

adding upload_local_file function to remove the extra code in importer.mako (using the previously available code)

ayush.goyal 4 years ago
parent
commit
6518e63adb

+ 23 - 18
desktop/libs/indexer/src/indexer/api3.py

@@ -228,22 +228,13 @@ def guess_field_types(request):
   file_format = json.loads(request.POST.get('fileFormat', '{}'))
 
   if file_format['inputFormat'] == 'localfile':
-    upload_file = request.FILES['inputfile']
-    fs = FileSystemStorage()
-    name = fs.save(upload_file.name, upload_file)
-    reader = csv.reader(decode_utf8(upload_file))
-
-    sample = []
-    column_row = []
-
-    for count, row in enumerate(reader):
-      if count == 0:
-        column_row = row
-      elif count <= 5:
-        sample.append(row)
-        csv_data.append(row)
-      else:
-        csv_data.append(row)
+
+    if len(csv_data) <= 5:
+      sample = csv_data[1:]
+    else:
+      sample = csv_data[1:5]
+
+    column_row = csv_data[0]
 
     field_type_guesses = []
     for count, col in enumerate(column_row):
@@ -258,7 +249,6 @@ def guess_field_types(request):
     ]
 
     format_ = {
-      'file_url': fs.url(name),
       'columns': columns,
       'sample': sample
     }
@@ -549,7 +539,7 @@ def importer_submit(request):
         request,
         source,
         destination,
-        csv_data,
+        csv_data[1:],
         start_time
       )
     else:
@@ -737,3 +727,18 @@ def save_pipeline(request):
   response['message'] = request.POST.get('editorMode') == 'true' and _('Query saved successfully') or _('Notebook saved successfully')
 
   return JsonResponse(response)
+
+
+def upload_local_file(request):
+
+  upload_file = request.FILES['inputfile']
+  fs = FileSystemStorage()
+  name = fs.save(upload_file.name, upload_file)
+  reader = csv.reader(decode_utf8(upload_file))
+
+  csv_data.clear()
+
+  for row in reader:
+    csv_data.append(row)
+
+  return JsonResponse({'file_url': fs.url(name)})

+ 13 - 37
desktop/libs/indexer/src/indexer/templates/importer.mako

@@ -238,10 +238,7 @@ ${ commonheader(_("Importer"), "indexer", user, request, "60px") | n,unicode }
               <form method="post" action="" enctype="multipart/form-data" id="uploadform">
                 <div >
                     <input type="file" id="inputfile" name="inputfile" accept=".csv">
-                    <input type="button" class="button" value="Upload" id="but_upload">
                 </div>
-                <label for="path" class="control-label"><div>${ _('Path') }</div>
-                  <input type="text" id="file_path" data-bind="value: createWizard.source.path">
             </form>
           </div>
 
@@ -1790,7 +1787,7 @@ ${ commonheader(_("Importer"), "indexer", user, request, "60px") | n,unicode }
       // File
       self.path = ko.observable('');
       self.path.subscribe(function(val) {
-        if (val && self.inputFormat() != 'localfile') {
+        if (val) {
           wizard.guessFormat();
           wizard.destination.nonDefaultLocation(val);
         }
@@ -3161,49 +3158,28 @@ ${ commonheader(_("Importer"), "indexer", user, request, "60px") | n,unicode }
         };
         function upload() {
           var fd = new FormData();
-          fd.append('fileFormat', ko.mapping.toJSON(viewModel.createWizard.source));
+          var files = $('#inputfile')[0].files[0];
+          fd.append('inputfile', files);
+          var file_size = files.size;
+          if (file_size < 30*1000) {
             $.ajax({
-              url:"/indexer/api/indexer/guess_format",
+              url:"/indexer/api/indexer/upload_local_file",
               type: 'post',
               data: fd,
               contentType:false,
               cache: false,
               processData:false,
-              beforeSend: function (){
-                viewModel.createWizard.isGuessingFormat(true);
-              },
-              success:function (resp) {
-                var newFormat = ko.mapping.fromJS(new FileType(resp['type'], resp));
-                viewModel.createWizard.source.format(newFormat);
-                viewModel.createWizard.isGuessingFormat(false);
+              success:function (response) {
+                viewModel.createWizard.source.path(response['file_url']);
               }
+            
             });
+          }
+          else {
+            alert("As of now File size must be less than 30KB");
+          }
         };
 
-      $("#but_upload").click(function() {
-          var fd = new FormData();
-          var files = $('#inputfile')[0].files[0];
-          fd.append('inputfile', files);
-          fd.append('fileFormat', ko.mapping.toJSON(viewModel.createWizard.source));
-          ## $.post ??
-          $.ajax({
-              url: '/indexer/api/indexer/guess_field_types',
-              type: 'post',
-              data: fd,
-              contentType: false,
-              processData: false,
-              beforeSend: function(){
-                viewModel.createWizard.isGuessingFieldTypes(true);
-              },
-              success: function(response){
-                viewModel.createWizard.loadSampleData(response);
-                viewModel.createWizard.isGuessingFieldTypes(false);
-                viewModel.createWizard.source.path(response['file_url']);
-                document.getElementById("file_path").value = response['file_url'];
-              },
-          });
-      });
-
       $('.importer-droppable').droppable({
         accept: ".draggableText",
         drop: function (e, ui) {

+ 2 - 0
desktop/libs/indexer/src/indexer/urls.py

@@ -81,6 +81,8 @@ urlpatterns += [
 
   re_path(r'^api/importer/submit', indexer_api3.importer_submit, name='importer_submit'),
   re_path(r'^api/importer/save/?$', indexer_api3.save_pipeline, name='save_pipeline'),
+  
+  re_path(r'^api/indexer/upload_local_file/?$', indexer_api3.upload_local_file, name='upload_local_file'),
 ]
 
 urlpatterns += [