Move db queries out of global scope.

Change-Id: I464f9a9e6850f8ddeaac4573234d637a20636216
Update WSGI to Django 1.4 API.

Change-Id: I91b6cefd3cea11d371ccfa7da7bdbbbe18fd9988