Skip to content

Instantly share code, notes, and snippets.

View Wilfred's full-sized avatar

Wilfred Hughes Wilfred

View GitHub Profile
@Wilfred
Wilfred / clickablecolumn.py
Created June 6, 2013 13:44
column with URL to a model for django_tables2
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
import django_tables2 as tables
class ClickableColumn(tables.Column):
"""Renders a link to edit the current object."""
def __init__(self, url_name, *args, **kwargs):
self.url_name = url_name
@Wilfred
Wilfred / fakeCheckbox.js
Created June 20, 2013 16:50
Styling <input type="checkbox"> using <span>s and updating the input accordingly according to events
// replace real checkboxes with fake ones using spans that work the same
$('input[type=checkbox]').each(function() {
var $input = $(this);
$input.hide();
var $fakeCheckbox = $('<div class="fake-checkbox"><span class="icon-TICK"></span></div>');
$input.after($fakeCheckbox);
// when our fake checkbox is clicked
$fakeCheckbox.click(function() {
@Wilfred
Wilfred / irc.conf
Created June 25, 2013 11:40
supervisor configuration for ii
[program:irc]
command = /usr/bin/ii -s irc.freenode.org -i /tmp -n bot_user_name
@Wilfred
Wilfred / admin.py
Created June 27, 2013 13:28
adding custom admin actions for built-in Django models
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
actions = ['my_action']
def my_action(self, request, queryset):
# stuff here...
@Wilfred
Wilfred / response_test.py
Last active December 19, 2015 07:18
Some additional assertions for convenient Django unit testing
from django.test import TestCase
class ResponseTestCase(TestCase):
"""Some additional assertions for Django responses."""
def assertHttp200(self, response):
status_code = response.status_code
self.assertEqual(
status_code, 200,
"Expected a HTTP 200 response, but got HTTP %d" % status_code)
@Wilfred
Wilfred / arithmetic.py
Created July 8, 2013 15:41
digg-style pagination with Django templates, e.g. "... 3 4 5 *6* 7 8 9 ..." and Twitter bootstrap
from django import template
"""It's sometimes really handy to be able to do basic arithmetic in
Django templates.
"""
register = template.Library()
@Wilfred
Wilfred / gist:6010761
Created July 16, 2013 17:28
wget sentry homepage
$ wget "http://www.getsentry.com" [18:13:49]
--2013-07-16 18:19:37-- http://www.getsentry.com/
Resolving www.getsentry.com (www.getsentry.com)... 67.228.250.100
Connecting to www.getsentry.com (www.getsentry.com)|67.228.250.100|:80... failed: Connection timed out.
Retrying.
--2013-07-16 18:21:45-- (try: 2) http://www.getsentry.com/
Connecting to www.getsentry.com (www.getsentry.com)|67.228.250.100|:80... failed: Connection timed out.
Retrying.
@Wilfred
Wilfred / cached_instances.py
Created September 17, 2013 17:44
Ensure no class gets instantiated twice with the same arguments.
import copy
def make_hash(obj):
"""Make a hash from an arbitrary nested dictionary, list, tuple or
set.
"""
if isinstance(obj, set) or isinstance(obj, tuple) or isinstance(obj, list):
return hash(tuple([make_hash(e) for e in obj]))
@Wilfred
Wilfred / example.sh
Created September 23, 2013 11:02
Installing Xapian in a virtualenv
ln -sv /usr/lib/python2.6/dist-packages/xapian $VENV_HOME/lib/python2.6/site-packages
@Wilfred
Wilfred / lexer.py
Created October 19, 2013 23:27
rpython complains about the type here
import sys
import re
WHITESPACE = 'whitespace'
OPEN_PAREN = 'open-paren'
CLOSE_PAREN = 'close-paren'
INTEGER = 'integer'