Showing posts with label django. Show all posts
Showing posts with label django. Show all posts

Monday, April 28, 2008

secure django+jetty+jython

UPDATE: since this is now 2 years old, and one of my key links is broken i've written a quick update here: http://tristanking.blogspot.com/2010/01/re-jettydjangojython.html

Today I enabled a ssl connector in jetty to run my django app securely. I then spent most of the day trying to figure out why HttpResponseRedirect's didn't work.

Turns out all I had to do was implement is_secure in my django handler.

updated servlet.py can be viewed here

Sunday, March 23, 2008

twitter status messages as an image

I love twitter, and I like to have my status message everywhere possible. Unfortunately most forums and such don't allow having the flash or javascript badges in signatures etc. What was needed was an image generater which could create a dynamic image based from the current twitter status message. After some quick googling I cam across this site which generates an image similar to the flash based twitter badge. I'm not very fond of this badge tho. It's ugly. I much prefer the facebook badge for the official twitter facebook app. So I decided to build an app which generated an image similar to the facebook badge. After a few hours of pluggin away with django and the pil I had done it!

the source can be found on github.

The app currently grabs the status message from twitter every time the image is requested. Thus I don't think this app would scale very well (altho if it was run by twitter themselves, and connected directly to their databases it probably would). But for the forum or two that I have it on, it should be fine.

Wednesday, March 19, 2008

django+jython+jetty

UPDATE: since this is now 2 years old, and one of my key links is broken i've written a quick update here: http://tristanking.blogspot.com/2010/01/re-jettydjangojython.html

Yep, you read that right. Django, running ontop of jython, running inside jetty.

Why? The idea just popped into my head while in a meeting discussing our project's deployment strategy. I thought "hey, we already have a tomcat server running for other people's projects, I wonder if i can get django+jython running inside a servlet container". Seeing that jython already came with a PyServlet class, i figured the base work was already there and decided to give it a go.

So I started off looking at the PyServlet class that ships with Jython. PyServlet allows you to write servlets from python code, implementing the standard doPost, doPost, etc functions, from which you access by going to a URL which matches the path name of you .py file. This doesn't match the REST method django uses, so the PyServlet needed some work. It turned out to almost be an entire rewrite, the only things remaining from PyServlet being the PySystemState initialisation.

As well as the DjangoServlet, a django handler module had to be created to bridge the gap between the java servlet request and response objects and the django python request and response objects. The already existing modpython handler provided a great guideline to make this, and it turned out to be a breeze.

Currently, I build the DjangoServlet on it's own using maven and install it into my local maven repo (for people who have not used maven before, this involves a single command line call). The servlet handler module is put straight into django in the same location as the other handlers (django.core.handlers.servlet). I then have another maven project with a dependency on the DjangoServlet project, and a web.xml. The web.xml requires a few init-params: one for the jython home, one for the django handler class and one for the django app location.

With everything set up, using the maven jetty plugins, i fire jetty up, which loads the DjangoServlet and BAM! django running in a servlet container.

So far I've not noticed any difference in performance, but one nice 'feature' is being able to use maven's dependency management to set up the classpath with the jars required to run your app (it definitely beats having the edit the $CLASSPATH before starting jython and my settings.LIB_DIRECTORY method).

A few things still need to be done to make this really easy to setup and use, It may be best to try move the handler module out of the core django code, so that once jython is up to the task of running django without any modifications it is possible to deploy this on a pure django+jython install. I also need to write scripts to build the pom.xml and web.xml files.

If you want to have a look at this, or deploy it yourself, you can grab the projects from github. Here for the DjangoServlet project, servlet.py for the django handler module and hydrant is my django app which has example pom.xml and web.xmls for you to use as a reference for your own (until i have proper scripts/docs to build them).