Any idea how to get saving to a specific location working?  I've got
this half working...

I extended TemporaryFileUploadHandler with a CustomUploadHandler that
copies the files to the right location in file_complete.
However, when I call form.save() the files get saved to the MEDIA_ROOT
folder?

Really not sure whats going on :-\


Heres my model, view and handler:

# Model
class MidletPair(models.Model):
    jad_file = models.FileField()
    jar_file = models.FileField()


# View
def validate_build(request):
    '''
    Jad/Jar upload form
    '''
    # Save the file to disk
    request.upload_handlers = [CustomUploadHandler(upload_to =
os.path.join(settings.MEDIA_ROOT, 'superglu', 'userdata', str
(request.user), 'validate'))]

    UPLOAD_FORM_TITLE = 'Validate Build'
    if request.method == 'POST':
        form = UploadMidletPairForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
    else:
        form = UploadMidletPairForm()
    return render_to_response('validate/build/index.html',
context_instance = RequestContext(request,\
        {
        'form': form,
        'title': UPLOAD_FORM_TITLE,
        }))


# Custom handler
class CustomUploadHandler(TemporaryFileUploadHandler):
    """
    Upload handler that streams data into a temporary file, at a
specific
    location.
    """
    def __init__(self, request = None, upload_to = tempfile.gettempdir
()):
        TemporaryFileUploadHandler.__init__(self, request)
        self.upload_to = upload_to

    def file_complete(self, filesize):
        """
        Move file to correct location
        """
        super(CustomUploadHandler, self).file_complete(filesize)
        # Super class sets filesize and seeks to 0, this makes the
file object
        # valid when it is returned

        if not os.path.exists(self.upload_to):
            os.makedirs(self.upload_to)
        elif not os.path.isdir(self.upload_to):
            raise IOError("%s exists and is not a directory." %
self.upload_to)

        shutil.copy2(self.file.temporary_file_path(), os.path.join
(self.upload_to, self.file_name))
        return self.file
--~--~---------~--~----~------------~-------~--~----~
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