djangosnippets.org: Latest snippets posted by coleiferhttps://djangosnippets.org/users/coleifer/2011-07-03T10:39:10.313684-05:00coleiferAdd CSRF token to templates
2011-07-03T10:39:10.313684-05:00coleiferhttps://djangosnippets.org/snippets/2478/<p>Searches through templates, adding the <code>{% csrf_token %}</code> tag whenever it finds a form that posts.</p>
Freely redistributableGeneric Autodiscovery
2011-04-04T12:40:16.015984-05:00coleiferhttps://djangosnippets.org/snippets/2404/<p>Admin-like autodiscover for your apps.</p>
<p>I have copy/pasted this code too many times...Dynamically autodiscover a particular module_name in a django project's INSTALLED_APPS directories,
a-la django admin's autodiscover() method.</p>
Freely redistributableQuery count decorator for TestCase
2010-10-04T14:01:18.671390-05:00coleiferhttps://djangosnippets.org/snippets/2219/<p>Sometimes you may want to check how many queries a specific set of operations is taking. This TestCase decorator allows you to do that.
</p>
Freely redistributablerender_to_response decorator
2010-09-15T15:15:44.762437-05:00coleiferhttps://djangosnippets.org/snippets/2195/<p>I'll throw my implementation into the mix - shortcut for render_to_response as a decorator.
</p>
Freely redistributableIgnore Csrf Middleware
2010-06-17T17:08:34.540109-05:00coleiferhttps://djangosnippets.org/snippets/2069/<p>Add to your middleware classes before 'django.middleware.csrf.CsrfViewMiddleware'.</p>
Freely redistributableGenerating aggregate data across generic relations
2010-05-22T20:34:54.695033-05:00coleiferhttps://djangosnippets.org/snippets/2034/<p>http://github.com/coleifer/django-generic-aggregation
</p>
<p>http://charlesleifer.com/blog/generating-aggregate-data-across-generic-relations/
</p>
<p>Generate and calculate aggregations across generic foreign keys.
</p>
<pre><code>>>> from misc import generic_annotate, generic_aggregate
>>> from blog.models import Entry
>>> from tagging.models import TaggedItem
>>> from django.db.models import Count
>>> qs = generic_annotate(Entry.objects.all(), TaggedItem.object, 'id', Count)
>>> qs[0].score
5L
>>> qs[1].score
4L
>>> qs[1].tags
u'databases django many-to-many python' …</code></pre>
Freely redistributableCall a manager method on any model with a filter
2010-05-13T19:08:42.965967-05:00coleiferhttps://djangosnippets.org/snippets/2019/<p>Often I want to call a custom manager method in the template, something like Snippet.objects.get_latest(). I hate writing custom templatetags to do all that work, so instead I've written a filter that will work for any. Here's how I use it:</p>
<pre><code>{% for snippet in "cab.snippet"|call_manager:"top_rated"|slice:":5" %}
</code></pre>
Freely redistributableUse memcached to throttle POSTs
2010-05-08T19:18:39.514983-05:00coleiferhttps://djangosnippets.org/snippets/2017/<h3>Problem: you want to limit posts to a view</h3>
<p>This can be accomplished with a view decorator that stores hits by IP in memcached, incrementing the cached value and returning 403's when the cached value exceeds a certain threshold for a given IP.</p>
Freely redistributableLatest instances template filter
2010-05-08T19:17:09.577735-05:00coleiferhttps://djangosnippets.org/snippets/2016/<p>Writing templatetags is obnoxious. Say you have a small blurb on all your pages that shows the latest 5 comments posted to the site -- using this filter, you could write the following:</p>
<pre><code>{% for comment in "comments.comment"|latest:5 %}
...display comment here...
{% endfor %}
</code></pre>
Freely redistributableCached template filters
2010-05-08T19:16:08.961093-05:00coleiferhttps://djangosnippets.org/snippets/2015/<p>Say you'd like to cache one of your template filters. This decorator acts sort of like memoize, caching a result set based on the arguments passed in (which are used as the cache key).</p>
Freely redistributableassertQuerysetEqual
2010-05-07T23:26:59.499097-05:00coleiferhttps://djangosnippets.org/snippets/2013/<p>I often find myself testing that the queryset returned by a method contains the instances I expect. I use a custom method, <strong>assertQuerysetEqual()</strong>, to test the equality of two querysets or lists::</p>
<pre><code>def test_some_values(self):
qs = get_user_list()
self.assertQuerysetEqual(qs, [normal_user, super_user])
</code></pre>
<p>Makes it easy to test small querysets against lists …</p>
Freely redistributable