Skip to content

Instantly share code, notes, and snippets.

qs = MyRelatedObject.objects.all()
obj_dict = dict([(obj.id, obj) for obj in qs])
objects = MyObject.objects.filter(myrelatedobj__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(obj.myobject_id, []).append(obj)
for id, related_items in relation_dict.items():
obj_dict[id].related_items = related_items
<script>
<link href='prettify.css' rel='stylesheet' type='text/css'/>
<script src='prettify.js' type='text/javascript'>
</script>
class JsonpResponse(HttpResponse):
def __init__(self, data, callback):
json_encoder = LazyEncoder(ensure_ascii=False)
json = json_encoder.encode(data)
jsonp = "%s(%s)" % (callback, json)
HttpResponse.__init__(
self, jsonp,
mimetype='application/json'
)
from datetime import timedelta, datetime
def first_sunday_on_or_after(dt):
days_to_go = 6 - dt.weekday()
if days_to_go:
dt += timedelta(days_to_go)
return dt
# For a complete and up-to-date set of DST rules and timezone definitions,
# visit the Olson Database: http://www.twinsun.com/tz/tz-link.htm or
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend
class CIModelBackend(ModelBackend):
"""
Overrides the default authentication backend (ModelBackend)
to support case-insensitive login.
"""
def authenticate(self, username=None, password=None):
try:
class EmailUserCreationForm(UserCreationForm):
'''
User login form class that replaces the 'username' field with
an 'emailfield' on registration, sets email = username in the
User object, and adds case-insensitive username verification.
'''
username = forms.EmailField(label=("Email Address"))
confirm = forms.BooleanField(error_messages={'required':
'Please read and accept the Terms and Conditions to continue'})
filetype off
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
filetype plugin indent on
set nocompatible
set modelines=0
let mapleader = ","
inoremap jj <ESC>
from django.test import TestCase
from django.utils.importlib import import_module
from django.http import HttpRequest
from django.conf import settings
class SampleTest(TestCase):
def setUp(self):
# Create mock request object with session key
engine = import_module(settings.SESSION_ENGINE)
request = HttpRequest()
@dickbrouwer
dickbrouwer / gist:2368756
Created April 12, 2012 16:15
iOS singleton using +initialize
+ (void)initialize {
static BOOL initialized = NO;
if (!initialized) {
initialized = YES;
_sharedManager = [Manager new];
}
}
@dickbrouwer
dickbrouwer / gist:2368787
Created April 12, 2012 16:19
Create singleton using dispatch_once_t
+ (UserManager *)sharedManager {
static dispatch_once_t once;
dispatch_once(&once, ^ {
_sharedManager = [[self alloc] init];
});
return _sharedManager;
}