Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am evansd on github.
  • I am evansd (https://keybase.io/evansd) on keybase.
  • I have a public key whose fingerprint is 9080 1864 3ACF D646 EF1E BBB1 6284 0EA7 FA7D 6680

To claim this, I am signing this object:

@evansd
evansd / django-csrf.js
Last active August 29, 2015 13:59
Adds support for Django's CSRF protection to jQuery's ajax method
// Adds support for Django's CSRF protection to jQuery's ajax method
(function() {
var cookieRegex = new RegExp('(?:^|;)\\s?' + 'csrftoken' + '=(.*?)(?:;|$)', 'i'),
match = document.cookie.match(cookieRegex),
csrftoken = match && decodeURIComponent(match[1]),
isSafeMethod = function(method) {
return /^(GET|HEAD|OPTIONS|TRACE)$/.test(method);
};
if (csrftoken) {
jQuery(document).ajaxSend(function(event, jqxhr, settings) {
@evansd
evansd / bootstrap-popover-clicktoclose.js
Last active August 29, 2015 13:59
Adds a 'clicktoclose' option to Bootstrap's popovers which will cause them to close if the user clicks anywhere outside of the popover
// Adds a 'clicktoclose' option to Bootstrap's popovers which will cause them
// to close if the user clicks anywhere outside of the popover
(function() {
var closeOnExternalClick = function(e) {
if ( ! this.$tip.has(e.target).length) { this.hide(); }
};
var Popover = $.fn.popover.Constructor.prototype;
var superInit = Popover.init;
Popover.init = function() {
superInit.apply(this, arguments);
@evansd
evansd / json.py
Last active August 29, 2015 14:01
from __future__ import absolute_import
import json
import re
from django import template
from django.utils.html import format_html_join
register = template.Library()
#!/usr/bin/env bash
# Installs the requested version of node on Heroku
# Example: source install_node_on_heroku '0.10.15'
# For use as part of a post_compile step
set -eo pipefail
# Pretty printing functions
[ $(uname) == "Darwin" ] && SED_FLAG='-l' || SED_FLAG='-u'
#!/usr/bin/env python
import fileinput, re
for line in fileinput.input(inplace=True):
if fileinput.isfirstline():
future_load = '{% load url from future %}'
if '{% extends' in line:
print line,
print future_load
else:
print future_load
import json
import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@evansd
evansd / loopback-fs.sh
Last active August 29, 2015 14:05
Create loopback filesystem
# Create a loop-mounted filesystem to allow a very high inode count
# without having to reformat entire partition
# Allocate a 1.5TB file without actually having to write 1.5TB of data
fallocate -l 1500G /path/to/filesystem.img
# Make an ext4 filesystem in this file with:
# (-i) One inode per 1024 bytes
# (-F) Force creation of the filesystem even though the
# file is not a block special device
"""
Extends WhiteNoise so that it always re-checks the mtimes of files before serving
(and updates Content-Length headers as well).
Possibly useful if you are using WhiteNoise in development and want to modify files
without restarting the server. (Note: this won't pick up added/deleted files, you'll
still need a restart for that.)
Use the Django setting `WHITENOISE_RECHECK_MTIMES` to enable.
import hashlib
import json
class HashWrapper(object):
"""
Wraps a hashlib object to make it file-like (i.e. give it a `write` method)
"""
def __init__(self, hashobj=None):