Learning Django by Example(5): Software is hard

Web December 26th, 2007

“Software is hard.” reads the quote from Donald Knuth that opens Scott Rosenberg’s Dreaming in Code. Even the development of Gelman is not smooth as I expected.

Think big, but not fancy

Gelman is a hobby project to scratch personal itch, at the same time it acts as the playground to learn django and dojo. Therefore, it is extremely difficult to resist the temptation for some bling-bling but not essential features, which are called “free feature” in the industry. For example, the fancy django-registration plug-in. It will be cool to integrate a ring-and-whistle registration module, but do I really need this module since I am probably the only user/administrator of this web application, why bother to waste effort in registration?

Deodorize the smell

It is quite annoying to work on a bad structured code base with misleading name convention. Eventually, I decided to re-organize the layout of the source code, and moved out from Google Code to Assembla, since the latter provides the Trac support for online source browser and ticket support.

Some other changes include:

  • Rename library as bookshelf
  • Move the core functionality for book/add to file/add
  • Redesign and develop (in progress) the MassAdd functionality for files in server’s incoming

Find a formal way

What make Gelman stands out is his admin interface, 60% of its functionalities are only available to administrator. When working with admin module, we should play the game of default admin. Eventually, I may have to copy the code from admin module for the sake of security and behavior consistency. In this situation, the Django Book cover what is on the table, but not under the table.

Here is just a snapshot of current progress, the MassAdd.
MassAdd

Learning Django by Example(8): To DnD or Not to DnD

Web October 23rd, 2007

This is really a dilemma, it is quite painful to drop such a cool feature, but after carefully consideration, I decided to take the move for the sake of simplicity:

Using steps


Using navigation



Now the users can either upload the file locally or select the file in the incoming director remotely(N/A); use Prev | Next to navigate, click the image to select the meta data, add tags by clicking(N/A). I consider that click need less effort for user to operate than DnD. And the Step statements directs the users but not enforce the order like wizard does.

It may be worthy to wrap the file browser as a dojo dijit, and that is also a good candidate for comet. The server may register handlers for file system events(check System.IO.FileSystemWatcher, inotify in Linux) , then push the status change to the client. That is quite a little work, so I would rather leave the placeholder here and implement some other features first.

This is r41, the save functionality is still broken, I would squeeze time to fix it ASAP.

Learning Django by Example(7): Act like a desktop application: Dnd

Web October 6th, 2007

After some tests, I found the add-by-search has a big design flaw, I took the wrong direction: the focus of eBook management ultimately is the file, not the meta data. Current implementation is too meta-data centric, it adds the complexity since all dynamic features are created dynamically, and it looks like a little desperate to put so many controls in one page.

So in r37, a brand new UI is developed inspired by WordPress’s dashboard.

Dnd Overview



The user may drag the thumbnail from the search result and drop it in the left dropbox, attach a local file, then click Save, done. Here is another screen shot of drag-n-drop in action:

Dnd in action


Unlike the canonical Dnd example, our dropbox only accept no more than one item at any time, the newly added item would replace the old one. So we need to subscribe the /dnd/drop event:

dojo.subscribe("/dnd/drop", function(source, nodes, iscopy) {
        var t = dojo.dnd.manager().target;
        if(t == dropbox) {
            t.selectAll();
            t.deleteSelectedNodes();
            t.clearItems();
        }
        var jsnode = source.getItem(nodes[0].id);
        t.insertNodes(false, [jsnode.data]);

    });

The first action is to remove all the items, then we extract the data, and reconstruct the nodes by calling insertNodes. The rest part is quite the same.

An advanced version is in the blueprint: how about a file manager on FTP’s incoming directory? But I am lazy to copy-n-paste the code, I would rather grill myself to figure it out how to put the two options in one page more intuitively. If you have any suggestion or mock up, please just drop you comment here. Thanks.

Learning Django by Example(6): AJAX File Upload

Python, Web October 4th, 2007

In the last post, I just added the entities in the database, but did not bind the meta data with eBook files. This post will demonstrate how to upload the file in AJAX flavor:

The idea is inspired by this tutorial , using dojo.io.iframe, we could submit a form with file upload asynchronously. In client side, a hidden field meta is added to store the marshaled JSON string. dojo.io.iframe.send shares the same defer concept as dojo.xhrPost:

var meta = dojo.query("input[@name=meta]", form)[0];
    meta.value = dojo.toJson(cache[i]);

    // submit the form
    dojo.io.iframe.send({
        url: form.action,
        method: "post",
        handleAs: "json",
        form: form,
    }).addCallback(function(ret) {
    … …

NOTE: the handleAs is json, so the argument ret is JavaScript object.

In server side, the JSON string needs to be wrapped by textarea, this is the hard requirement of dojo.iframe.send.

return HttpResponse("<textarea>%s</textarea>" % simplejson.dumps(retset) , mimetype=‘text/html’);

Conceptually, add-by-search will issue INSERT operation to the database, the result may fall in the following categories:

  • Success
  • FileAlreadyExist: the eBook file exists, so the uploaded file is discarded
  • UnexpectedError: an 500 error happens in server side for unknown reason.

Check r34 for the implementation.

Proud to be a Dojo committer

Web October 3rd, 2007

After the development of dojox.gfx and dojox.gfx3d, and continuous efforts on dojox.charting, I was granted the commit privilege eventually. This is quite a honor to acknowledge the contribution and it is an approval from the community of my Web development skills.

Hmm, still a long way to go anyway.