Skip to content

Instantly share code, notes, and snippets.

# post_commit and post_rollback transaction signals for Django with monkey patching
# Author Grégoire Cachet <gregoire.cachet@gmail.com>
# http://gist.github.com/247844
#
# Usage:
# You have to make sure to load this before you use signals.
# For example, create utils/__init__.py and then utils/transaction.py contening
# this gist in you project. Then add "import utils.transaction" in your project
# __init__.py file
#
@davehughes
davehughes / backbone-elemental-view.coffee
Created March 2, 2012 02:42
Experimental Backbone.View extension that takes a declarative approach to dealing with DOM elements.
# Copied from backbone.js
getValue = (object, prop) ->
if not (object and object[prop])
return null
if _.isFunction(object[prop]) then object[prop]() else object[prop]
eventSplitter = /^(\S+)\s*(.*)$/
namedSelectorPattern = /^{(.+)}$/
viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'elements']
@davehughes
davehughes / paginate.py
Created March 2, 2012 02:48
Django template filter for rendering an ellipsized paginator.
from django import template
from django.utils.safestring import mark_safe as safe
register = template.Library()
class PaginationNav(object):
def __init__(self, page_number=None, display=None, is_current=False):
self.page_number = page_number
self.is_current = is_current
self.display = display or page_number or ''
@davehughes
davehughes / modes.py
Created July 20, 2012 15:33
Implementation of cuisine modes that works by monkey-patching Fabric internals.
'''
Stack-based mode managers for mode_(sudo|user|local|remote).
'''
from fabric.api import env
MODE_STACKS = {}
env.cuisine_mode_sudo = False
env.cuisine_mode_local = False
class ModeStackManager(object):
@davehughes
davehughes / modes.py
Created July 20, 2012 16:00
Implementation of cuisine modes that works by monkey-patching Fabric internals.
'''
Stack-based mode managers for mode_(sudo|user|local|remote).
'''
from fabric.api import env
MODE_STACKS = {}
env.cuisine_mode_sudo = False
env.cuisine_mode_local = False
class ModeStackManager(object):
@davehughes
davehughes / monkey.py
Created July 31, 2012 18:53
Monkey patch to cuisine to improve mode_local behavior
import ssh
import subprocess
import time
from fabric.api import env, settings
from fabric.state import output
from fabric.operations import (_shell_wrap, _prefix_commands, _prefix_env_vars,
_sudo_prefix, _AttributeString)
from fabric.io import output_loop
from fabric.thread_handling import ThreadHandler
@davehughes
davehughes / gist:6458078
Created September 6, 2013 00:33
Implementation of User 'masquerade' in Django.
# 1) Add MasqueradeMiddleware to Django's middleware stack (see comment below for placement)
class MasqueradeMiddleware(object):
'''
Looks for a field in the active session specifying a user to masquerade as
and sets request.user to that user, storing the real user to the session.
This middleware is dependent on the existence of sessions, so it should be
deployed after ('inside') Django's session middleware. It should probably
be deployed after middleware like the TermsAndConditionsMiddleware which
implement one-time intercepts, so that masquerading superusers don't get
@davehughes
davehughes / config_resolver.py
Last active December 29, 2015 20:18
Utility object for expanding a dict of configuration template strings
class ConfigResolver(object):
def __init__(self, **kwargs):
self.config_templates = kwargs
self.config = resolve(copy.copy(kwargs))
def __getattr__(self, attr):
return self.config[attr]
def __getitem__(self, item):
@davehughes
davehughes / detect-duplicate-south-migrations.sh
Created June 2, 2014 23:49
Detect duplicate indices in South migrations between two git commits
RETURN_CODE=0
MIGS_PATH="thefundersclub/migrations"
@davehughes
davehughes / .nesh-preload.js
Last active August 29, 2015 14:06
Nesh shell initialization
// Exports to Nesh shell
// + Add a shell alias 'alias nesh="nesh --eval {this file}"' to automatically
// trick out your shell with these natural code enhancements.
__ = _ = require('lodash');
config = require('./common').config();
db = require('./models');
Q = require('q');
mongo = require('monk')(config.db.mongo.url);