Skip to content

Instantly share code, notes, and snippets.

View kezabelle's full-sized avatar

Keryn Knight kezabelle

View GitHub Profile
@kezabelle
kezabelle / test1.py
Last active August 29, 2015 13:55
Testing various ways to test a dictionary (say, a wsgi environ) for an AJAX request
stmt = """
environ = {
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'
}
if 'HTTP_X_REQUESTED_WITH' in environ:
result = environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
"""
timeit.timeit(stmt=stmt, number=10000000)
# yields 2.7133121490478516
@kezabelle
kezabelle / form.py
Created February 14, 2014 09:16
For bt4 on #django IRC
class MyForm(Form):
def __init__(self, request, *args, **kwargs):
user = request.user
super(MyForm, self).__init__(*args, **kwargs)
def my_fake_validator(value):
return my_real_validator(value, user)
# can't remember if this is the right syntax
self.fields['myfield'].validators.append(my_fake_validator)
@kezabelle
kezabelle / thing.py
Created February 15, 2014 14:47
Regardless of what the function does, I want to roll back the changes it made to the database. I have no idea how.
def function(**kwargs):
Thing.objects.create(**kwargs)
Thing2.objects.get_or_create(**kwargs)
# desired usage:
transaction.let_the_objects_buildup()
try:
function(a=1, b=2, c=3)
finally:
@kezabelle
kezabelle / urls.py
Created April 4, 2014 10:14
django-uuidfield fix to prefetch_related. No idea what else it breaks instead though :|
from django.conf.urls import patterns, include, url
from uuidfield import UUIDField
# Fix the bloody UUID field so that we can do prefetching.
UUIDField._old_to_python = UUIDField.to_python
def maybe_fix_to_python(self, value):
return unicode(UUIDField._old_to_python(self, value=value))
UUIDField.to_python = maybe_fix_to_python
@kezabelle
kezabelle / scrollup.js
Created April 13, 2014 14:58
Hacking out a simple "return to top"
;(function($, window, document, undefined) {
var totop;
var show_at;
var already_shown;
var didscroll;
var update_didscroll;
var maybe_should_display;
var move;
/*
@kezabelle
kezabelle / example.py
Created May 22, 2014 14:19
for omarek in #django IRC
itin_ids = EventItinerary.objects.all().values_list('itinerary', flat=True)
places = Schedule.objects.filter(itinerary__in=itin_ids).distinct().values_list('place', flat=True)
destins = Place.objects.filter(pk__in=places)
@kezabelle
kezabelle / urls.py
Created June 1, 2014 13:51
Faking Django urlpatterns; yes really.
class DoItLazy(object):
def __iter__(self):
from django.contrib import admin
admin.autodiscover()
yield url(r'^admin/', include(admin.site.urls))
def __reversed__(self):
return reversed(tuple(iter(self)))
def __radd__(self, other):
@kezabelle
kezabelle / pip.conf
Created June 21, 2014 17:28
for xmj in #django IRC; goes in ~/.pip/pip.conf
[global]
timeout = 15
download_cache = /home/whatever/pipcache
use-wheel = true
wheel-dir = /home/whatever/pipwheels
find-links = /home/whatever/pipwheels
@kezabelle
kezabelle / user_admin.py
Last active August 29, 2015 14:02
Become some other random user via the Django admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib.auth import login, get_backends
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib import admin
from django.contrib.admin.sites import NotRegistered
@kezabelle
kezabelle / does_thread_issue_exist.py
Created July 10, 2014 07:19
Accessing `self.x` vs accessing a locally scoped equivalent?
class MySharedObject(object):
"""
If using a locally scoped dictionary to check for duplicates,
do the same thread issues arise?
Note that I don't care about what gets stored -- it should be
the same end-result regardless of which threads do what, the
ValueError is mainly a developer-awareness issue.
"""
def do_stuff(self):