import json

from django.http import HttpResponse, HttpResponseBadRequest, Http404

from ajaxuploader.backends.local import LocalUploadBackend

class AjaxFileUploader(object):
    def __init__(self, backend=None, **kwargs):
        if backend is None:
            backend = LocalUploadBackend
        self._backend = backend(**kwargs)

    def __call__(self,request):
        return self._ajax_upload(request)

    def _ajax_upload(self, request):
        if request.method == "POST":
            if request.is_ajax():
                # the file is stored raw in the request
                upload = request
                is_raw = True
                # AJAX Upload will pass the filename in the querystring if it
                # is the "advanced" ajax upload
                try:
                    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
            else:
                is_raw = False
                if len(request.FILES) == 1:
                    # FILES is a dictionary in Django but Ajax Upload gives
                    # the uploaded file an ID based on a random number, so it
                    # cannot be guessed here in the code. Rather than editing
                    # Ajax Upload to pass the ID in the querystring, observe
                    # that each upload is a separate request, so FILES should
                    # only have one entry. Thus, we can just grab the first
                    # (and only) value in the dict.
                    upload = request.FILES.values()[0]
                else:
                    raise Http404("Bad Upload")
                filename = upload.name

            # custom filename handler
            filename = (self._backend.update_filename(request, filename)
                        or filename)
            # save the file
            self._backend.setup(filename)
            success = self._backend.upload(upload, filename, is_raw)
            # callback
            extra_context = self._backend.upload_complete(request, filename)

            # let Ajax Upload know whether we saved it or not
            ret_json = {'success': success, 'filename': filename}
            if extra_context is not None:
                ret_json.update(extra_context)

            return HttpResponse(json.dumps(ret_json))

@@ -291,32 +291,35 @@
 nd%0A        self.
+get
 _backend = backe
@@ -312,16 +312,24 @@
 ackend =
+ lambda:
  backend
@@ -1834,16 +1834,58 @@
 d.name%0A%0A
+            backend = self.get_backend()%0A%0A
         
@@ -1938,22 +1938,16 @@
 name = (
-self._
 backend.
@@ -2058,22 +2058,16 @@
         
-self._
 backend.
@@ -2104,22 +2104,16 @@
 ccess = 
-self._
 backend.
@@ -2196,22 +2196,16 @@
 ntext = 
-self._
 backend.

Make the upload view thread safe.