inicio mail me! sindicaci;ón

share11 with Google AppEngine

While I was boring myself to death last weekend and while everybody was talking about it I came up with a sample application.

Now I can talk about Google AppEngine as well :-)

It is simply a pastebin using the DataStore API, the webapp framework, the Users API, pygments and Django templates.

It is heavily inspired by http://dpaste.com

http://share11.appspot.com

The great thing? It took me less than an afternoon (mostly reading the framework docs) and it is ~200 LOC. Even greater? One shell command to upload and deploy :-)

Django, image uploading, validation and newforms

Update: this post is for version 0.96 only since the trick is now outdated.

As many of you already the trunk version of Django carries a brand new infrastructure to handle HTML forms providing rendering capabilities and validation.

Its name is newforms but I guess it will soon become just forms replacing the current framework which will be removed in Django 1.0. The documentation is in evolution and right now does not cover all the stuff.

One of the top questions about newforms is how to upload a file and validate it in some way. It is imperative for our example to hook into the newforms’ validation system otherwise we won’t gain any advantage. Let’s move on, then.

A couple of days ago I read all the previous threads on django-users about the subject but nothing really came up so I dug into the bare code and I hacked some very straightforward (not sure if it’s a best practice) to handle the case.

In our sample we’re going to upload an image to a Django website validating the content (we want to be sure it’s an image), its width and height (we’re lazy and we want to spare CPU cycles instead of resizing on the fly) and its size.

Read the rest of this entry »

Django community please stand up (for DB2)

Antonio Cangiano is asking for help and ideas. Being himself the former author of the Rails tools for the IBM DB2 DBMS he’s searching for input for the development of a set of tools and drivers for Python, Django an DB2.

I think it’s a great idea having that kind of support in Django, even maybe if I won’t be using DB2 anytime soon. So, if you do care about Django and Python send your ideas to him and we’ll all make one enterprisey step ahead :-)

Python, Django and DB2: we need your input

Profiling Django

Sometimes you need to get down to the core and make your code faster. One technique to speed up things is performance analysis through profiling.

Python 2.5 has revamped its support for profiling with the addition of cProfile which is (the name says all) a C-based version of the profile module. It’s definitely better than hotshot (which has been somewhat fixed in the latest version).

Django has a custom handler which enables mod_python powered apps to be profiled (with hotshot) without monkey patching the codebase.

What about internal webserver anyway?

There’s a wiki page about that. You simply have to patch django/core/management.py with this patch and Django will generate a handful of .prof files in /tmp or whatever each time you invoke the webserver. Then you have to read the stats with hotshot.stats.load or you can use gather_profile_stats.py and collect them all in aggregated profile files.

Sadly I didn’t get cProfile to work with Django because it lacks of a method returning the result of the profiled callable like the hotshot’s runcall but I didn’t spent much time on that so I guess the hack is just around the corner.

That’s it.

HTH

Enter Django Snippets

James Bennet just launched a community driven website to collect useful snippets of Django code all in one place. Very nice!

Thanks James!

djangosnippets.org

Playing with django.test

This morning I poked around the Django testing framework and altough is definitely usable as a simple testing framework (unittest and doctest) it lacks a massive fixture framework (like Rails for instance).

I also have some random problems using test.Client() for POST resulting in some HTTP 500 response instead of 200.

In the meantime I tried using twill to fulfill the holes in the testing infrastructure but it seems to be useless for our web app because the forms use an image to submit and twill understands only submit buttons.

The solution seems to be Selenium. It’s handy and powerful. I can test visually without have to remember line by line in which state am I and go back and forth from Firebug to TextMate to see what name has the view I have to check after. I’m not done testing with Selenium but I like it. It also produces Python source code I can integrate with the app.