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 User(object): | |
... username = "bob" | |
... | |
>>> class SuperUser(object): | |
... position = "super" | |
... | |
... def __init__(self, user): | |
... self.user = user | |
... | |
... def __getattr__(self, attr): |
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
# admin.py - get the User form working the Django Admin w/ email usernames. | |
from django.contrib.auth.forms import UserChangeForm | |
from django.contrib.auth.admin import UserAdmin | |
class EmailUserChangeForm(UserChangeForm): | |
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w@.]+$', | |
help_text = _("Required. 30 characters or fewer. Valid email addresses only.")) | |
class EmailUserAdmin(UAdmin): |
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.core.paginator import Paginator as DefaultPaginator | |
class Paginator(DefaultPaginator): | |
def __init__(self, object_list, per_page, orphans=0, | |
allow_empty_first_page=True, request=None, page_var="page"): | |
assert request is not None, "request must be supplied." | |
super(Paginator, self).__init__(object_list, per_page, orphans, | |
allow_empty_first_page) | |
self.query = request.GET.copy() |
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
;(function($){ | |
$.inlineformset = { | |
defaults: { | |
extra: 3, | |
formSelector: '.form', | |
formsetSelector: '.formset', | |
hideOnDeletion: true, | |
}, |
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
""" | |
This Middleware inserts a link to the file which rendered the view for you. | |
Subclass and add the protocol of your favorite text editor to open the files | |
right from your web browser! | |
Now on DjangoSnippets: | |
http://www.djangosnippets.org/snippets/1546/ | |
Inspiration DjangoSnippets: | |
http://www.djangosnippets.org/snippets/766/ |
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
# Traditional - 9 lines | |
def view(request, template): | |
if request.method == "POST": | |
form = FormCls(request.POST) | |
if form.is_valid(): | |
form.save() | |
return HttpResponseRedirect("") | |
else: | |
form = FormCls() | |
return render_to_response(template, {"form": form}) |
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
import re | |
# Some mobile browsers which look like desktop browsers. | |
RE_MOBILE = re.compile(r"(iphone|ipod|blackberry|android|palm|windows\s+ce)", re.I) | |
RE_DESKTOP = re.compile(r"(windows|linux|os\s+[x9]|solaris|bsd)", re.I) | |
RE_BOT = re.compile(r"(spider|crawl|slurp|bot)") | |
def is_desktop(user_agent): | |
""" |
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
;(function($) { | |
$.fn.pop = function(options) { | |
var defaults = { | |
distance: 10, | |
time: 250, | |
delay: 500, | |
// pop: selector or element to show when this is hovered over | |
}; |
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
$(function() { | |
// Github Repo JSON Endpoint | |
var endpoint = "http://github.com/api/v1/json/johnboxall?callback=?"; | |
$.fn.sort = function() {return this.pushStack(jQuery.makeArray([].sort.apply(this, arguments)));}; | |
$.getJSON(ENDPOINT, function(data) { | |
// Remove all the old projects! | |
$("#projects li").remove(); | |
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 mechanize import Browser | |
from lxml import html as HTML | |
import pprint | |
ENDPOINT = "https://apps.cra-arc.gc.ca/ebci/rhpd/startLanguage.do?lang=English" | |
class PayrollCalculator(object): | |
def __init__(self): |