Skip to content

Instantly share code, notes, and snippets.

View bruth's full-sized avatar

Byron Ruth bruth

View GitHub Profile
@bruth
bruth / gist:1120062
Created August 2, 2011 12:02
Backbone Controller/Delegate Example
AppState = new Backbone.Model
domain: null
class Domain extends Backbone.Model
class DomainCollection extends Backbone.Collection
model: Domain
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
author = Author('John', 'Doe')
author.save()
author.pk = None
author.save()
author.pk # 2
class Book(models.Model):
title = models.CharField(max_length=50)
author = models.ForeignKey(Author)
summary = models.TextField()
pub_date = models.DateField()
book = Book('John Doe: The Autobiography', author,
'A long, long time ago..', date(2011, 9, 3))
book.save()
class Book(models.Model):
author = models.OneToOneField(Author)
...
class Book(models.Model):
authors = models.ManyToManyField(Author)
...
authors = book.authors.all()
book.pk = None
book.save()
book.authors = authors
@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