The blagotube home of Sune Kirkeby. Here are rambling rants and some bits of code.

13. Sep
2005

GZipMiddleware

I just implemented a GZipMiddleware for Django, so now you can enjoy the bandwidth-saving goodness that is gzip without using django.middleware.cache (actually, since Djangos cache-middleware is oblivious to the Vary header, it will not work with GZipMiddleware…)

It is in ibofobi.middleware.gzip under http://mel.ibofobi.dk/~sune/r/ibofobi.git. Enjoy.

This post was written by Sune Kirkeby on 2005-09-13, and claimed to be mostly about rambling.
12. Sep
2005

Comments!

Yay!

I finally got around to implementing a simple commenting system here.

Please, pretty please, don’t let the spammers find me…

This post was written by Sune Kirkeby on 2005-09-12, and claimed to be mostly about rambling.
12. Sep
2005

Vary-header goodness

The XHTMLAsHTMLMiddleware class now sets (or updates) the Vary response-header, to indicate to HTTP-caches that the response might change, based on the content of the Accept request-header.

Now, I just need to hack the Django cache-middleware, to set, understand and obey the Vary-header, so I can use it with XHTMLAsHTMLMiddleware.

This post was written by Sune Kirkeby on 2005-09-12, and claimed to be mostly about rambling.
10. Sep
2005

Django developer gadgets

I’ve written two gadgets for helping me when developing Django sites. Since they might be useful to others, I thought I would mention them here.

ibofobi.utils. developer_http

The first is a web-server which re-exec’s itself after every request, so that all resources are reloaded for every request. It has worked very well for me. It lives as ibofobi.utils.developer_http.py in http://mel.ibofobi.dk/~sune/r/ibofobi.git.

You can either execute it directly, with --bind and --port (it defaults to all interfaces port 8080). Or if you just want to WSGI-server in there you can import BaseWSGIServer from the module.

ibofobi.middleware. template_dirs_hacker

The second gadget is a middleware which munges django.conf.settings.TEMPLATE_DIRS for every request. This is how I use it in my settings.devel:

from main import *
import admin

ROOT_URLCONF = 'settings.urls.devel'
MIDDLEWARE_CLASSES = ('ibofobi.middleware.template_dirs_hacker.TemplateDirsHacker',) + MIDDLEWARE_CLASSES
ADMIN_MEDIA_PREFIX = admin.ADMIN_MEDIA_PREFIX
TEMPLATE_DIRS_MAPPING = (
    ('/admin/', admin.TEMPLATE_DIRS),
    ('/', TEMPLATE_DIRS),
)
TEMPLATE_DIRS = []

And in my settings.urls.devel I have:

(r'^admin/', include('django.conf.urls.admin')),
(r'', include('settings.urls.main')),

So, when I request /admin/ I get the admin site with the proper TEMPLATE_DIRS, and when I request anything else I get the main site with the TEMPLATE_DIRS for that.

This post was written by Sune Kirkeby on 2005-09-10, and claimed to be mostly about rambling.
09. Sep
2005

Django vs XHTML

I like to serve my pages as XHTML, and with the correct content-type header application/xhtml+xml when possible, and downgrade to text/html for XHTML-challenged browsers. Out of the box Django cannot do this, but luckily the middleware architecture comes to my rescue :)

So, since this is such a short and sweet piece of code, here it is:

import re

re_ct_xhtml = re.compile(r'^application/xhtml\+xml\b')
re_accept_xhtml = re.compile(r'\bapplication/xhtml\+xml\b')

class XHTMLAsHTMLMiddleware:
    """I change content-type application/xhtml+xml into text/html, if the
    browser does not support the XHTML content-type."""

    def process_response(self, request, response):
        if re_ct_xhtml.match(response['Content-Type']):
            accept = request.META.get('HTTP_ACCEPT', '')
            if not re_accept_xhtml.search(accept):
                ct = response['Content-Type']
                ct = ct.replace('application/xhtml+xml', 'text/html')
                response['Content-Type'] = ct

        return response

It is also available as ibofobi.middleware.xhtml in http://mel.ibofobi.dk/~sune/r/ibofobi.git.

This post was written by Sune Kirkeby on 2005-09-09, and claimed to be mostly about rambling.
09. Sep
2005

Three small Django apps

I’ve just converted this site to Django. In the process I wrote a few small apps, two of which are in a state, where others might use them (or improve them, until they become useful :).

The first app is a blog, with posts and categories (which drives this blog…). It’s still not feature-complete (I want comments and drafts at the very least), but it works. One nice feature, is that the posts are formatted using Markdown.

The second app is a personal calendar-type-thingie, it’s stille very rough around the edges, but it helps me to keep track things I have to remember…

They live in http://mel.ibofobi.dk/~sune/r/ibofobi.git under src/apps/, I still haven’t gotten around to writing a setup.py, and the templates need a bit of cleaning up.

This post was written by Sune Kirkeby on 2005-09-09, and claimed to be mostly about rambling.
25. Apr
2005

rss2email web administration

I use rss2email to read blogs of various people. Combined with gmail rss2email is a brilliant way to read blogs. But, for a long time I have missed an easier way than ssh ibofobi.dk r2e add http://some.blog when adding new feeds. So, I decided to write a quick little rss2email web admin-interface. You can get it at http://mel.ibofobi.dk/~sune/r/r2eadmin.git.

As a bonus, here is a bookmarklet to subscribe; you just have to edit the URL.