From ac7d76f3f1946803dc3a8c49e5155fbf0cc74b70 Mon Sep 17 00:00:00 2001
From: Harsha Kethineni <harshakethineni@flywheel.io>
Date: Wed, 6 Dec 2017 10:48:38 -0600
Subject: [PATCH] move file count check to finalize()

---
 api/placer.py | 33 ++++++++++++++++++++-------------
 api/upload.py |  2 --
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/api/placer.py b/api/placer.py
index 9e67722f..c52f12a1 100644
--- a/api/placer.py
+++ b/api/placer.py
@@ -142,6 +142,7 @@ class UIDPlacer(Placer):
         super(UIDPlacer, self).__init__(container_type, container, id_, metadata, timestamp, origin, context)
         self.metadata_for_file = {}
         self.session_id = None
+        self.count = 0
 
     def check(self):
         self.requireMetadata()
@@ -150,22 +151,25 @@ class UIDPlacer(Placer):
         metadata_validator = validators.from_schema_path(payload_schema_uri)
         metadata_validator(self.metadata, 'POST')
 
-        # If not a superuser request, pass uid of user making the upload request
-        targets = self.create_hierarchy(self.metadata, type_=self.match_type, user=self.context.get('uid'))
-
-        self.metadata_for_file = {}
-
-        for target in targets:
-            if target[0].level is 'session':
-                self.session_id = target[0].id_
-            for name in target[1]:
-                self.metadata_for_file[name] = {
-                    'container': target[0],
-                    'metadata': target[1][name]
-                }
 
 
     def process_file_field(self, field, file_attrs):
+        # Only create the hierarchy once
+        if self.count == 0:
+            # If not a superuser request, pass uid of user making the upload request
+            targets = self.create_hierarchy(self.metadata, type_=self.match_type, user=self.context.get('uid'))
+
+            self.metadata_for_file = {}
+
+            for target in targets:
+                if target[0].level is 'session':
+                    self.session_id = target[0].id_
+                for name in target[1]:
+                    self.metadata_for_file[name] = {
+                        'container': target[0],
+                        'metadata': target[1][name]
+                    }
+        self.count += 1
 
         # For the file, given self.targets, choose a target
         name        = field.filename
@@ -195,6 +199,9 @@ class UIDPlacer(Placer):
         self.saved.append(file_attrs)
 
     def finalize(self):
+        # Check that there is at least one file being uploaded
+        if self.count < 1:
+            raise FileFormException("No files selected for upload")
         if self.session_id:
             self.container_type = 'session'
             self.id_ = self.session_id
diff --git a/api/upload.py b/api/upload.py
index cfc779d7..7bf25f9b 100644
--- a/api/upload.py
+++ b/api/upload.py
@@ -94,8 +94,6 @@ def process_upload(request, strategy, container_type=None, id_=None, origin=None
     # Ref docs from placer.TargetedPlacer for details.
     if strategy == Strategy.targeted and len(file_fields) > 1:
         raise FileFormException("Targeted uploads can only send one file")
-    elif strategy in [Strategy.reaper, Strategy.uidupload, Strategy.labelupload, Strategy.uidmatch] and len(file_fields) < 1:
-        raise FileFormException("No files selected for uploading")
     placer.check()
 
 
-- 
GitLab