Shall I Go To The Pub?
April 21, 2011
Last Friday someone asked "shall we go to the pub?" I thought it was a great idea, but as I feel I've lost my own identity and become a conduit for the Internet I thought I should check with it first.
Whacking shalligotothepub.com into my browser I was shocked to find no answer. I couldn't possibly go to the pub without the Internet sanctioning it, so Pawel and I spent 20 minutes creating shalligotothepub.com.

It was done for our own amusement, but after Patrick submitted it to StumbleUpon the hits started rolling in, and in, and in. Thousands and thousands of visits within a couple of hours was NOT what we were expecting! (So we quickly stuck a Facebook Like button on there.) And they continued. Over 11k on the first day, and half that on the next. Monday and Tuesday saw an unexpected resurgence in people wanting to go to the pub with the Internet's permission. Tuesday being the busiest say yet with 11.5k visits.
We've had around 40k unique visitors, over 2k Facebook Likes and 56k page views. Over 75% of traffic came from StumbleUpon and 8% from Facebook. Overwhelmingly out visitors choose Firefox and Chrome to browser the web - perhaps the high Firefox percentage is linked to the handy StumbleUpon plugin.
This has certainly been an interesting lesson.
Categories: Fun
What's the world listening to?
July 8, 2010
A while back I crafted a Processing sketch that displayed a map of the world and popped up the most popular artist for the country the mouse was hovering over. Bored last night, I decided to give the newish Google Maps API a whirl and see if I could recreate the sketch for the web.
It uses fxb's javascript-last.fm-api library which made the artist lookup incredibly easy. The whole thing took no more than 2 hours.
Categories: Fun, Programming, Visualization
Processing.js - Playing with particles and transitions
June 9, 2010
After having used having used Processing on and off for a couple of years I decided it was time to have a play with Processing.js. Heavily inspired by this I decided to have a play. This is my current "train-project" to keep me busy on my crazy daily commute - expect lots of changes and hopefully some polish sometime soon.
Oh, you'll need a decent and recent browser (Chrome or Firefox - unfortunately this doesn't work with Safari).
Categories: Fun, Processing
A nicer way of using the Django 1.2 messages framework
May 11, 2010
Update: Someone asked me "why?" recently, claiming you could just order messages in the view. It slipped my mind that the whole point of this solution is that it's usable in any template without extra work, so you can render the messages out in your base template and then forget about it.
In Django versions prior to 1.2 adding user feedback messages was cumbersome. In your view you'd use a snippet like this request.user.message_set.create(message='My message.').
to add a message to be retrieved and displayed to the user in the template. Messages were also attached to the current user and had no severity level.
Version 1.2 brings an easy to use messages framework. Adding a new message becomes as simple as messages.success(request, 'Your details have been updated.') or messages.warning(request, 'Your password expires in one week.'). Built-in levels are debug, info, success, warning and error, and adding your own is easy.
To retrieve the messages in the template you need to make sure your view renders the template with the current request as the context_instance like so:
def my_view(request):
# ...
return render_to_response('my_template.html',
my_dict,
context_instance=RequestContext(request))
And them spit them out in the template like this:
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
This is where I think the framework falls down. All your info, success, warning and error messages are in the same list. Whilst you can check the tag/level and include it as a CSS class for the <li> you can't get around that it's still the same list. It's quite common to see success and warning messages styled differently in <div>s with red and green backgrounds, and as far as I can tell there's no way of doing this with the framework without hackery.
To counter this I wrote a template tag that accepts the messages instance and separates all the messages out into separate <ul>s ordered by level so I can style up something like this.

The template tag is included in full below and you are free to use it as you wish. (Working example project on Github.)
from django import template
register = template.Library()
class MessagesNode(template.Node):
""" Outputs grouped Django Messages Framework messages in separate
lists sorted by level. """
def __init__(self, messages):
self.messages = messages
def render(self, context):
try:
messages = context[self.messages]
# Make a dictionary of messages grouped by tag, sorted by level.
grouped = {}
for m in messages:
# Add message
if (m.level, m.tags) in grouped:
grouped[(m.level, m.tags)].append(m.message)
else:
grouped[(m.level, m.tags)] = [m.message]
# Create a list of messages for each tag.
out_str = ''
for level, tag in sorted(grouped.iterkeys()):
out_str += '<div> class="messages %s">\n<ul">' % tag
for m in grouped[(level, tag)]:
out_str += '<li>%s</li>' % (m)
out_str += '</ul>\n</div>\n'
return out_str
except KeyError:
return ''
@register.tag(name='render_messages')
def render_messages(parser, token):
parts = token.split_contents()
if len(parts) != 2:
raise template.TemplateSyntaxError("%r tag requires a single argument"
% token.contents.split()[0])
return MessagesNode(parts[1])
Calling the tag form your template is then done like this:
{% load messages %}
...
{% render_messages messages %}
Easy!
This gives you a separate list, in a <div>, for each tag/level in the messages object (see below) so you're free to style them all differently.
<div class="messages success">
<ul>
<li>This is a success message.</li>
</ul>
</div>
<div class="messages error">
<ul>
<li>This is an error message.</li>
<li>This is another error message.</li>
</ul>
</div>
Visit Github for an example site using all the code, with sample CSS and images ready to drop into your own projects.
Categories: Django, Programming, Python
About me
Hi, I'm Ben Tappin.
I make stuff, mainly for the web. You can read more about me.
And you can hire me for freelance work.

