I'm building a django-based media server where each person has a
private account.

The person can upload media files such as mp3s.

The media files will be uploaded to a directory '/media/
uploads/'+<username>+'/' with name of filename.mp3

I started working with this posting:
http://groups.google.com/group/django-users/browse_thread/thread/6876b45961c6ade6/054fb073921ebd31?lnk=raot&pli=1

But it solves a slightly different challenge (posting above web
root).  It seems to require setting the path at 'compile' rather than
run time. (I know python is interpreted.)

I found a code snipped posted a few days ago that appears to address
the issue, but I lack a deep enough understanding to know the one-line
change I need to make to django-transfers based on this snippet.

I'm guessing it is here;
      if request.user: self.get_backend = lambda:
backend(UPLOAD_DIR=request.username)

Please direct me to any resources that can further me along this
journey or suggest how to solve my challenge.

Thank you in advance,

Jonathan
===========================================================================================
import json
from django.http import HttpResponse, HttpResponseBadRequest, Http404
from ajaxuploader.views import AjaxFileUploader
from ajaxuploader.backends.local import LocalUploadBackend

class MyAjaxFileUploader(AjaxFileUploader):
    def _ajax_upload(self, request):
        # check to see if the user info is passed
        print request
        if request.user:
            self.get_backend = lambda:
backend(UPLOAD_DIR=request.username)

        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

            backend = self.get_backend()

            # 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)

            # 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))

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to