Skip to content

Instantly share code, notes, and snippets.

View bruth's full-sized avatar

Byron Ruth bruth

View GitHub Profile
@bruth
bruth / finders.py
Created November 1, 2011 17:17
Django staticfiles storage and finder classes for apps to copy static files into their own directory
# Add:
#
# STATICFILES_FINDERS = (
# 'path.to.this.module.PrefixedAppDirectoriesFinder',
# ...
# )
from django.contrib.staticfiles.finders import AppDirectoriesFinder
from django.contrib.staticfiles.storage import AppStaticStorage
@bruth
bruth / multi-app.conf
Created November 3, 2011 00:33
Dynamic location-based mass hosting approach with maintenance mode
<VirtualHost *:80>
# Define a conventional location for where are apps are deployed
# Locations such as "/apps/foo" and "/apps/bar" will match result
# in "foo" and "bar", respectively, being the application "names".
WSGIScriptAliasMatch ^/apps/([^/]+) /usr/local/srv/apps/$1-env/$1/src/conf/wsgi/app.py
# This example derived from deployed multiple Django applications, thus
# the static and media directories are mapped here.
AliasMatch ^/apps/([^/]+)/static/(.*) /usr/local/srv/apps/$1-env/$1/src/_static/$2
@bruth
bruth / queue_contexts.py
Created November 5, 2011 13:09
Django cache and session queue context managers
# Inspired by http://code.activestate.com/recipes/576567/
import time
from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
class cache_queue(object):
"A context manager for queuing Django cache operations."
poll_interval = 0.005 # in seconds
# Copyright (C) 2011 by Blade Polska s.c.
# Full rights belong to Tomek Kopczuk (@tkopczuk).
# www.askthepony.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@bruth
bruth / example_model.py
Created March 8, 2012 15:07
ETag "ready" Django model using a version number
class Song(VersionedModel):
"""
>>> song = Song(title='Here Come the Sun')
>>> song.save()
>>> cache.get('music.song-1,1)
1
>>> song.title = 'Here Comes the Sun'
>>> song.save()
>>> cache.get('music.song-1,1)
>>> cache.get('music.song-1,2)
@bruth
bruth / main.js
Created March 26, 2012 17:44
Clean dynamic Python/Java package-style pattern for JavaScript modules
// Example directory structure
// js/
// package.js
// package/
// mod1.js
// mod2.js
// mod3.js
require(['package'], function(pack) {
var obj1 = new pack.Mod1Class(),
obj2 = new pack.Mod2Class();
@bruth
bruth / README.md
Created April 23, 2012 15:15
App environment

Client-side Environment Boilerplate

Sets up the environment for a client-side application.

  • Remove jQuery as a global
  • Change Underscore template parsers to not use ERB-style tags
  • Setting a reasonable timeout for AJAX requests
  • Define a AJAX queue manager for Backbone, inspired by @maccman's Spine implementation (Backbone as of 87c9b17a required)
    • GET requests are executed outside of the queue
  • onbeforeunload handler defined for prevent navigation when there are pending requests
@bruth
bruth / README.md
Created April 27, 2012 02:54
jQuery groupBy => HTML

groupBy => HTML

jsFiddle Example

Takes an object with the format key => list and a list of keys to exclude from the table. Returns an array of groups. The keys should be the ones used as the iterator or by the iterator when performing the group by to prevent redundancy of data in the output table.

Since JavaScript object keys can only be numbers or strings, if the data was grouped by multiple keys, the group key must be a string delimited by some value. By default, the separator is a comma ,.

groupByHTML(groups, keys[, sep])
require(['jquery'], function($) {
var data,
form = $('#queue-download-form'),
url = queueDownloadForm.attr('action');
form.on('submit', function(event) {
event.preventDefault();
// for JSON
data = JSON.stringify(form.serializeArray());
@bruth
bruth / deferrable.coffee
Created June 14, 2012 00:28
Deferrable Mixin
define ['jquery', 'underscore'], ($, _) ->
Deferrable =
deferred: {}
initialize: ->
@pending()
# Takes an object of method names and flags for implicitly
# wrapping class methods in a deferred execution
for method, once of @deferred