djangosnippets.org: Latest snippets posted by jdunckhttp://djangosnippets.org/users/jdunck/2012-01-24T13:09:16.512298-06:00jdunckmake a combined set of db.Q objects out of a list of dicts and an operator
2012-01-24T13:09:16.512298-06:00jdunckhttp://djangosnippets.org/snippets/2660/<p>It's often useful to dynamically create filter criteria, and Q objects are useful for that, but sometimes you need to make a combined Q composed of various alternates. This bit of code eases the awkwardness of creating the first Q so that there's a combiner, plus the odd case of …</p>
Freely redistributableSee what depends on a given model
2011-02-07T10:56:05.038458-06:00jdunckhttp://djangosnippets.org/snippets/2351/<p>Riffing on http://djangosnippets.org/snippets/1024/ I didn't want a graph; I just wanted to see what depended on a specific model.
</p>
<p>This does that nicely.
</p>
Freely redistributableSecurity: Sideband information cover traffic middleware
2010-03-25T09:20:08.232559-05:00jdunckhttp://djangosnippets.org/snippets/1970/<p>This is a quick hack to address the SSL info leakage covered here:
http://www.freedom-to-tinker.com/blog/felten/side-channel-leaks-web-applications</p>
<p>Don't use this in prod without testing. :-)</p>
<p>I'll get some feedback from django-dev and update here.</p>
Freely redistributableSupport IP ranges in INTERNAL_IPS
2010-01-09T18:28:11.151972-06:00jdunckhttp://djangosnippets.org/snippets/1862/<p>CIDR ( http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing ) is a well-known IP range syntax. This CIDR_LIST class can be used to make ranges of IPs considered "internal" for Django's debugging and security purposes. (Django only ever needs to do "ip in INTERNAL_IPS" so <strong>contains</strong> is sufficient for the purpose.)
</p>
<p>For example, to make localhost …</p>
Freely redistributableWrappable text
2009-10-02T21:34:09.990633-05:00jdunckhttp://djangosnippets.org/snippets/1751/<p>Sometimes you have an uncontrolled amount of text in a horizontally constrained space.</p>
<p>The wrappable filter places zero-width breaking spaces into the given text so that it can wrap at any point, as necessary for the containing width. Sometimes better than eliding (chopping long text...) or cropping/scrolling overflow.</p>
Freely redistributableTechnical 500 by group membership
2009-09-07T20:32:51.504710-05:00jdunckhttp://djangosnippets.org/snippets/1719/<p>Based loosely on <a href="http://ericholscher.com/blog/2009/sep/5/debugging-django-production-revisited/">Eric's middleware</a>, this middleware will show the technical 500 page (which you'd get if DEBUG == True) to any user who is (1) superuser and (2) a member of the settings.TECHNICAL_500_GROUP_NAME group. (If no setting exists, 'Technical Errors' is the presumed group name.
</p>
<p>I agreed with …</p>
Freely redistributableiter_fetchmany
2009-01-08T02:13:06.075286-06:00jdunckhttp://djangosnippets.org/snippets/1280/<p>When executing custom sql, the temptation is to use fetchall or fetchone, since the API for fetchmany is a bit awkward. (fetchall makes all records resident in client memory at once; fetchone takes a network round-trip to the DB for each record.)</p>
<p>This snippet, hoisted from django.db.models.sql.query.results_iter, presents a nice, …</p>
Freely redistributableUser from session key
2009-01-05T07:49:42.405510-06:00jdunckhttp://djangosnippets.org/snippets/1276/<p>This blog post outlined how to get the user from the session key:
http://scottbarnham.com/blog/2008/12/04/get-user-from-session-key-in-django/</p>
<p>Unfortunately, it assumes DB-backed session and auth backends. This isn't required, so this snippet provides a backend-agnostic way to do the same thing.</p>
<pre><code>>>> skey = 'ea0ed02d35d43aeaf20b3ef516f51396'
>>> user_from_session_key(skey)
<User: jeremyd>
</code></pre>
Freely redistributable