Code Samples

Modifying mongoose query objects

Mongoose is quite easy to use, but modifying or annotating a query result manually is a little tricky. The problem is that the result objects are mongoose documents that can't be modified at will. In my case I was trying to change a Date type field to a human readable ... more

You do not have sufficient permissions to access this page

Are you getting "You do not have sufficient permissions to access this page." errors everywhere in the WordPress admin? I had this happening on a site, apparently the problem is that somebody changed the database prefix. It's a little suprising that this would totally break the admin, but whatever... As ... more

Additional context for class based views through urlconf

I thought there was a simpler method of adding context through the urlconf but after having a look at the source this is what I came up with. Apparently there's only get_context_data() so I used a lambda to add what I needed. Update: I should probably have used extra_context={'model': 'Frontpage'} more

Django redirect on duplicate object creation

So I had this project with a peculiar requirement: It had to do a redirect to an existing object when the CreateView failed because such an object already existed in the db. Not really hard to do but it took me a while to figure out.. so here's the code. more

Script to migrate WordPress plugins from SVN to github

Update: This script is almost obsolete as github will switch to their v3 API on may 1st, 2012 and doesn't display the API token any more... With this this script you can easily migrate a WordPress plugin from the wordpress.org svn repository to github. This script will NOT import the ... more

No suitable request handler found

Very annoying typo3 "bug" and it took me long to find a solution. See this Bug report for some more info. more

Redirect or block hotlinked files with nginx

Hotlinking is always annoying, but easy to prevent. Here are two ways to do this with nginx. The first example simply returns a 405 when any hotlinking is detected. The second example prevents hotlinking only from specific domains. I used this when a site tried to embed media files through ... more

Debian init script for virtualenv'd gunicorn_django

I recently moved my projects to gunicorn and needed init scripts. Here's what I'm currently using. I have the gunicorns running behind nginx, so you might want to tweak the IP and PORT settings. It might also be nice to use start-stop-daemon. more

Django query on model instance's class

I have an abstract base class that is used by various other models. The problem is that I need the same method that runs a query on all instances of those models, and that the manager isn't accessible via model instances. This is the code I ended up using. more

How to mirror password protected content

You can use wget to mirror pages that use cookies to authenticate visitors. That's usually the case when there is a login form on a site. more