richwerk.com

Tumblelog

Sep 2

Unicode Encode Error on Django Admin File Upload

I love tracking down a good UnicodeEncodeError through a web app or any Python code for that matter. I thought I’d share the solution to one of the more low-level exceptions I’d triggered.

Essentially, the test is to upload an image to the Django Admin with a special character in it. My example was this:

bib_fortüna.jpg

If you can upload an image with this filename and it works, you’re golden. If you get a UnicodeEncodeError, you might have the same issue we had.

UnicodeEncodeError at /admin/cal/event/add/
('ascii', u'/srv/code/python/appname/media/uploads/images/bib_fort\xfcna.png', 54, 55, 'ordinal not in range(128)')

In this case, the Python os machinery expects everything on the filesystem to be an ascii string, instead it’s getting a unicode representation of a lower case u with umlaut. I did my usual checks, database character set and collation are utf8 and utf8_unicode_ci respectively, template character sets are utf8 too. There were apparently no problems at the application level.

In our case, it turns out Apache was running under the wrong environment. The important environment variable here is LC_ALL—Apache understood the value to be POSIX but it needs to be en_GB.UTF-8 to allow special characters in filenames. We tried everything; setEnv in Apache config; exporting the variable in bash.

The Solution

The only thing that would work was a direct edit to the Apache init script. On our SUSE Linux (SLES) box, we edited /etc/init.d/apache2 and inserted the following lines before the case switches.

LANG='en_GB.UTF-8';
LC_ALL='en_GB.UTF-8';
LC_LANG='en_GB.UTF-8';
export LANG;
export LC_ALL;
export LC_LANG;

Later in the script I temporarily added echo $LC_ALL; to verify the environment variable had indeed been set. Restarting Apache via the init script resulted in successful uploads of files named using accented or extended character sets.

As ever, we are indebted to Stack Overflow for helping us find this solution.


Aug 17

Aug 6

Aug 4

Feb 8

Git… My new favourite SVN client

My friend and former manager Simon Jones, Technical Director of the talents at Studio 24 challenged me to post my experiences with Git. So, I have cobbled together the commands and concepts that I’ve found most useful in transitioning from Subversion. Here goes, Si!

Intro

Git is fast, unfussy and seems beautfully adapted to my workflow. Subversion is centralised, slow and usually makes me feel a bit clumsy. However, SVN remains a sensible choice for smaller dev teams within a hierarchical organisation due to its ubiquity and bureaucracy. I am delighted to discover that I can use Git as an interface to my SVN repositories. Git is now my favourite SVN client, especially when it comes to branching and merging.

If you’re prepared to discard your existing Subversion client and use Git exclusively (on a project-by-project basis), your SVN-using colleagues won’t see any difference in your commits to the Subversion repository and you too can be smug when enjoying the benefits of Git locally.

Mental Notes

A couple of conceptual differences I needed to overcome.

  1. Your local Git repository has the complete history of your project, meaning you have access to every changeset and version even when offline.
  2. I usually maintain one Subversion repository with several directories; one directory per project. With Git, it’s one repository per project. You will likely map your local Git repo to a directory on your SVN server.

A cheat sheet of sorts

Sensible prerequisites for these commands are a local installation of Subversion and Git, probably in that order.

“Checking out”

With git svn clone, you tell Git to suck down every changeset from Subversion and create a local repo. Note this is not an svn checkout; regular svn won’t know what to do in this local directory.

The following command assumes that your svn project directories follow the usual trunk, branches, tags structure. Feel free to adjust the args to match your house style.

# clone project from your Subversion repository
git svn clone svn://server.name/svnrepo/project/directory -T trunk -b branches -t tags local_dir

This will take a little while and could copy quite a lot of data, so hop into the local directory that has been created and run Git’s excellent compression.

# repack it
git repack -a -d -f

You might have created a blank repo on Github for your project. You can manage both SVN and Github from within your local repo.

# link this repo to github
git remote add origin git@github.com:username/project.git

# push svn trunk to github master
git push origin master

The Github stuff is optional but master refers to your local copy of SVN trunk. Now, back to more local Git/SVN integration.

Branching

# check which branches are available on svn
git branch -a

# create a new branch on the svn server (the -n switch does a dry run, remove it when ready)
git svn branch -n git-test -m "branch for messing"

# create a local git-test branch pointed at remote svn branch
git checkout --track -b local/git-test remotes/git-test

# check it's pointed at the right branch on svn server
git svn info

# Optional: push this branch to github (or have a dry run at it—remove --dry-run to do it for real)
git push --dry-run origin git-test:svn/git-test

# make lots of granular code changes and commit to your local Git repo freely and often
git status
git add changed/files.*
git commit -m "committing small but perfectly formed changeset"

Once you have a sensible bunch of local commits, you can commit all those changesets (with messages included) to Subversion.

# similar effect to "svn commit"
git svn dcommit

Merging

After a few commits to your branch you will want to merge into master locally and commit that to svn trunk.

# fetch changes from SVN if other coders are active in your repo, e.g. other branches
git svn fetch

# switch to master
git checkout master

# make sure master is pegged to trunk
git reset --hard remotes/trunk

# rebase is the "svn update" equivalent. Run it on trunk.
git svn rebase

# merge in changes from the branch we created earlier (squashing all changesets into one fat one)
git merge --squash local/git-test

^^^^Look!^^^^ no svn log, no specifying revision numbers. Git “just knows” what you want to merge.

Clean up after the merge, adding files to master and resolve conflicts if necessary (unlikely).

git add new/file.py

# commit merged result to local master
git commit -m "Merging git-test branch."

# commit merge to svn trunk
git svn dcommit

So, now you can play with the cool kids on github whilst fulfilling your VC obligations at work.

And remember, your local Git repo has EVERYTHING, so get a graphical Git browser like gitx on the Mac or use gitk and enjoy surfing your favourite changesets from yesteryear.


Feb 4

My other Editor is Textmate: my current Eclipse set up for web development.

Eclipse, the legendary Java-based IDE, is a big beast and probably not designed for a quick Apache conf change. TextWrangler, Textmate or Vim are my choice here . But if I’m going to be spending more than 15 minutes on a task, I will eagerly spin up Eclipse.

To keep Eclipse starting up and working efficiently, it pays to be judicious in the extra modules you install.

Here’s how I build my IDE for Django and general web development.

Eclipse for PHP Developers

I start by downloading Eclipse for PHP developers.

I prefer programming in Python but as a web developer you’re always likely to be fixing someone else’s PHP at some point.

Web Tools

This package gives you the Eclipse Web Tools Platform for free, with good, basic content assist and syntax highlighting for HTML, CSS and JavaScript.

PyDev

The first and most important package I add to Eclipse is PyDev, giving me everything I need to work with Python. It knows more about the code I’m writing than I do. PyDev content assist is powerful and code style correction makes sure my syntax conforms to PEP 8.

Version Control

SubClipse for brilliant Subversion integration and EGit for my emerging Git habit. EGit is in incubation and is available from the core Eclipse update site, available from the help menu Install New Software….

Other Eclipse perks

Local History

Even between SVN or Git commits you might lose an hour or two’s work by a careless cut and paste. Eclipse is constantly tracking your code changes and will even let you restore an entire file from local history if you mistakenly obliterate it from the file system. This alone is worth the Eclipse learning curve. Eclipse tracks your history from the moment you start a project, without even giving you the option to make the error of turning it off.

Terminal (command line)

To lessen the need to switch away from Eclipse when “in-the-zone” I’ve got the built in terminal panel working. It is incubating and it’s not perfect out of the box but a quick source /etc/profile and source ~/.bash_profile got the environment the way I like it on my Mac.

Android

The recommended development platform for Android is Eclipse ADT and there are loads of tutorials and videos that assume this combo.

I’m quite excited about playing with Java for mobile within my Eclipse comfort zone.


Jun 10

Web Search Graffiti

The new Ceramics Study Galleries opened to the public at the Victoria and Albert Museum today and I went to have a scout around. They are packed floor-to-ceiling with the V&A’s massive collection, acting like a “live storage” system.

Here’s an artist’s impression because my phone ran out of battery:

V&A Ceramics Study Galleries. V&A Ceramics Study Galleries.

Because the objects are packed so tightly, allowing little scope for labelling of individual items, other information sources have been provided. Digital interactives allow you to search the Ceramics Collection from “terminals” in the galleries and to my delight, the case labels and even the walls provide text to search for using the museum’s Search the Collections web site.

Stencilled search terms on a museum wall Who put that there?

There’s decent public wi-fi in the galleries and I would hope to see people using laptops, tablets and our mobile search on phones to research the vast collection.

Working in a museum web team, it feels warm and fuzzy to see our work in the digital space being written on the walls of the building.


Apr 12

How open is your mobile phone?

I am a freedom-hating iPhone owner. This means I tolerate DRM-encumbered films from the iTunes Store. I also enjoy Audible audiobooks, which I can only play back on a maximum of 5 computers. The development of the iPhone operating system is a closely-guarded secret which is then protected by litigation after release. I don’t like these things much at all. Yet, I am better rewarded than my friends who opted for the righteous Android option 12 months ago.

In October 2008, when the T-Mobile G1 was released in the UK, I seriously considered buying one in preference to an iPhone but it was very much a version 1.0 device and it didn’t have a headphone socket.

I considered the second Android phone on the UK market, the HTC Magic. I wanted the freedom to put any software on a device I paid for. But it didn’t have a headphone socket. In the summer of 2009, I bought an iPhone. On that day, I installed Google Earth for iPhone. This was not available on the Google Android platform at the time. I love Google Earth.

In February 2010, Google finally released Google Earth for Android. It works only on the latest version of Android, 2.1, which effectively limits it to the Motorola Droid and the Nexus One. Speaking to owners of Android phones purchased contemporaneously with my iPhone, I gather they remain on Android 1.5 or 1.6. HTC have indicated it is a matter for the carrier networks (such as T-Mobile or Vodafone) to update the OS. A search on the Vodafone forums indicates the Vodafone staff don’t have a timescale for upgrading to Android 2.1. Some people ask the question whether the hardware can even support the new Android version.

These good people who supported the open phone platform are being neglected now. The carriers still “own” the phone, Android or not. Their business has involved getting new phone models in and out as quickly as possible (since the Nokia 3210), not nurturing existing handsets.

There is currently no obvious or safe way to upgrade a 12 month old Android phone to the latest OS version.

Both Apple and O2 “own” my phone but this summer I will download iPhone OS 4.0 for free and I will benefit from the features immediately. The 3 year old iPhone 2G will probably not run iPhone OS 4.0 but every app developed for the last 3 years will run. 3 years is a long lifespan for a phone, albeit a real expensive one. But it strikes me that support for Android handsets is much more short-lived.


Mar 30

Bernard Bresslaw, at home and at work

I love it when collections of web data make the unlikely connections.

My colleague and I were delighted to see the Harry Hammond photography collection fully digitised on the V&A Search the Collections database a couple of months back. This is a collection worth exploring…

There are some extraordinary photographs from the fifties and sixties here. Update: I just realised I should link to these images rather than display them inline, due to copyright issues! Don’t want the people downstairs coming after me…

Frank Sintra at Radio Luxembourg

Elvis Presley at Glasgow Prestwick Airport

All very nice. But what struck me today was that one of the sites I develop (Film on Freeview) identified there was a glut (mostly from the Carry On stable) of films starring England’s 6’ 7½” comedy giant, Bernard Bresslaw over the 2010 Easter holidays.

Much to our amusement, there are plenty of photographs of Bernard Bresslaw in the Harry Hammond collection. And some are pure magic…

Bernard Bresslaw as a cowboy in 1958. (Gotta feel for the horse)


Mar 29

I’ve written a proper blog post about being a n00b to RPG games and gaming. What do you think? http://bit.ly/dAuBev


Das Projekt

Film on Freeview

Film on Freeview

My Tracks

GPS Tracks Mapped

Das Foto

Flickr