This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
<link href='prettify.css' rel='stylesheet' type='text/css'/> | |
<script src='prettify.js' type='text/javascript'> | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (void)initialize { | |
static BOOL initialized = NO; | |
if (!initialized) { | |
initialized = YES; | |
_sharedManager = [Manager new]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (UserManager *)sharedManager { | |
static dispatch_once_t once; | |
dispatch_once(&once, ^ { | |
_sharedManager = [[self alloc] init]; | |
}); | |
return _sharedManager; | |
} |
OlderNewer