djangosnippets.org: Latest snippets posted by akaiholahttps://djangosnippets.org/users/akaihola/2012-01-31T02:21:00-06:00akaiholaDump a model instance and related objects as a Python data structure2012-01-31T02:21:00-06:00akaiholahttps://djangosnippets.org/snippets/2670/<p>This utility makes a text dump of a model instance, including objects related by a forward or reverse foreign key. The result is a hierarchical data structure where</p>
<ul>
<li>
<p>each instance is represented as a list of fields,</p>
</li>
<li>
<p>each field as a (&lt;name&gt;, &lt;value&gt;) tuple,</p>
</li>
<li>
<p>each &lt;value&gt; as a primitive type ...</p></li></ul>
Freely redistributableValidation for full e-mails (e.g. &quot;Joe Hacker &lt;joe@hacker.com&gt;&quot;)2011-12-21T02:43:32-06:00akaiholahttps://djangosnippets.org/snippets/2635/<p>Out of the box, Django e-mail fields for both database models and forms only accept plain e-mail addresses. For example, <code>joe@hacker.com</code> is accepted.</p>
<p>On the other hand, full e-mail addresses which include a human-readable name, for example the following address fails validation in Django:</p>
<pre><code>Joe Hacker &lt;joe@hacker ...</code></pre>
Freely redistributableNon-pickling locmem (in-process memory) cache backend2011-03-18T02:52:10-05:00akaiholahttps://djangosnippets.org/snippets/2396/<p>You can use this cache backend to cache data in-process and avoid the overhead of pickling. Make absolutely sure you don't modify any data you've stored to or retrieved from the cache. Make deep copies instead if necessary.</p>
<p>The backend is basically identical to Django's stock locmem ...</p>
Freely redistributableFinding related objects for instances in a queryset2011-03-08T05:47:58-06:00akaiholahttps://djangosnippets.org/snippets/2385/<p>When deleting objects in Django's admin interface, it lists other objects which would be deleted and asks for confirmation. This snippet does the same programmatically.</p>
<p>The snippet works in Django 1.3 (more specifically, revision 14507 or later). It uses Django internals which are not a part of the ...</p>
Freely redistributablechange settings locally in an individual test2010-08-19T04:41:55-05:00akaiholahttps://djangosnippets.org/snippets/2156/<p>So you need to change some settings when running an individual test in a test case. You could just wrap the test between <code>old_value = settings.MY_SETTING</code> and <code>settings.MY_SETTING = old_value</code>. This snippet provides a helper which makes this a bit more convenient, since settings are restored to their old values ...</p>
Freely redistributableYUI Loader as Django middleware2008-04-21T00:57:51-05:00akaiholahttps://djangosnippets.org/snippets/712/<p>This server-side middleware implements some of the functionality in the Yahoo
User Interface Loader component. YUI JavaScript and CSS modules requirements
can be declared anywhere in the base, inherited or included templates, and the
resulting, optimized <code>&lt;script&gt;</code> and <code>&lt;link rel="stylesheet"&gt;</code> tags are inserted at
the specified position of ...</p>
Freely redistributableWho&#39;s helping a lot in IRC2008-04-19T06:10:05-05:00akaiholahttps://djangosnippets.org/snippets/710/<p>We had some fun today on the #django IRC channel searching and counting through past logs for people saying "thanks" to <a href="http://djangopeople.net/magus/">a known very helpful person</a>.</p>
<p>Here's a unix shell script for checking your own score if you're using Pidgin and have logging turned on.</p>
<p>Replace ".purple" with ...</p>
Freely redistributableSmart append slash middleware2008-02-15T07:10:12-06:00akaiholahttps://djangosnippets.org/snippets/601/<p>This middleware replaces the behavior of the APPEND_SLASH setting in the CommonMiddleware. Please set <code>APPEND_SLASH = False</code> and <code>SMART_APPEND_SLASH = True</code> if you are going to use this middleware.</p>
<p>In your URL patterns, omit the trailing slash for URLs you want accessible without the slash. Include the slash for those URLs you ...</p>
Freely redistributableTemplate tag for stripping blank lines2008-01-29T14:30:23-06:00akaiholahttps://djangosnippets.org/snippets/569/<p>When writing clean and easy-to-read templates, it's usually good to have structural template tags (e.g. {%for%}, {%if%}) alone on their own line with proper indentation.</p>
<p>When such a template is rendered, the resulting HTML will have blank lines in place of the template tags. The leading blank space ...</p>
Freely redistributableStop tests at the first failure2008-01-03T06:28:37-06:00akaiholahttps://djangosnippets.org/snippets/534/<p><strong>Note</strong>: The <code>--failfast</code> argument in Django since version 1.2 does this. Use this snippet for earlier versions.
</p>
<p>If a large number of your unit tests get "out of sync", it's often annoying to scan through a large number of test failures which overflow the terminal window's scroll ...</p>
Freely redistributableUse crypt instead of sha1 as password hash algorithm2007-08-26T11:39:45-05:00akaiholahttps://djangosnippets.org/snippets/389/<p>This snippet uses signals to replace the <code>contrib.auth.models.User.set_password()</code> function with one that uses <em>crypt</em> instead of <em>sha1</em> to hash the password.</p>
<p><em>Crypt</em> is of course cryptographically inferior to <em>sha1</em>, but this may be useful for interoperability with legacy systems e.g. when sharing a user authentication ...</p>
Freely redistributableSave disk space by hard-linking multiple Django installations2007-04-12T13:02:18-05:00akaiholahttps://djangosnippets.org/snippets/175/<p>If you work with Django like I do, you have a separate installation or Subversion check-out of Django for each of your projects. Currently each one of them eats 22 MB of disk space.</p>
<p>This utility hard-links all identical files between copies of Django. They can even be different versions ...</p>
Freely redistributableSanitize text field HTML (here from the Dojo Toolkit Editor2 widget)2007-04-10T14:11:42-05:00akaiholahttps://djangosnippets.org/snippets/169/<p>When using a JavaScript WYSIWYG editor widget for text area content, the resulting HTML should be sanitized so no unallowed HTML tags (esp. script tags) are present.</p>
<p>The <a href="http://www.crummy.com/software/BeautifulSoup/">BeautifulSoup</a> library handles HTML processing in the solution presented above, so you should place it in the Python path.</p>
<p>The snippet also ...</p>
Freely redistributableDatabase migration and dump/load script2007-04-10T13:56:38-05:00akaiholahttps://djangosnippets.org/snippets/167/<p>I once needed to convert a Django project from PostgreSQL to SQLite. At that time I was either unaware of manage.py dumpdata/loaddata or it they didn't yet exist. I asked for advice on the #django IRC channel where ubernostrum came up with this plan:
</p>
<pre><code>simple process:
1 ...</code></pre>
Freely redistributable