Refactoring Django apps
February 8, 2010, 1:09 am
Filed under: django | Tags: ,

I’ve written a Django app to help people who organise social 5 a side games, (yeah, I know I said it was dead, but I left it up and it started to get a few users, it’s still not a success, but it is being used). It was the first “large” scale Django app I’d attempted and so it’s got a lot of things wrong with it, for starters I didn’t really see where I could split the project up into seperate Django apps, but I understand that a lot better now, so as I add new features I’m also splitting the project out into seperate apps. This blog post covers how I do it, and hopefully you’ll find it usefull.

I’m making a start from this post on Stackoverflow.

I’m already using south for migrations, so it makes sense to use that for refactoring too. If you havn’t already, install it and convert your app over.

Everything I created originally was under an app called, ‘sheets’, and now I want to split the parts of this that control creating and playing matches into a seperate app, which I’m going to call it ‘match’, don’t forget to add it to your INSTALLED_APPS

./ startapp match

Now move the models over to your new file, you’ll have to add a related_name parameter to some of your models if you’re using Postgres because south will try and create a relation with the same name as the existing models relation.

stuart-grimshaws-macbook:teamsheet stuartgrimshaw$ ./ startmigration match --initial
Error: One or more models did not validate:
sheets.match: Accessor for field 'location' clashes with related field 'Location.match_set'. Add a related_name argument to the definition for 'location'.
sheets.matchplayers: Accessor for field 'player' clashes with related field 'User.matchplayers_set'. Add a related_name argument to the definition for 'player'.
sheets.sides: Accessor for field 'player' clashes with related field 'User.sides_set'. Add a related_name argument to the definition for 'player'.
sheets.sides: Accessor for field 'side' clashes with related field 'SideNames.sides_set'. Add a related_name argument to the definition for 'side'.
match.match: Accessor for field 'location' clashes with related field 'Location.match_set'. Add a related_name argument to the definition for 'location'.
match.matchplayers: Accessor for field 'player' clashes with related field 'User.matchplayers_set'. Add a related_name argument to the definition for 'player'.
match.sides: Accessor for field 'player' clashes with related field 'User.sides_set'. Add a related_name argument to the definition for 'player'.
match.sides: Accessor for field 'side' clashes with related field 'SideNames.sides_set'. Add a related_name argument to the definition for 'side'.

Don’t be tempted to dive right into your new migration and add your code to migrate data from the old models to the new ones, South recommends that you do a data migration in 3 steps, which makes sense when you consider that doing it in 1 big step might have it’s shortcomings.

The startmigration –initial command created the first migration, this simply created the tables in your database. Run migrate to make these alterations:

./ migrate

Next, you need to create the migration that actually moves the data from one set of files to another:

./ startmigration match sheets-to-match-data

The migration in the example on Stackoverflow is actually a lot more complicated than ours, we simply need to rename the database tables, like this:

def forwards(self, orm):
  db.rename_table('sheets_match', 'match_match')
  db.rename_table('sheets_matchplayers', 'match_matchplayers')
  db.rename_table('sheets_sides', 'match_sides')

def backwards(self, orm):
  db.rename_table('match_match', 'sheets_match')
  db.rename_table('match_matchplayers', 'sides_matchplayers')
  db.rename_table('match_sides', 'sides_sides')

So, that’s the data migrated over, now we need to migrate any views to the new app, just identify and copy them, one of the mistakes I made in the original attempt actually helps me out here, I created a seperate for some sections, that was my first clue I was doing something wrong.

After copying this file over to the new apps, it’s time to take the views it mentions and copy them to the new apps views file, once you’re happy that they’re working, you can delete them from the old views file too.

How do you know they’re working? Well, you have been using Django’s unit tests havn’t you? No? Well that’s another post then.


Git branch in your bash prompt
January 18, 2010, 1:31 pm
Filed under: Uncategorized

While trying to work out how the excellent tab completion works for git, I found /etc/bash_completion.d/git which mentions the __git_ps1 command, you can use this to put the current branch in your bash prompt

here’s mine:

PS1='${debian_chroot:+($debian_chroot)}\[\ 033\[01;34m\]
\u@\h\[\ 033\[00m\]:\[\ 033\[01;33m\]\w\[\ 033\[1;32m\]
$(__git_ps1 " (%s)")\[\ 033\[00m\]$ '

(Remove the space from \ 033, WordPress wouldn’t escape it properly)

The magic bit being

$(__git_ps1 " (%s)")

There are a couple of options you can set too to get it to show you the status of the repo too, see the file above for more info.

My Top Albums of the Decade
December 31, 2009, 11:07 am
Filed under: music

Here’s a list of my Top 10 6 albums of the decade, in no particular order:

Reverend and the Makers – The State of Things, superb album from John “The Reverend” McClure and his band, The Makers. I went to see them in Sheffield for the second tim this year and it was easily the best gig I’ve been to.

Serj Tankian – Elect the Dead, the first album from ex System of a Down frontman Serj Takian. Pretty much another System of a Down album, which is no bad thing. Looking at my stats he’s the single most played artist with over 2000 plays.

Queens of the Stone age – Songs for the Deaf joined on drums by Foo Fighters front man Dave Grohl this is easily the best of the QotS albums, with songs like No-one Knows and Hanging Tree are still on my playlist today.

Foo Fighters – Echoes, Silence, Patience & Grace I love this album from the Foo Fighters, when you look at the amount of material Dave Grohl puts out it’s amazing that so much of it is so good, and this is the best Foo Fighters album of the decade.

Reverend and the Makers – A French Kiss In The Chaos The only band to have 2 albums in my list, this 2nd album from the Sheffield band is just as good as the first and was on constant repeat for about a month after I bought it!

Batman Begins Soundtrack – maybe a bit of an odd one to include in a list of top albums, but this is one of the best soundtracks to a film ever.

What are your top album’s from the last 10 years?

Installing PsycoPG2 on OSX
June 7, 2009, 1:15 pm
Filed under: postgres

Today, I’ve been trying to get PsycoPG2 installed on my Macbook so that I can run through some tests and try and improve the user experience on Who’s Playing and hopefully increase it’s usage. I re-installed OSX the other week, and there’s still some little things like this to catch.

First off, I tried installing it via Macports, which worked Ok, but didn’t install it in a directory that the default Apple install of Python could see, it put it in the Macports directory. After a while I couldn’t find any way to change the install dir, so I ran:

python_switch python25

which lost all my other libraries, and the Django framework. Not quite what I wanted really.

python_switch python-apple

Will switch you back to the Apple install.

The easiest thing to do is just to download the latest psycopg2 tarball, and edit the setup.cfg file. I also installed Postgres from the official Mac binary, not from Macports, so I told it to use pg_config from that install. Uncomment the pg_config line and point it to your install of Postgres.


Then run

sudo python install

Job Done.

Update: For Snow Leopard users, you might also see the following error:

Symbol not found: _PQbackendPID

The solution, that I found on Kreegers blog, is to tell Snow Leopard that python needs to run in 32bit mode:

defaults write Prefer-32-Bit -bool yes

The Premier League is Heading for Disaster
June 4, 2009, 8:05 pm
Filed under: football, Premier League, soccer | Tags: , ,

The Premier League is heading for disaster, todays Guardian carries a story about the debts run up by the leagues clubs, it will come as no surprise that the top 4 in the league are also the top 4 in debt.

Chelsea and Manchester United are both £700 million in debt, Arsenal are £416m in debt and Liverpool are £280m in debt. Fulham are a surprising 5th at almost £200m, all these teams are either bankrolled by a millionaire owner or they’ve mortgaged the stadium and other assets and they’re owned by the banks (does that mean we actually own them?)

With this amount of debt, the only way the owners can ever hope to make their money back is to sell them to someone richer, but that can’t go on forever. Hoe many people in the world could afford to buy any of those top 4 clubs, and if they’ve had the business nouce to make that much money, would they think buying a football club in that position was good business?

For the big 4 clubs, the pressure to win is enorous, winning keeps their cash flow positive, the extra cash that the Champions League brings is a must, you only have to look at Leeds and the position they found themselves in not too long ago. What would happen if Arsenal, Chelsea, manchester United or Liverpool failed to finish in the top 4? Well I don’t think it would mean a collapse on the Leeds scale, but 2 seasons in a row? 3?

Whenever teams are under such vast pressure to win, there comes the temptation to cheat.

If you think I’m being over dramatic, you can look to Italy in 2006 when Lazio, Juventus, Milan and Fiorentina were all found guilty of match fixing and either relegated or banned from European competition. It’s not too hard a leap to make to see that kind of thing happening in the Premier League, or even the Championship with the riches up for grabs. It wouldn’t surprise me if we see such allegations in this country in the next 4 or 5 years, and when they do, the Premiership is doomed.

Government needs to stop wasting money on big IT projects.
March 10, 2009, 1:37 pm
Filed under: politics

Give it in little projects to the little guys.

As I’ve said in the past, government should concentrate on providing data and let it’s citizens build the sites they want out of that.

Some would say that the sites built wont cover the needs of everybody, but then how is that different from today?

Over the weekend a bunch of people got together for Hack The Government Day and proved what can be done when data is free, using screen scraping, they built projects against the data held at companies house, the job center, an API for searching schools, a site for finding a local dentist, a site to review your MPs voting record and a site that took Sport England’s Active Places and improved it using Google Maps. Active Places was developed using £5 million of lottery money.

The sooner this kind of data becomes free, the sooner we start to get these kind of sites available.

How to tell when your site is dead?
January 29, 2009, 1:00 am
Filed under: Uncategorized

I’m coming to the conclusion that (at least) one of my side projects is dead, is a site I wrote to help people manage 5 a side football teams, it lets you pick teams and rate your team mates to help keep the teams fair.

The idea was to have the site free while people beta tested it, then charge people to use it once I was happy that it worked Ok, but no-one wants to use it when it’s free, so I doubt people would want to pay for it.

It’s been live for about 6 months and has gained less than 100 users users, none of them active. I’ve received 2 emails regarding problems on the site, both of which were fixed in quick order, after that, neither user carried on using the site.

I tried engaging the team organisers whenever I fixed major bugs, or improved the interface in such a way that might make it usable for them, but I rarely got even a log in from them.

I’ve tried gaining users by joining various 5-a-side groups on Facebook, but it’s hard not to look spammy when you’re the most active user on there. Some people signed up, but never got any further.

One of the users works for one of the major 5-a-side football pitch providers in the UK, but his experience of the site didn’t get any further than trying to invite a few players to join his team. I’ve even emailed the FA, but heard nothing back.

So now it’s beginning to look like the site is a dud, no active users even when the site is free, even when some of the users who signed up are your friends, it doesn’t appeal to people who hang out on groups aimed at the subject it covers.

So it looks like is in injury time, all the substitutes have been played, the referee has checked his watch and unless something happens soon I might have to blow the final whistle.