I bet you have heard the news on the street about this old ericsson programming language coming back to life bringing functional programming on the web. Yes, I talk about erlang! The language that is used by companies like Facebook, Amazon and of course Ericsson for their network products. It offers features like hot code swapping, lightweight inter-process communication and more. Generally it is a really great language that fits really well into the "Cloud computing" industry.
As I was exploring the language a while ago, I came into a project called Apache CouchDB, an object-oriented database, or to be more precise a "document store". At first I thought, oh just another object store, but then, after a bit of research, I got in love with it. It is wonderful because It gives you the ability to store, retrieve and query structured data without having to define a schema, one can also attach files onto each document! And the best of all you can do all of this through a neat web interface! Of course it is programmed in Erlang and this is the reason why it offers great speed, stability and distributed features like replication. As I mentioned earlier you can even query data, using uhm... yes... JavaScript! Smart.
After I used it for a couple of hours, I thought that it would really be a great Django template store! One could serve all templates in CouchDB and also define template instances using documents that include all the variables a template renders. Isn't this some kind of a CMS? I started a simple implementation and after no more than 30 lines of python there it was! Really simple but also really functional!
Here are the steps!
- Install CouchDB
- Create a new Django project
- Create a new app inside tha django project, I called mine totemplate.
- Create the appropriate views and setup the urls.py
- Design the database on CouchDB
- enjoy!
Step 1
Installing CouchDB should be really easy for all platforms.
Mac OS X
You will have to download and install MacPorts (http://www.macports.org/install.php) and then issue the following command:
$ sudo port install couchdb
Linux
Here is a link to a tutorial for linux: http://barkingiguana.com/2008/06/28/installing-couchdb-080-on-ubuntu-804.
Windows
If you are on windows you can take a look here: http://wiki.apache.org/couchdb/Installing_on_Windows I haven't tested it myself but it should work fine.
After you have installed couchdb, you will also have to install the python library for it. This is called couchdb-python and is available here:
http://code.google.com/p/couchdb-python/.
For you that have easy_install installed on your machines, issuing:
$ sudo easy_install couchdb-pytho
should do the job quickly and easilly.
Here is the views.py:
# Create your views here.
from django.http import HttpResponse
from django.template import Template, Context
from couchdb import *
from totemplate.settings import COUCH_SERVER
def show(request, resource_id, page_id):
couch_store = Server(COUCH_SERVER)
category_name = couch_store['indexers'][resource_id]['category']
category = couch_store[category_name][page_id]
template = couch_store['templates'][category['template_id']]
html = Template(template['body']).render(Context(category))
return HttpResponse(html)
def index(request, resource_id):
couch_store = Server(COUCH_SERVER)
indexer = couch_store['indexers'][resource_id]
t = Template(indexer['template'])
html = t.render(Context(couch_store))
return HttpResponse(html)
def resources(request):
couch_store = Server(COUCH_SERVER)
resources = {"indexers":[]}
for indexer in couch_store['indexers']:
if couch_store['indexers'][indexer].has_key('template'):
resources['indexers'] += [indexer]
t = Template( couch_store["settings"]["index"]["body"])
html = t.render(Context(resources))
return HttpResponse(html)