acadsamplesdb / acad_samples

ACAD samples database
0 stars 0 forks source link

Priority enhancement for batch upload of files #23

Closed sgoodswen closed 8 years ago

sgoodswen commented 8 years ago

Currently, there is an [upload files] option in Edit Sample to attach one file to a sample at a time. There are hundreds of files to upload and an urgent enhancement is required to batch upload files (this was mentioned at the Lab meeting that you recently attended).

Ideally, the upload program would read the files in a directory and upload the file to the appropriate ACAD Sample ID taken from the file name.

Here are some examples of the filenames:

ACAD15627A.JPG‎ ACAD15627B.JPG ACAD15631A.JPG‎ ACAD15631B.JPG‎ ACAD15638A.JPG‎ ACAD15638B.JBG ACAD15640A.JPG‎ ACAD15640B.JPG‎ ACAD15658A.JPG‎ ACAD15658B.JPG‎

For example, 2 files (ACAD15627A.JPG and ‎ ACAD15627B.JPG) Would be attached to the SAMPLE ID ACAD15627

Li-ReDBox commented 8 years ago

One solution may be incorporated in later on UI:

diff --git a/samples/templates/sample/detail.html b/samples/templates/sample/detail.html
index 831c5f4..582b298 100644
--- a/samples/templates/sample/detail.html
+++ b/samples/templates/sample/detail.html
@@ -59,9 +59,9 @@
     {% csrf_token %}
     <div class="form-group">
         <input type="hidden" name="next" value="{{ sample.get_absolute_url }}">
-        <input id="id_file" name="file" type="file" />
+        <input id="id_file" name="file" type="file" multiple>
     </div>
-    <input class="btn btn-success" type="submit" value="Upload" />
+    <input class="btn btn-success" type="submit" value="Upload" >
 </form>
 </div>
 </div></div>
diff --git a/samples/views.py b/samples/views.py
index 8de4bb2..4a80af6 100644
--- a/samples/views.py
+++ b/samples/views.py
@@ -773,13 +773,16 @@ def attach_file(request, pk, model):

         if form.is_valid():
             from django.forms import ValidationError
-            file = FileAttachment(file = request.FILES["file"])
-            try:
-                file.save()
-            except ValidationError as e:
-                return HttpResponse(e)
-            object.file.add(file)
-            object.save()
+            uploaded = request.FILES.getlist('file')
+            for uf in uploaded:
+                file = FileAttachment(file = uf)
+                try:
+                    file.save()
+                except ValidationError as e:
+                    return HttpResponse(e)
+                object.file.add(file)
+                object.save()
+
             cleaned_data = form.cleaned_data
             if cleaned_data["next"]:
                 return HttpResponseRedirect(cleaned_data["next"] + "#files")