Commits

Matthew Frazier committed 5834730

added implicit folders - fixes #1

  • Participants
  • Parent commits 653dff1

Comments (0)

Files changed (2)

File flaskext/uploads.py

         :param storage: The uploaded file to save.
         :param folder: The subfolder within the upload set to save to.
         :param name: The name to save the file as. If it ends with a dot, the
-                     file's extension will be appended to the end.
+                     file's extension will be appended to the end. (If you
+                     are using `name`, you can include the folder in the
+                     `name` instead of explicitly using `folder`, i.e.
+                     ``uset.save(file, name="someguy/photo_123.")``
         """
         if not isinstance(storage, FileStorage):
             raise TypeError("storage must be a werkzeug.FileStorage")
         
+        if folder is None and name is not None and "/" in name:
+            folder, name = name.rsplit("/", 1)
+        
         basename = lowercase_ext(secure_filename(storage.filename))
         if name:
             if name.endswith('.'):

File tests/test-uploads.py

         assert res == 'someguy/photo_123.jpg'
         assert tfs.saved == '/uploads/someguy/photo_123.jpg'
     
+    def test_implicit_folder(self):
+        uset = UploadSet('files')
+        uset._config = Config('/uploads')
+        tfs = TestingFileStorage(filename='boat.jpg')
+        res = uset.save(tfs, name='someguy/photo_123.')
+        assert res == 'someguy/photo_123.jpg'
+        assert tfs.saved == '/uploads/someguy/photo_123.jpg'
+    
     def test_secured_filename(self):
         uset = UploadSet('files', ALL)
         uset._config = Config('/uploads')