Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View acdha's full-sized avatar

Chris Adams acdha

View GitHub Profile
@acdha
acdha / bookmarklet-template.js
Created April 3, 2009 21:04
A template for creating bookmarklets which load jQuery from the Google CDN - see http://chris.improbable.org/2009/apr/3/cleaning-up-the-web-with-jquery-and-a-little-help/
/*
To avoid polluting the global namespace this uses an anonymous
function which is called with either the URL for an external
JavaScript file or a function. In either case jQuery will be loaded
from the Google CDN before your code is executed so it's free to
depend on jQuery without checking for it and can do things like
jQuery.getScript() to load other components (e.g. jQuery UI),
stylesheets, etc.
*/
(function (target, msg) {
@remy
remy / gist:330318
Created March 12, 2010 13:59
autofocus and placeholder support
/**
* Add this script to the end of your document that use <input autofocus type="text" />
* or <input type="text" placeholder="username" /> and it'll plug support for browser
* without these attributes
* Minified version at the bottom
*/
(function () {
function each(list, fn) {
var l = list.length;
@kmike
kmike / asserts.py
Created September 10, 2010 12:24
assertNumQueries decorator and context manager
import functools
import sys
import re
from django.conf import settings
from django.db import connection
def shrink_select(sql):
return re.sub("^SELECT(.+)FROM", "SELECT .. FROM", sql)
def shrink_update(sql):
@jbalogh
jbalogh / urlconf_decorator.py
Created September 23, 2010 17:32
Apply a decorator to a whole urlconf instead of a single view function.
"""
Apply a decorator to a whole urlconf instead of a single view function.
Usage::
>>> from urlconf_decorator import decorate
>>>
>>> def dec(f):
... def wrapper(*args, **kw):
... print 'inside the decorator'
@acdha
acdha / solr_extraction_backend.py
Created December 28, 2010 20:46
django-haystack "hackend" which stuffs values in through the Solr extraction handler for rich content indexing
# encoding: utf-8
import logging
from poster.encode import multipart_encode
from pysolr import SolrError, safe_urlencode
from haystack.backends.solr_backend import *
# For sanity:
from haystack.backends.solr_backend import SearchBackend as StandardSolrBackend
@acdha
acdha / content_extraction_index.py
Created December 30, 2010 21:16
Django haystack prepare() method using proposed backend.extract() for rich-content handling
class MyIndex(SearchIndex):
text = fields.CharField(document=True, use_template=False)
def prepare(self, obj):
data = super(MyIndex, self).prepare(obj)
extracted_data = self.backend.extract(open(obj.file_field.path, "rb"))
if extracted_data is not None:
for k, v in extracted_data['metadata'].items():
@acdha
acdha / object_history.html
Created February 8, 2011 19:19
django-tastypie VersionDiffResource helper for django-reversion
{% extends "admin/object_history.html" %}
{% load adminmedia %}
{% block extrahead %}
{{ block.super }}
<script src="{% admin_media_prefix %}js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
{% endblock %}
{% block content %}
@dpnova
dpnova / gist:1223933
Created September 17, 2011 13:26
invalidate cache_page entries in django
def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None, method="GET"):
"""
This function allows you to invalidate any view-level cache.
view_name: view function you wish to invalidate or it's named url pattern
args: any arguments passed to the view function
namepace: optioal, if an application namespace is needed
key prefix: for the @cache_page decorator for the function (if any)
from: http://stackoverflow.com/questions/2268417/expire-a-view-cache-in-django
added: method to request to get the key generating properly
@acdha
acdha / chunked_iterator.py
Created December 7, 2011 23:02
Python iterator using arbitrary slices: handy for processing objects like django-haystack SearchQuerySets which trigger backend I/O on slicing
from itertools import count
def chunked_iterator(iterable, chunk_size):
"""Given a slice-able yield individual items but consume them in chunk_size
batches so we can e.g. retrieve records from Solr in 50-100 batches
rather than the default 10
Note that unlike the itertools grouper example we must use slice notation
to trigger things like Haystack's sliced __getitem__ to set our own batch
size
@carljm
carljm / runner.py
Created December 9, 2011 04:07
Unittest2 test discovery and real dotted-path named test selection for Django
"""
An alternative Django ``TEST_RUNNER`` which uses unittest2 test discovery from
a base path specified in settings, rather than requiring all tests to be in
``tests`` module of an app.
If you just run ``./manage.py test``, it'll discover and run all tests
underneath the ``TEST_DISCOVERY_ROOT`` setting (a path). If you run
``./manage.py test full.dotted.path.to.test_module``, it'll run the tests in
that module (you can also pass multiple modules).