Posts Tagged ‘euro python 2007’

Howto get custom GET variables into djangos admin list pages

Tuesday, May 12th, 2009

Yesterday I ranted a bit about how to insert extra GET parameters in the django change list page without having the admin blow up when it was not able to recognise them as filters. I’m using this for building a custom menu that uses GET parameters to fold / unfold a submenu. The menu is context aware so I can’t do this by looking up the path, ie. several submenu points can like to /admin/auth/user/, usually just with different filter parameters.

I haven’t been able to find a nice solution for this, but here is a dirty one. As always I find myself using middlewares when something takes a nasty turn. The idea is to put the information in GET parameters, but use a middleware to actually find the special parameters and insert them on the request object. A context processor can then later give template the variables. I’m controlling which parameters we should fetch with a simple settings tuple:

#!/usr/bin/python/
ADMIN_GET_VARS = (
    ('submenu', None),
)

In my case I have only defined the submenu parameter, but you could take as many as you’ll like. The second element in the tuple (The None after “submenu”) is a default value that will be used if the GET parameter is not present. The middleware looks like this:

#!/usr/bin/python/

from django.conf import settings

class ForwardGetMiddleware:
    def process_request(self, request):
        new_get = request.GET.copy()
        custom_vars = {}
        if hasattr(settings, "ADMIN_GET_VARS"):
            for name, default in settings.ADMIN_GET_VARS:
                custom_vars[name] = new_get.get(name, default)
                try:
                    del new_get[name]
                except KeyError:
                    pass
        request.ADMIN_GET_VARS = custom_vars
        request.GET = new_get

Here is something important. We need to remove the get parameter from the request.GET as it will otherwise make the admin blow up. It’s therefore important that you don’t use parameter names that will clash with the admin. I’m considering introducing a prefix on every parameter to leave the admin alone. The context processor is very simple due to the way we build the middleware:

#!/usr/bin/python/

def admin_get_vars(request):
    return getattr(request, "ADMIN_GET_VARS", {} )

So there it is in a very basic stage. It’s possible to introduce more advanced settings to enable finer control with when this functionality kicks in, but it’s already a hack, so I don’t think there is any need for it. Also, one might consider trying to limit this to specific urls to improve performance and limit the number of side effects this might have.

Hangover and a giant pizza

Wednesday, July 11th, 2007

Mads and Christian woke me at noon (something about a conference we should go to). So not only was my head hurting a bit from last night, but I was hungry as ****. So before we went to the Reval hotel for the mysql keynote we figured that food was necessary. We found a pizzajoint where christian ordered a 8167.14cm2 pizza as seen on the picture below:

Mads and I settled for a much smaller pizza – only 3017.54cd2. That was a good choice as the pizza got itself into a fight with my immune system (or something), so I got sick at the conference.

The keynote wasn’t that interesting, but then again I don’t have a thing with MySQL so I wasn’t expecting it to be. I did however have a little fun:

And we found the american guy from yesterdays party:

So the conference is over – for our part anyway – so we look forward to going back home tomorrow and start at work again.

Conference dinner and postdinner beers.

Wednesday, July 11th, 2007

After all the talks yesterday we went to the conference-dinner. The conference dinner wasn’t all that; the food wasn’t super good and they didn’t have any beer. We talked with some people from Sweden and Norway as the table but I managed to forget what we talked about :)

The lack of beer at the dinner made a rather large group move to some bar in town. Some drunk american took care of the first bill with a comment like this is ridiculous (20 large draughts cost about 150LT). The bar closed about an hour later and as we left I took some pictures at the main bar door;

Why can’t the witch come in?

… So we went to another bar and got really drunk. The american guy thought that it would be a super idea to have a vodka drinking contest with a russian guy. The contest went very well so he decided that he would throw-up in the bar kitchen. Christian and I gave some money to the waitress that cleaned it up and she – and the other really angry bar people – settled down.

Back the the hotel we stormed the drunk fuckers hotel room:




They seemed a bit surprised… So today we didn’t exactly get up at 8a.m…

Second day started a bit slow :|

Tuesday, July 10th, 2007

We didn’t make it to the conference on time, probably because Mads didn’t wake me 7.30 :) It might have helped if I went a bit earlier to bed, but… The first talk we attended was on Silverlight and Python; it’s not the flashkiller we were promised. We continued with a talk about The Essentials of Stackless Python which was very interesting. It looks a lot like CSP so it would be cool to implement something with stackless.

So we spent the second most interesting period having our break… Having a break with coffee – and a laptop – is so much better than hearing some guy talking about M$ technologies.

After a lunch with beer and coffee we’re back in the main conference room waiting for the next talk. Something about safety being for weenies. It’s kinda like the taxies not having a safety-belt – one that works anyway.

I don’t think that Mads has heard any of the talks today, but he is almost done with implementing a blog for swag.dk.

If people have a good idea on what I should implement in SML in the next couple of talks, drop a comment :)

First conference day completed & google cheap points :)

Monday, July 9th, 2007

The conference website suggested that we would have to meet 2 hours before the conference start to avoid a queue. The conference started at 9a.m. so none of us felt like getting up at 6a.m. after our night out with our finnish hotel friends. So we didn’t and found the hotel where the conference is held about 8.30a.m. There was no queue and even though we got 2 hours extra in bed both Christian and I almost fell asleep at the first talk.

Before noon we attended several talks:

  • Seamless object persistence with ZODB
  • Pythonic Interfaces
  • Case study of a Pylons project
  • KSS, Ajax development with style
  • Using FormEncode for web data validation

All talks had a common feature; the people given the talks didn’t seem to care much for what they talked about. Christian and I agreed that the guy talking about Pythonic Interfaces had misunderstood Python – and should properly code java. Why would you want to have interfaces in a dynamic language like python. Just try whatever you want and catch an exception if it fails.

selected pictures from day 1 at the conference

After a – very small – lunch and too much coffee we went to the main conference hall to hear a talk about pypy. It was primarily about partial evaluation, which I had a course about as diku (with Christian). It was very interesting but didn’t cover any theoretical information we didn’t have anyway. I think the next talk – about PyPy Python Interpreter(s) Features – was very interesting but we didn’t have any coffee so I used most of the time doing stuff on my mac to keep awake. And good news: I have a nice new Standard ML – New Jersey installed. Looking forward to code some SML tomorrow if there is any boring talks.

After paying with SML for a while we continued with 3 or more lectures about testing. Why one testframework is good and another is bad. And why didn’t we leave the room after the first lecture :(

Finally the keynote by Simon Willison was there. He used this talk to introduce OpenID. We didn’t think much of it, but people seems to like it (maybe we’re just getting old).

A google representative scored some cheap points when – at the keynote end – he announced that google would be paying all the drinks at a local bare later that afternoon. We were tired and didn’t join the rest at the bar but went home to work a little and watch some TV.

More on the conference tomorrow.