views:

54

answers:

1

We've got some clients sending a custom POST of a data blob to our django servers.

They do things in a rather funky way that I'd rather not get into - and we've since moved on from making that particular format the norm. To make further implementations of our upload protocol more streamlined, I was looking to roll a custom UploadHandler in django to make our data handling in the views a bit more streamlined.

So, moving forward, we want all code in the views to access our POSTs via the:

data = request.FILES['something']

So, for our new submissions, we're handling that dandily.

What I'd like to be able to do is get the upload handler we've made, affectionately called LegacyUploadHandler(), to populate the request.FILES dictionary with the right parts, so the code in our view can access the parts the same way.

So, my question:

How does a custom uploadhandler actually populate the request.FILES dictionary? The django documentation doesn't really give a descriptive example of doing that.

Our particular desire is that we have a singular blob of data coming in. We custom parse it and want it to appear as the request.FILES dictionary.

The current code as it stands right now does this:

def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
    files_dict = {}
    files_dict = magic_parser(input_data.read())
    #now what do I do?

I see examples of setting a files MultiValueDict in the http.MultiPartParser, but that seems to be outside the scope/control of where I am in my handlers.

Any ideas of how to actually do the return value? Or am I trying to populate the request.FILES object the completely wrong way?

A: 

From handle_raw_input you have to return a tuple of what will be POST and FILES on the requst. So in your case it's something like:

def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
    files_dict = magic_parser(input_data.read())
    return QueryDict(), files_dict

The magic_parser should return a MultiValueDict of the form {'filename': fileobj}. A fileobj is an instance of some suitable django.core.files.File subclass (or may be that class itself).

isagalaev