How to re-route the Django admin homepage

It’s very easy to change the default homepage of the Django admin to show any other page you have already defined. For this example I’ll use the now famous “poll” application described in the Django documentation.

Assuming you already have setup the admin interface for the Poll model, your polls/admin.py file should look something like this:

from mysite.polls.models import Poll
from django.contrib import admin
 
admin.site.register(Poll)

and your urls.py file might be something like this:

from django.conf.urls.defaults import *
from django.contrib import admin
 
admin.autodiscover()
 
urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
)

Now, what if we wanted to show the list of Polls after the user logs in, instead of the default homepage showing the list of installed applications? This is how it’s done:

from mysite.polls.models import Poll
from django.conf.urls.defaults import *
from django.contrib import admin
 
admin.autodiscover()
 
urlpatterns = patterns('',
    (r'^admin/$', admin.site.admin_view(admin.site._registry[Poll].changelist_view)),
    (r'^admin/', include(admin.site.urls)),
)

Notice we’re adding an extra pattern to the urls.py file where we define the changelist_view as the view for the homepage. It is important to call admin.site.admin_view method so all the redirection rules work right.

That’s it!

Tags: , ,

No Comments

Iframe resizing across subdomains

A few weeks back I had a problem: an application I’d installed on a subdomain of interalia.net, which is the domain name of the company I work for, was going to be contained inside an iframe in another application, which happened to be on a different subdomain of the interalia.net domain, and the requirement was that the iframe had to be dynamically resized to fit its contents.

Due to browser security restrictions it’s not allowed to access the properties of a document from a different domain, but fortunately, I found a little trick to work around this issue, it consists on setting both the containing and the contained page to the same base domain, in this case ‘interalia.net’. Like this:

<script type="text/javascript">document.domain = 'interalia.net';</script>

The only condition here is that both applications must live inside a common base domain.

Then, just add the necessary JavaScript code to resize the iframe to the containing page:

<script type="text/javascript">
function resize(iframe)
{
    iframe.height = iframe.contentWindow.document.body.scrollHeight + 30;
}
</script>

We also have to add the onload attribute to the iframe tag, so our function gets triggered as soon as the inner page is loaded:

<iframe onload="resize(this);" ... />

Tags: ,

No Comments

Converting SVG to PDF

I recently had the need to dynamically create PDF files based on Adobe Illustrator (*.ai) files created by a talented artist. The application allowed the user to embed his name on the resulting pdf file, and I had to make sure the quality of the drawing wasn’t lost, since it was going to be used for printing. Therefore I turned to SVG, a wonderful XML-based file format for vector images.

XML files are relatively easy to work with on the server, so by converting the AI file to SVG I was able to edit the drawing dynamically. But it turned out converting SVG to PDF on the fly wasn’t as straightforward as I thought. Fortunately a colleague at work found out a very nice way to do it by using an application called Inkscape. It was a bit hard to get the dependencies working on the RHEL server we had, but he finally managed to get it going.

If you’re using Ubuntu, there’s nothing to worry about, Inkscape is as easy to install as running the following on your command line:

sudo apt-get install inkscape

Inkscape has an option for using it as a command-line tool, this is what you must run in order to convert SVG to PDF:

inkscape -z --file=original.svg --export-pdf=converted.pdf

For it to work properly you have to make sure you have the latest Cairo libraries installed.

Here’s an example of the result:

tiger.svg tiger.pdf

This process also embeds the necessary fonts defined in the SVG into the PDF file.

Hope this is useful to someone.

Tags: , , ,

2 Comments

Remuco on Blackberry Pearl

Remuco is an application that lets you control your linux music player using your cellphone using either bluetooth or wifi, this is great for parties!

I’m a Rhythmbox user and I wanted to try this functionality with my Blackberry Pearl 8120, which fortunately has a wifi antenna. I followed the instructions on the Remuco’s website and everything installed like a charm on my Ubuntu Hardy, I then installed the client on the Blackberry using the OTA (over the air) method, but when I tried to connect to my Rhythmbox running on my machine through wifi, got a nice “Connection failed” message. It turned out to be that the Blackberry was using the BIS (BlackBerry Internet Service) connection instead of the wifi I had on.

I digged into the Remuco client’s Java code and found out the connection route wasn’t being specified in the StreamConnection.open() method, so I tweaked it to include “;interface=wifi”, and voila!, it worked!. Now the Blackberry knows it has to use the wifi connection when an IP is specified as a Remuco server host.

I recompiled the Remuco client and put it up, you can install it on your Blackberry pointing your browser here: http://tinyurl.com/remuco

I’ve also changed the application icon so it shows nicer on the Blackberry screen.

Enjoy!

Tags: , , ,

No Comments

Batch convert files to utf-8

I recently had to convert a directory structure of files from Windows created ISO-8859-1 format to standard Unicode UTF-8.

I created a shell script that recursively creates a copy of the directory tree converting to Unicode each PHP file it finds.

find . -name "*.php" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o ../newdir_utf8/{} \;

It was originaly made to be run inside the directory I wanted to convert, but it can be easily changed in order to run from anywhere.

Hope it’s useful to someone having this problem.

Tags: , , , ,

2 Comments