first draft of the dropbox via ajaxuploader functionality for testing
authorSeth Woodworth <seth@sethish.com>
Fri, 12 Jul 2013 19:26:38 +0000 (15:26 -0400)
committerSeth Woodworth <seth@sethish.com>
Fri, 12 Jul 2013 19:26:38 +0000 (15:26 -0400)
karmaworld/apps/ajaxuploader/backends/local.py
karmaworld/apps/ajaxuploader/views.py
karmaworld/templates/partial/dropbox_support.html

index 5efba15faf73ad3e85a597d4bc5f6e775db20762..0856082cb0f07d842f446d9a277b1895e53dd6a4 100644 (file)
@@ -91,8 +91,6 @@ class LocalUploadBackend(AbstractUploadBackend):
         unique_filename = False
         filename_suffix = 0
 
-        #print "orig filename: " + os.path.join(self._dir, filename)
-
         # Check if file at filename exists
         if os.path.isfile(os.path.join(self._dir, filename)):
             while not unique_filename:
index 4f34426856c7b571dfb32db72d6ba8a0f23d7809..cbf443b395734fd653e58c1b05c1737b23612337 100644 (file)
@@ -12,12 +12,17 @@ class AjaxFileUploader(object):
         self.get_backend = lambda: backend(**kwargs)
 
     def __call__(self,request):
-        print "ajax file uploader called"
+        """ Make the resulting AjaxFileUploader instance itself a callable object """
         return self._ajax_upload(request)
 
     def _ajax_upload(self, request):
+        #FIXME: `upload` is two possible types based on upload type
+
         if request.method == "POST":
             if request.is_ajax():
+                # This case is for:
+                # + async ajax upload
+
                 # the file is stored raw in the request
                 upload = request
                 is_raw = True
@@ -27,9 +32,32 @@ class AjaxFileUploader(object):
                     filename = request.GET['qqfile']
                 except KeyError:
                     return HttpResponseBadRequest("AJAX request not valid")
-            # not an ajax upload, so it was the "basic" iframe version with
-            # submission via form
+
+            elif 'drpbxURL' in request.GET:
+                # This case is for:
+                # + dropbox chooser URL submission
+                backend = self.get_backend()
+
+                # custom filename handler
+                filename = (backend.update_filename(request, filename)
+                            or filename)
+                # save the file
+                backend.setup(filename)
+
+                with open(settings.MEDIA_ROOT + filename, 'wb') as handle:
+                    request = requests.get('http://www.example.com/image.jpg', prefetch=False)
+
+                    # In requests 1.0, this would be:
+                    # request = requests.get('http://www.example.com/image.jpg', stream=True)
+
+                    for block in request.iter_content(1024):
+                        if not block:
+                            break
+
+                        handle.write(block)
             else:
+                # This is case is for:
+                # + fall-back normal file upload
                 is_raw = False
                 if len(request.FILES) == 1:
                     # FILES is a dictionary in Django but Ajax Upload gives
@@ -44,13 +72,15 @@ class AjaxFileUploader(object):
                     raise Http404("Bad Upload")
                 filename = upload.name
 
-            backend = self.get_backend()
+            if not backend:
+                backend = self.get_backend()
+
+                # custom filename handler
+                filename = (backend.update_filename(request, filename)
+                            or filename)
+                # save the file
+                backend.setup(filename)
 
-            # custom filename handler
-            filename = (backend.update_filename(request, filename)
-                        or filename)
-            # save the file
-            backend.setup(filename)
             success = backend.upload(upload, filename, is_raw)
             # callback
             extra_context = backend.upload_complete(request, filename, upload)
index 94e45f85d3efb24f01d42b54966af6e0cfcfce0d..4f9753d7994f9454b6e938e63db451ec8d38b040 100644 (file)
@@ -10,6 +10,7 @@
       success: function(file) {
                 console.log("Here is the file selected " + file[0].link);
                 console.log(file);
+                $('#id_drpbxURL').val(file[0].link);
               },
       }