The last few weeks I’ve been experimenting with different Django setups, which virtualenv makes trivial. (If you’re not using virtualenv and virtualenvwrapper for Python development, the secret is to bang the rocks together guys!) My goal has been to create a reusable project framework for quick social experiments that provides the following features:
- External authentication, to exploit as wide an audience as possible without the hurdle of requiring registration.
- Useful debugging information, both through logging and in-page debug.
- Database schema versioning and migration.
- Asynchronous task queuing and control.
- A lightweight WSGI HTTP server to sit between Django and Nginx.
- Straightforward deployment.
- Messaging, so that users can communicate with one another.
- Friends / social groups, so that users know who they want to communicate with.
- A rewards system, to reinforce and drive positive activity.
To that end, I’ve managed to refine my base project to the following:
- Django SocialAuth, providing oauth account creation and login. Extremely quick to implement multi-site auth, and even contact imports.
- Django Debug Toolbar. The swiss pocketknife of debug tools, this outputs an overlay through your HTML templates and offers detailed information from SQL queries, settings, request timings, cookies, signals, and even full profiling.
- Sentry. This fantastic app integrates with Django to log exceptions to the database, and offers real-time reporting and tracking of them. Makes tracking errors on your production environment significantly easier.
- South is the de-facto schema migration tool for Django. Others exist, but the momentum behind South has reached the point where it’s difficult to find one that has undergone as much scrutiny.
- Celery, and RabbitMQ. It’s difficult to separate these two, although possible if you need to run Celery with some kind of ghetto queue behind it. Simply put, Celery allows you to designate Python functions run to asynchronously or under a schedule like a cron task. If you’ve got a heavy bit of processing that doesn’t need to be done during a request, throw it at Celery and it will get done as soon as possible without slowing your site down for the user. RabbitMQ is the message queue that manages running these tasks.
- Green Unicorn. Despite the whimsical name, this handy lightweight HTTP server makes running Django as a WSGI app pure simplicity. Just add ‘gunicorn’ to INSTALLED_APPS, and ./manage.py run_gunicorn. Then proxy to it with your favourite web server for production deployment. Green Unicorn provides both synchronous and asynchronous worker threads depending on your needs.
- Fabric is the webapp deployment tool of the moment. It provides an API for executing Python commands over SSH, so that you can test, deploy and run your django project to any number of hosts with a single command.
- Django Messages is a simple messaging application with an inbox, sent messages, and trash. Very easy to setup and configure.
- Django Friends is a fully-featured contact system for social sites. It covers relationship types, invitations, contact tracking (for when invitees aren’t yet members of the site), and can even do indirect relationships (friend of a friend).
- Brabeion. This final component is a reusable Django application for creating a rewards system with badges. Simply define the badge names and some arbitrary scoring system, and then call a function with the user and score to assign the badge if the user meets the requirements. Works very well in combination with Celery tasks for computing complex scoring.
More on the projects I’ve been using these for in the very near future!