Skip to content

Instantly share code, notes, and snippets.

View acdha's full-sized avatar

Chris Adams acdha

View GitHub Profile
@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 / 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 %}
@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 / 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
@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'
@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):
@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;
@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) {