Skip to content

Instantly share code, notes, and snippets.

@nicksnell
nicksnell / django_http_auth.py
Created November 1, 2013 13:09
Dead simple HTTP Auth decorator for django. Just add @http_basic_auth(username='...some user...', password='...some pass...') round the view
from django.http import HttpResponse, HttpResponseForbidden
def _auth_request(realm):
response = HttpResponse()
response.status_code = 401
response['WWW-Authenticate'] = 'Basic realm="%s"' % realm
return response
def http_basic_auth(username=None, password=None, realm='Authorization required'):
@nicksnell
nicksnell / vimeo_thumbnail.py
Created December 19, 2013 20:52
Fetches a thumbnail URL for a given video (and size) from Vimeo metadata API.
import json
import urllib2
def get_vimeo_thumbnail(vimeo_video_id, size='large'):
"""Fetches a thumbnail URL for a given video (and size) from Vimeo metadata API"""
assert size in ['small', 'medium', 'large'], 'You must specify a valid size! small/medium/large'
req = urllib2.urlopen('http://vimeo.com/api/v2/video/%s.json' % vimeo_video_id)
@nicksnell
nicksnell / model_analyzer.py
Created February 25, 2014 10:16
Utility to pull all the callable methods from a given Django model. Tries to ignore default internal methods.
import yaml
DEFAULT_IGNORE = [
'__unicode__', '__module__', '_meta', 'MultipleObjectsReturned', '_base_manager',
'objects', 'DoesNotExist', '__doc__', '_default_manager', '_curried',
]
def analyze_model(model, ignore=[], default_ignore=True):
"""Analyze a Django Model to return a list of methods along with
the methods docstring"""
@nicksnell
nicksnell / djangocms_clean_placeholders.py
Created February 26, 2014 12:23
Utility to clear our unused Django CMS placeholder data from database - USE AT OWN RISK.
"""Utility to clear our unused Django CMS placeholder data from database"""
# Nick Snell <n@nicksnell.com>
# WARNING; IS A DISTRUCTIVE OPERATION (Obviously.)
from django.template import TemplateDoesNotExist
from cms.models.pagemodel import Page
from cms.utils.plugins import get_placeholders
@nicksnell
nicksnell / tiler.py
Created July 25, 2014 17:26
Create a mosaic of tiles
from PIL import Image, ImageOps
import requests
class Tiler(object):
def __init__(self, images, rows=4, tile_size=(135, 135)):
self.images = images
self.rows = rows
self.tile_size = tile_size
@nicksnell
nicksnell / webfaction-mongo-start.sh
Created September 2, 2014 22:00
start script for mongo on webfaction
#!/bin/sh
PREFIX=$HOME/webapps/mongo_db
$PREFIX/mongodb-linux-x86_64-2.6.3/bin/mongod --auth --dbpath $PREFIX/data/ --port <some port> --fork --logpath=$HOME/logs/user/mongod.log
@nicksnell
nicksnell / webfaction-mongo-stop.sh
Created September 3, 2014 21:15
stop script for mongo on webfaction
#!/bin/sh
PREFIX=$HOME/webapps/mongo_db
kill -2 `cat $PREFIX/data/mongod.lock`
@nicksnell
nicksnell / webfaction-mongo-restart.sh
Created September 3, 2014 21:16
restart script for mongo on webfaction
#!/bin/bash
$HOME/webapps/mongo_db/bin/stop
sleep 4
$HOME/webapps/mongo_db/bin/start
@nicksnell
nicksnell / osx-setup.md
Last active December 4, 2015 23:37
Script for setting up Homebrew and a bunch of apps

OSX Setup

  1. Software update
  2. Setup XCode
  3. Install Oh-my-ZSH

curl -L http://install.ohmyz.sh | sh

  1. Generate SSH keys
  2. Install Dotfiles
@nicksnell
nicksnell / osx-stop-host-renaming.sh
Created November 24, 2014 09:54
Stop annoying OSX renaning of computer hostname :|
sudo /usr/libexec/PlistBuddy -c "Add :ProgramArguments: string --no-namechange" /System/Library/LaunchDaemons/com.apple.discoveryd.plist