Author Archives: Ben Miller

Backbone history and IE9

This bit me the other day, so I thought I’d share the pain.

IE9 doesn’t support pushState as you probably know which meant everything was routing to root (as it were).

The following snippet checks and resorts to hash based routing if it can’t cut the mustard:

app.on('initialize:after', function() {
    if(Backbone.history && !Backbone.History.started) {
        if(!(window.history && history.pushState)) {
            Backbone.history.start({ pushState: false, silent: true });
            var fragment = window.location.pathname.substr(
                Backbone.history.options.root.length);
            Backbone.history.navigate(fragment, { trigger: true });
        }
        else {
            Backbone.history.start({ pushState: true });
        }
    }
});

Add it wherever you would initialize Backbone history – often the entry point of the app. Mine for instance has an app.js that is initialised by main.js

About us: Isotoma is a bespoke software development company based in York and London specialising in web apps, mobile apps and product design. If you’d like to know more you can review our work or get in touch.

Hacking Trac 0.12

We use Trac at Isotoma. Actually that’s a bit of an understatement, we don’t just use it, it’s integral to our project development process.

Trac offers a wiki, ticketing system and svn browser out of the box, but it’s the increasingly featureful plugins API that allows you to adapt, hack and install to fit your purpose.

Home grown tweaks

We’re hopping a couple of versions and upgrading to 0.12 in the next few weeks and with it being a system that everyone uses daily, there’s been more than a few enhancement requests for the new installation.

Here are a couple of the plugins that made it through the requirements gathering process:

Isotoma Theme

Of course natively you can tweak the static content and templates to your hearts content. The TracThemeEngine plugin wraps this up into a neater package so you can customise your install to add extra markup, follow your own style, or even make it look like Alta Vista circa 1997. We plumped for following our own style.

HotKeys Plugin

My muscle memory often causes an involuntary “ctrl+s” when editing wiki entries. One too many save dialogs prompted this plugin which maps scripts to keyboard shortcuts using the JQuery HotKeys plugin (the version of jquery.hotkeys is from John Boxall’s fork on github, which allows inputs and textareas to be bound).

QuickTicket Plugin

RTM like input for quickly adding tickets.

As a side note, this plugin also hijacks a keyboard shortcut (“ctrl+q”  – used by Firefox, amongst others, for “quit”). An abominable accessibility crime to be sure, but we’re in the rare position here that we are our target audience.

WikiTable Macro

Adapted from work by optilude on Trac-Hacks, this macro allows SQL to be included in wiki pages to show query results in a table. We added DSN mappings that you add to trac.ini allowing you to query databases other than the current instance’s.

Third party plugins from Trac-Hacks

The invaluable Trac-Hacks should be the first port of call for looking into expanding on Trac’s functionality. For reference, here are a few of the plugins we’ll be implementing come the roll out:

Batch Modify Plugin

Allows users to modify several tickets together in one shot.

Timing and Estimation Plugin

For estimation and time tracking. We’re using the permissions branch.

Git Plugin

Essential. Enables Git support for the versioning system backend.

Further reading

The documentation on the Edgewall’s Trac site is pretty comprehensive:
http://trac.edgewall.org/wiki/TracDev/PluginDevelopment

For help on the Genshi template language and content parser, again, you could do worse than looking at the documentation:
http://genshi.edgewall.org/

Join the mailing list here:
http://groups.google.com/group/trac-dev/

And finally, 9 times out of 10, someone will have written a plugin for it and submitted it to Trac-Hacks:
http://trac-hacks.org/

Returning an actual proper real life HTTP code from a Django error page

Go to a non existent page on a Django site and you will (hopefully) be met with a friendly error page telling you not to panic, everything is OK and all you’ve done is mistyped the URL or something.

If it’s your thing, you may be interested enough to look see what the actual HTTP code for the page is in the header; chances are that it will be a 200 rather than a 404 as the default handler just passes the dealings onto the HttpResponse class.

Generally speaking this is fine, but there are situations where an accurate code would be very handy, as I found out the other day when I was trying to detect whether a file had been uploaded to a remote server. Scraping the resultant HTML for “Page not found” is not my idea of a robust solution.

So, instead, pass the error page’s HTML into the respective class by putting something like this in urls.py:


handler404 = 'urls.return_404'
handler500 = 'urls.return_500'

def return_404(request):
	return HttpResponseNotFound(
                render_to_string("errors/404.html"))

def return_500(request):
	return HttpResponseServerError(
                render_to_string("errors/500.html"))

Fullest of props to PiotrLegnica at Stack Overflow for this most elegant of solutions.

Edit: After further examination (see the comments) the default handlers do act as expected, but you’re still restricted to where you put your error templates, i.e. the root of the templates directory.
To my mind, it’s neater if you can specify a dedicated location.

Django templates derived from the view docstring for rapid prototyping

The rather verbose title says it all really.

We had a need the other week to churn out the skeleton of a site to see how the different areas fitted together.
As it was being written in Django anyway I put together this quick ‘n dirty utility that renders the reStructuredText docstring of a view to the returned response, so you can quickly put in page furniture and links to other views without having to go to the effort of creating templates.


def docview(fn):
    from docutils.core import publish_string
    from django.http import HttpResponse

    r = HttpResponse(publish_string(
            source=fn.func_doc,
            writer_name='html'))
    
    return lambda rtn: r

Then decorate your method:


@docview
def fast_view(request):
    """
===========
A Fast View
===========

With links to:

 * `An even faster view </faster_view>`_
 * `Somewhere else <http://isotoma.com>`_
 
    """

Which will render:

A Fast View

With links to: