Django template tag to display latest git commit, date and tag in template

I found a few references to bits and pieces of this in various places, but this snippet ties all three bits of information together in a single string, and accounts for calling the path to the git repo from anywhere (which tends to work “magically” on localhost but breaks on servers).

If you don’t use git tags, season to taste.

@register.simple_tag
def git_ver():
    '''
    Retrieve and return the latest git commit hash ID and tag as a dict.
    '''

    git_dir = os.path.dirname(settings.BASE_DIR)

    try:
        # Date and hash ID
        head = subprocess.Popen(
            "git -C {dir} log -1 --pretty=format:\"%h on %cd\" --date=short".format(dir=git_dir),
            shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        version = head.stdout.readline().strip().decode('utf-8')

        # Latest tag
        head = subprocess.Popen(
            "git -C {dir} describe --tags $(git -C {dir} rev-list --tags --max-count=1)".format(dir=git_dir),
            shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        latest_tag = head.stdout.readline().strip().decode('utf-8')

        git_string = "{v}, {t}".format(v=version, t=latest_tag)
    except:
        git_string = u'unknown'

    return git_string

Then, in your template, you can simply:

{% if user.is_superuser %}{% git_ver %}{% endif %}

GoPro Time Lapse: Stinson Beach

Last weekend at Stinson Beach I attached a GoPro to a tree and had it shoot one image every 5 seconds for a couple of hours. Later compiled the image into a 29fps half-speed video in GoPro Studio. Not sure why I enjoy making these so much; something oddly satisfying about the process.

View 403, 404, 500 with media in Django DEBUG mode

When working with Django in DEBUG mode, it can be tough to see your 403, 404, and 500 views, since they raise visible stack traces instead of the UX the end user will see. But if you turn DEBUG off, runserver’s local media serving is disabled because it’s designed to work only with DEBUG = True. The solution is scattered throughout the Django docs, and I couldn’t find it compiled into one compact code block anywhere – just reference the handling functions directly from the end of your urls.py:

if settings.DEBUG:
    from django.views.defaults import server_error, page_not_found, permission_denied
    urlpatterns += [
        url(r'^500/$', server_error),
        url(r'^403/$', permission_denied, kwargs={'exception': Exception("Permission Denied")}),
        url(r'^404/$', page_not_found, kwargs={'exception': Exception("Page not Found")}),
    ]

And voila, your 403.html, 404.html, and 500.html templates will be displayed in full glory for developers.

Remove Duplicates, Fix Broken Album Art in iCloud Music Library

Apple Music + iCloud Music Library is a brilliant pairing, and finally lets us access our personal music collections from anywhere. But it’s not without its warts – duplicated tracks and bad/missing cover art has been a sore spot for iCloud Music Library users since the service launched. In my first piece for Medium.com, I walk readers through the reasons – and the fixes – for those two problems.

record-collection

The Ethics of Modern Web Ad-Blocking

For years, I’ve resisted – and argued against – using web ad blockers of any kind. After spending a decade working at a journalism school and watching publishers large and small struggle (and mostly fail) to find a way to be paid for their essential efforts, it felt like bearing a certain amount of advertising was the very least we-the-public could do to support quality journalism. Paywalls don’t work for almost any publications – what else is there?

But the rise of the mobile web tipped the scales – the “social contract” around advertising was no longer a fair one. The mobile experience is far less tolerant of intrusion, and network speeds are slower. But because monetization is more difficult, publishers were “forced” to insert more, and more intrusive advertising. The cumulative effect has been a steady decline in the quality of mobile browsing. Today, many news sites are close to unusable on a smart phone, having become choked out by network and screen-stealing crap.
Continue reading

Sane Password Strength Validation for Django with zxcvbn

While many admins and blog posts tell users that length is by far the most important factor in creating strong passwords/passphrases, the majority of password input fields are giving them a set of hide-bound rules: Eight characters, at least one upper- and one lowercase letter, some digits and punctuation marks, etc.

Even though it includes dictionary words, a passphrase like:

Sgt. Pepper's Mr. Kite

is far stronger than:

js72(.Tb8

(there’s a world of difference between 22 characters and 9, from a cracking perspective). But many password input fields would reject the first one. No wonder users are confused by the process of creating strong passwords!
Continue reading

What Is Code?

I’ve been thinking recently about how some people have jobs that most everyone can “understand” more or less – we all know kinda sorta what a teacher or a policeman does – while others work in areas that are virtually inaccessible to the general public. I’m often reminded how little my family and closest friends understand about how I spend my days.

So Paul Ford of Businessweek has written this colossal, 38,000-word article “What Is Code” that attempts to bridge that gap. I’m not sure it succeeds (or that any article could) but it’s a fine attempt and a damn good read. Even for coders. It took hours to get through, and reading is not generally how I like to spend my weekend time, but it was time very well spent. Super recommend.

What Is Code

Maker Faire 2015

More images in the Flickr Set.

This year was the 10th birthday of “The Greatest Show and Tell on Earth” – that Rainbow Gathering of robot makers, sculptors, hackers, welders, Burning Man attendees with kids, benders of light, food artisans, bicycle tweakers, DJs and artistic misfits.

Watches

I’m proud to be able to say I’ve taken my child to Maker Faire @ San Mateo every single year since 2006, meaning we haven’t missed a single event.

Iris

Light Sculpture

Despite the annoying aspect of the ever-growing crowds, it’s become a father-son tradition we look forward to every year, and we can’t imagine ever skipping it at this point. Every year is both “more of the same” and completely different.

Creature Quad

Certain exhibits seem almost perennial, but there are always tons of new surprises. It was especially nice to have cooler temperatures this year – low 60s meant we were able to do a full eight hours on the fairgrounds without missing a beat.

More fire-breathing giant beastie sculptures than ever before:

Robot sculpture fire

Riding Cyclecide’s collection of hacked bicycles is always our favorite part of the day. Bikes with hinges in the middle of the frame are almost impossible to ride, but you do kind of get the hang of it after a while.

Cyclecide

Same with the reverse-steering-gear bike that turns the opposite of the direction you turn the wheels. Our fave this time was the bike with off-center axles, making it feel like it’s navigating bumpy terrain even on flat ground.

Cyclecide

The “dark room” seemed better than ever, with more sophisticated interactives, plus a truly gorgeous wall-sized mixed-materials glowing sculpture reminiscent of a time tunnel receding into space.

Light Sculpture

We’ve admired the masking tape cities and gardens every year (now represening 10,000 hours of work and more than 27 miles of tape!), and for the first time this year we actually sat down for a 30-minute lesson on masking tape “origami.”

Masking Tape Art

And Miles had his first opportunity to sit at the helm of an original Apple IIe, just like the ones we used in high school in the early 80s:

Apple IIe

Totally loved the “junk” drumming of John F. King:

More images in the Flickr Set.