tags:

views:

104

answers:

2

Hi all,

I want to build an API service using Django. A basic workflow goes like this:

First, an http request goes to http://mycompany.com/create?id=001&callback=http://callback.com. It will create a folder on the server with name 001.

Second, if the folder does not exist, it will be created. You get response immediately in XML format. It will look like:

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <status>
        <statusCode>0</statusCode>
        <message>Success</message>
    </status>
    <group id="001"/>
</response>

Finally, the server will do its job (i.e. creating the folder). After it is done, the server does a callback to the URL provided.

Currently, I use

return render_to_response('create.xml', {'statusCode': statusCode,
                                                   'statusMessage': statusMessage,
                                                   'groupId': groupId,
                                                   }, mimetype = 'text/xml')

to send the XML response back. I have an XML template which has statusCode, statusMessage, groupId placeholders.

<?xml version="1.0" encoding="UTF-8"?> 
<response>
    <status>
        <statusCode>{{ statusCode }}</statusCode>
        <message>{{ statusMessage }}</message>
    </status>
    {% if not statusCode %}
        <group id="{{ groupId }}"/>
    {% endif %} 
</response>

But in this way I have to put step 3 before step 2, because otherwise step 3 will not be executed if it is after return statement.

Can somebody give me some suggestions how to do this? Thanks.

+3  A: 

I have a feeling that you might be missing some Django fundamentals here.

Why is create.py inside of your url?

If you're using Django's url routing and views, the render_to_response should work fine. You might be jumping to an incorrect conclusion regarding why your response isn't getting returned.

I'm not sure I understand the statement:

But in this way I have to put step 3 before step 2, because otherwise step 3 will not be executed if it is after return statement.

Step 3 is not after the return statement. It is part of the return statement.

You could always do something like this to split up the process:

# Code that creates folder, statusCode, statusMessage, groupId
response = render_to_response('create.xml', {'statusCode': statusCode,
                                                   'statusMessage': statusMessage,
                                                   'groupId': groupId,
                                                   }, mimetype = 'text/xml')
# Some other code, maybe an import pdb; pdb.set_trace() 
# So that you can inspect the response inside of a python shell.
return response
Koobz
Thanks for answering. Yes, should be `http://mycompany.com/create?params...` I also defined the URL in `url.py`. What I mean is that rendering XML and creating folders are two separate steps. I want to return XML first (immediately after HTTP request), and create folders afterwards. A callback should be made after the folder is created.
zihaoyu
Okay. You're probably going have to devise something creative. If you want to create the folders afterwards you're going to need some sort of queue of pending folders. The data for the queue has to persist between requests. You might want to create a model such as FolderQueue where you stash all of your to-be-created folders. Because it's a full blown model you can be pretty robust with it and track a variety of data (who created it, when it was created, what the name is going to be, etc). Might not be the simplest, but it's robust and still very easy to do thanks to the orm.
Koobz
Another alternative would be the stash data in the session but that's asking for trouble. With the queue method at least it's somewhat idiomatic and self-documenting, rather than a fudged together hack ;)
Koobz
+2  A: 

You can use celery for the problem of te queue

diegueus9
Thanks, that should be helpful.
zihaoyu