2010 Jun 28
I have a quick teaser of what I have been working on, its a complete new design for this website.

I have coded all the html/css, the only thing left is to create a portfolio backend in django. So this new website will probably come out in July somewhen.
2009 Jul 01
I have always found it awkward working with sites and user's in django admin panel. James Bennet from b-list.org explained his way of doing it on his blog, but I found his way a bit limiting, I still want superusers to be able to change the author of a post.
After looking around in the source code for ModelAdmin I found two method's, one of which was not documented. These were formfield_for_manytomany and formfield_for_foreignkey. These methods allow us to supply our own FormField, which could have initial data. These methods also get passed the HttpRequest, which contains the user. So we can fill in author of a blog post and the current site.
In this post I will quickly show you how to use this method to fill in current data based upon the request inside the admin.
My model
Here is a model from which the rest of the article will work from.
from django.db import models
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=255)
content = models.TextField(blank=True)
sites = models.ManyToManyField(Site)
author = models.ForeignKey(User)
As you can see, the author is a ForeignKey field, and sites is a ManyToMany field. The methods we use to set the default reflects this. We use formfield_for_foreignkey to set the default user.
Here is my ModelAdmin class
class PostAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
'fields': ('title', 'content',)
}),
('Advanced options', {
'classes': ('collapse',),
'fields': ('author', 'sites',)
})
)
admin.site.register(Post, PostAdmin)
Selecting the current site
To select the current site, what we will do is create a method called formfield_for_manytomany inside our PostAdmin class. This method will be called each time the admin panel goes to draw a ManyToMany field in a Post. So all we do is set the initial value to the current site. But it is important to remember that the field may be something else, so it is important to check if it is the sites field.
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == 'sites':
kwargs['initial'] = [Site.objects.get_current()]
return db_field.formfield(**kwargs)
return super(PostAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
Selecting the current user
This is very similar to selecting the current site, instead we create a method called formfield_for_foreignkey for ForeignKey's instead of ManyToMany fields.
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'author':
kwargs['initial'] = request.user.id
return db_field.formfield(**kwargs)
return super(PostAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
Now that we have selected the current user, it would be nice to only let superusers use this field, but I couldn't find a way to only show this for certain users. So instead we will just disallow normal user's from saving the post as another user.
I dont think it is possible to optionally hide the author field from a user, so what I have done was to disallow a non superuser from editing the post's author.
Disallowing a user from changing another post
def has_change_permission(self, request, obj=None):
has_class_permission = super(PostAdmin, self).has_change_permission(request, obj)
if not has_class_permission:
return False
if obj is not None and not request.user.is_superuser and request.user.id != obj.author.id:
return False
This snippet was not written by myself, but by James Bennet from b-list.org.
Only let the user view their own posts
Another measure to limit the possibilities of the user can be to let the user only view their own posts. To do this we just change the queryset to filter posts where the author is themselves. We still want superusers to see all posts, so only filter it for normal users.
def queryset(self, request):
if request.user.is_superuser:
return Post.objects.all()
return Post.objects.filter(author=request.user)
To see a complete example of this you can view the source code for lithium.
2009 Feb 24
This was a useful guide I wrote back on my old website. I was trying to install irssi, and I couldn't remember how I installed it last time. Luckily it was available on archive.org's WayBackMachine. I have updated this guide to glib-2.19.8 and irssi-0.8.15.
To install irssi, you can eigher follow the next 7 steps and install from source. Or you could download the binary I compiled here.
Every command on this page should be entered into a SSH Prompt on DreamHost
SSH into your Dreamhost server (for me, this is titan.dreamhost.com, you can find your server inside panel)
Create the necessary directories:
mkdir -p bin lib tmp
chmod 700 bin lib tmp
Adding lines to your ~/.bash_profile
echo "export PATH=$PATH:$HOME/bin" >> ~/.bash_profile
echo "export PKG_CONFIG_PATH=$HOME/lib/pkgconfig" >> ~/.bash_profile
echo "export LD_LIBRARY_PATH=$HOME/lib:/usr/local/lib:$LD_LIBRARY_PATH" >> ~/.bash_profile
Activate the new changes
Download, untar, and install glib
cd ~/tmp
wget ftp://ftp.gtk.org/pub/glib/2.19/glib-2.19.8.tar.gz
tar -xvzf glib-2.19.8.tar.gz
cd glib-2.19.8
./configure --prefix=$HOME
make
make install
Download and install irssi
cd ~/tmp
wget http://irssi.org/files/irssi-0.8.15.tar.gz
tar -xvvzf irssi-0.8.15.tar.gz
cd irssi-0.8.15
./configure --prefix=$HOME
make
make install
Clean up
rm -rf ~/tmp/glib-2.19.8.tar.gz ~/tmp/glib-2.19.8 ~/tmp/irssi-0.8.15.tar.gz ~/tmp/irssi-0.8.15
Once you have installed it, simply type irssi to run it.
I asked Dreamhost if they would allow the use of a terminal application running all the time. Here is their response:
You won’t be in “trouble” for running an irc program (no servers) on our servers. However if the system becomes unstable, it would likely be one of the first processes killed by our admins as our servers are meant for hosting web pages only. In general , we just don’t want users running bots or their own server daemons on our servers.”
2009 Feb 23
Hello and welcome to my new website. Its been a long time since I have had a fully working website, this time I hope to maintain and update this regularly.
The design is heavily inspired by Nathan Borrow, ever since I first saw his website, I loved how simple and white it looked. I have tried to make my website quite different, but it always goes back to a similar look and feel that Nathan Borrow has on his site.
What can you find here?
On my website you can find various projects I am working on such as: PyPPP. I am also working on lithium which is the batteries that wern't included in Django, it is a suite of applications for writing a Django website, it includes a blog, a forum, and many other useful applications.
The blog on my website is mainly as a home to post updates and notifications based on the projects I am working on, I will also post anything interesting I find in Python or Django.