Getting up and running with Django nonrel

This POST might be slightly out of date from time to time. See http://github.com/sidmitra/django_nonrel_testapp.

The Django nonrel project is an attempt to make django work with non relational backends. They’ve also made a Google AppEngine backend for the same. Thus, now you can easily run native django apps on GAE.

Some caveats first, there are some features from Django that are missing(like joins! which is coming) or some that aren’t perfect yet but it’s a good start to port over any existing simple apps that you already have on Django. Something you should keep in mind is that there’s no way to mix the native django models with those that appengine provides. Some of the steps i took to get it running are specified below. In case you are too lazy :–), I have a Github repository for the same at http://github.com/sidmitra/django_nonrel_testapp.**

Downloading stuff

Getting a simple test app running with nonrel is simple. But you would need to install Mercurial first. Once you have that up and running, they project already provides a sample project to support the dev server. So you can start by cloning all required repositories first.

hg clone http://bitbucket.org/wkornewald/django-nonrel
hg clone http://bitbucket.org/wkornewald/django-testapp/
hg clone http://bitbucket.org/wkornewald/djangoappengine/
hg clone http://bitbucket.org/wkornewald/djangotoolbox/
hg clone http://bitbucket.org/wkornewald/django-dbindexer/src

Setting it up

django-nonrel is the main project containing a modified django, and we need djangoappengine, djangotoolbox as further dependencies. django-testapp is the sample app created with custom configuration to make this quicker.

cd django-testapp
cp -R ../django-nonrel/django django
cp -R ../djangoappengine djangoappengine
cp -R ../djangotoolbox/djangotoolbox djangotoolbox
cp -R ../django-dbindexer/dbindexer dbindexer
python manage.py runserver

And that’s it. Essentially we’ve copied all the dependencies into the project folder. So your directory structure should look something like this.

* django-testapp
     * django
     * djangotoolbox
     * djangoappengine
     * dbindexer 
     * ...

You can also use links on linux instead of copying the directories. On windows install Link shell extension. You can drop junctions into the common-apps folder.

Source:

http://www.allbuttonspressed.com/blog/django/2010/01/Native-Django-on-App-Engine http://www.allbuttonspressed.com/projects/djangoappengine#docu

How to syncronize a server's clock and correct drift

Servers with large uptime, might gather quite a few seconds of drift in their clocks. To synchronize the server's time with an external source, simple run the following:

sudo apt-get install ntp ntpdate
sudo ntpdate pool.ntp.org ntp.ubuntu.com

If you get an error while about the port being in use, then you might try killing the previous running ntp process and trying again.
It would also be a good idea to add the second line to your cron tab as a daily run.

Check raw SQL queries that Django ORM generates

To find out what queries Django generates behind all that ORM magic, make sure that settings.DEBUG=True, and then run:

>>> from django.db import connection
>>> connection.queries
[{'sql': u'SELECT `auth_user`.`id`, `auth_user`.`username`, 
`auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, 
`auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, 
`auth_user`.`is_superuser`, `auth_user`.`last_login`, 
`auth_user`.`date_joined` FROM `auth_user` 
WHERE `auth_user`.`id` = 2 ',  'time': '0.002'}, ]

The connection.queries attribute spits out a list of dictionaries of all queries in order of execution. The dictionary contains the actual SQL executed along with the time it took for the same.

To see the last query run, you can use pop:

>>> connection.queries.pop

and to explore further methods, attributes on the connection object, you can always do:

>>> dir(connection.queries)

Static site generation frameworks

My homepage at sidmitra.com, started out as a single page portfolio, but since then has grown into multiple pages. It’s coded in simple static HTML, CSS, JS and with no moving parts.  A full blown content management system seems overkill in my case and also ‘cos I'm running it on App Engine. So i was looking for frameworks that would let me exploit the benefits of a full blown templating engine, but spit out a static site. I came across quite a few, which are listed below. I’ll be exploring the Python ones a little bit more to see if they fit my need. Somehow i feel the itch to make my own, though.

 

Python

Hyde - http://github.com/lakshmivyas/hyde/tree/

Sleepy-django - http://code.google.com/p/sleepy-django/

Stango - http://www.digip.org/stango/

Blatter - http://bitbucket.org/jek/blatter/

 

Ruby

Jekyll - http://github.com/mojombo/jekyll

Templette - http://github.com/jdunphy/templette

StaticMatic - http://staticmatic.rubyforge.org/

Nanoc - http://nanoc.stoneship.org/

Webby - http://github.com/TwP/webby

 

If you know of any more, even for other languages let me know in the comments. I’ll update this list further.

LinkStash - 12 Oct '09

I cleared out my Google Reader unread count after months. I’m not sure about the actual numbers but my guess is there were more than 10,000 items there.

I was listening to cover songs on http://copycats.tumblr.com and these are some of the ones i loved recently.

 

Nature Lover by Philippe Sainte-Laudy

Django complains “Unable to Open Database File”, with SQLite3

This is a fairly common gotcha, that i had forgotten about until i wasted half a day troubleshooting it again.

To solve, make sure the database file itself is writable, and not only that, also make sure that the parent directory is writable as well so the database engine can make temporary files. So just chmod 777 to check if it works :-) but remember to choose your permissions carefully later.

Ref: http://code.djangoproject.com/wiki/NewbieMistakes#DjangosaysUnabletoOpenDatabaseFilewhenusingSQLite3

Merge multiple PDF files into one from the command line

Unless you have Adobe Acrobat, it’s hard to find tools that allow you to play around with PDFs. For viewing i stick to Foxit for now, as it’s the fastest thing around. But when it comes to merging, there sure is a lack of good free tools. I looked at and some other ones, but they were purely crap.

The best tool i found was pdftk, the PDF Toolkit. It’s a simple exe that you copy to your system path and merging files is as easy as

pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf

It also allows you to merge/split files, update meta data etc. You can check out its page for more details.

Test Redmine with sqlite3

I asked around on HN a while back for a project management solution. I’ll do a comparison post soon among all the ones i checked out. I also tested Redmine, which came highly recommended.  Using it with SQLite on Windows might result in an nmake error, while installing the ruby-sqlite3 gem. Make the minor changes mentioned below to make it work.

You can choose to download it from RubyForge, or plain old svn with

svn co http://redmine.rubyforge.org/svn/trunk redmine

You might also want to check out the Bitnami readymade stack at http://bitnami.org/stack/redmine.

 

Redmine is based on Ruby on Rails, so you need to install those two first. The install guide on their site uses MySQL by default, but we want to use the more portable SQLite instead. To do that, you only need to modify/add to the steps listed in http://www.redmine.org/wiki/redmine/RedmineInstall, as follows:

 

  • Install the Ruby sqlite3 gem, after installing Ruby and Rails. Only the specific version mentioned below works on my machine. If you don’t specify the version, it may result in an nmake error on Windows.

     gem install sqlite3-ruby -v=1.2.1 

  • Follow the steps in the install guide till where it asks you to copy the database.yml . Then in database.yml, you could replace

    production:
    adapter: mysql
    database: redmine
    host: localhost
    username: root
    password:
    encoding: utf8

    by

    production:
    adapter: sqlite3
    dbfile: db/redmine.db

  • The rest of the steps stay the same, for eg. the following commands

    rake db:migrate RAILS_ENV="production"

    rake redmine:load_default_data RAILS_ENV="production"

    ruby script/server webrick -e production

The rest of the steps should be similar to the one mentioned on the install documentation, for example setting permissions to directories etc., if you’re on Linux.

Get a standalone version of Windows Live Essentials

Windows live has some pretty nifty tools, like Live writer which is one of the best blogging tools that I've come across. I recently tried to upgrade from my previous beta version of Writer to a new one at http://download.live.com/.

I can’t figure out this fixation for web installers everywhere. First you download an installer which in turn will download more stuff. This is an obvious problem if you want to run the installer on a computer that’s offline (yes, that does happen in this day and age!!).  It is also a major problem if the web installer croaks and complains that there isn’t an internet connection when there evidently is.  You get stuck with this unhelpful error:

OnCatalogResult: 0x80072ee6

There are some suggestions given here, but they didn’t work for me. The only way to get it working was to download a standalone version. When you click the download link on the main page you’re redirected to a page and also get the prompt to download the web installer (something like http://download.microsoft.com/download/E/B/7/EB7AAFE7-514F-495A-A981-99830FE8E99E/en/wlsetup-web.exe). You can choose to cancel this prompt and press “Try Again” on the same page to download the full standalone version. Or even simply rename the above link to wlsetup-all.exe instead of wlsetup-web.exe. The file size is a lot bigger, but it works!