djangosnippets.org: Latest snippets posted by jezdezhttps://djangosnippets.org/users/jezdez/2012-08-20T08:57:44.305316-05:00jezdezSyntax highlighting for tracebacks in console output
2012-08-20T08:57:44.305316-05:00jezdezhttps://djangosnippets.org/snippets/2805/<p>This is hardcoded to use <a href="http://pypi.python.org/pypi/django-discover-runner">django-discover-runner</a> since that's my main test runner but could easily be adopted to use Django's own test runner. If you're using a terminal that is capable of showing 256 colors use the <code>Terminal256Formatter</code> formatter instead.</p>
<p>Enabled it with the <code>TEST_RUNNER</code> setting:</p>
<pre><code>TEST_RUNNER = 'dotted.path.to.highlighted.runner.HighlightedDiscoverRunner'
</code></pre>
<p>Where …</p>
Freely redistributableCustom requests auth class for Tastypie API key authentication
2012-03-30T05:52:25.098864-05:00jezdezhttps://djangosnippets.org/snippets/2727/<p>In case you ever use <a href="http://python-requests.org/">requests</a> (or <a href="http://slumber.in/">slumber</a>) to do requests against a Tastypie API that requires API key authentication, this small custom auth class will help you.</p>
<p>Use it like that (with requests):</p>
<pre><code>auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252')
response = requests.get('http://api.foo.bar/v1/spam/', auth=auth)
</code></pre>
<p>or with slumber:</p>
<pre><code>auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252') …</code></pre>
Freely redistributableMixin for named URL WizardViews with one form on the frontpage
2012-03-05T16:58:46.854847-06:00jezdezhttps://djangosnippets.org/snippets/2709/<p>A Django 1.4 wizard mixin for use cases with a wizard step on the frontpage
of your site -- with a request path of <code>'/'</code>.</p>
<p>Just define the name of the step (e.g. <code>root_step = 'landing_page'</code>)
and it does the setup and redirection automatically.</p>
Freely redistributableDecorator and context manager to override settings
2011-05-17T10:02:14.118453-05:00jezdezhttps://djangosnippets.org/snippets/2437/<h2>Overriding settings</h2>
<p>For testing purposes it's often useful to change a setting temporarily and revert to the original value after running the testing code. The following code doubles as a context manager and decorator. It's used like this:</p>
<pre><code>from django.test import TestCase
from whatever import override_settings
class LoginTestCase(TestCase):
@override_settings(LOGIN_URL='/other/login/')
def …</code></pre>
Freely redistributableslugify with transliteration
2010-11-26T05:05:13.164923-06:00jezdezhttps://djangosnippets.org/snippets/2274/<p>This slugify correctly transliterates special characters using the translitcodec package from PyPI.
</p>
<p>Make sure you've installed http://pypi.python.org/pypi/translitcodec/ before using this.
</p>
Freely redistributableMiddleware to prevent access to the admin when user ip not in INTERNAL_IPS
2010-07-05T09:19:18.483535-05:00jezdezhttps://djangosnippets.org/snippets/2095/<p>This middleware will prevent access to the admin if the users IP isn't in the INTERNAL_IPS setting, by comparing the request path with the reversed index URL of the default admin site, ultimately raising a 404 (unless DEBUG = True).</p>
Freely redistributableSiteRedirectMiddleware
2008-08-18T05:02:38.397791-05:00jezdezhttps://djangosnippets.org/snippets/989/<p>Redirects to the default site (from Django's Sites contrib app), specified by the <code>SITE_ID</code> setting.</p>
<p>That's for example useful if you configured your webserver to handle multiple domains with the same virtual host and want to make sure every requests is then redirected to the right domain.</p>
Freely redistributableRandomFileExtensionMiddleware
2008-06-02T04:25:42.974362-05:00jezdezhttps://djangosnippets.org/snippets/781/<p>If you feel nostalgic for the old days of crufty URLs, put this middleware somewhere in your Django app and add the first entry in settings.MIDDLEWARE_CLASSES as shown below. Keep in mind that you need to replace 'path.to' with the correct Python path to the middleware.</p>
<pre><code>MIDDLEWARE_CLASSES = (
'path.to.RandomFileExtensionMiddleware', …</code></pre>
Freely redistributableugettext tag
2008-05-28T17:09:31.609918-05:00jezdezhttps://djangosnippets.org/snippets/776/<p>This translates a given message with ugettext. That's it.</p>
<p><code>{% load where_you_have_it %}</code>
<code>{% ugettext "German" %}</code></p>
Freely redistributableAutomagically import settings from installed applications
2008-01-31T08:03:06.767993-06:00jezdezhttps://djangosnippets.org/snippets/573/<p>Use this snippet at the end of your main settings.py file to automagically import the settings defined in each app of <code>INSTALLED_APPS</code> that begins with <code>APPS_BASE_NAME</code>.</p>
<p>Set <code>APPS_BASE_NAME</code> to the base name of your Django project (e.g. the parent directory) and put <code>settings.py</code> files in every app directory (next …</p>
Freely redistributable"for" template tag with support for "else" if array is empty
2008-01-15T10:04:21.178120-06:00jezdezhttps://djangosnippets.org/snippets/546/<p>This is a customized version of the default <code>for</code> template tag which takes an optional <code>{% else %}</code> clause that will be displayed if the given array is empty.</p>
<pre><code>from django.template import *
>>> t1 = Template("""
{% load mytags %}
{% for athlete in athlete_list %}
{{ athlete }} …</code></pre>
Freely redistributableSeparatedValuesField
2007-12-05T07:28:00.905678-06:00jezdezhttps://djangosnippets.org/snippets/497/<p>A Django newforms field which takes another newforms field during
initialization and validates every item in a comma-separated list with
this field class. Please use it like this:</p>
<pre><code>from django.newforms import EmailField
emails = CommaSeparatedValuesField(EmailField)
</code></pre>
<p>You would be able to enter a string like "john@doe.com,guido@python.org"
because every email address would …</p>
Freely redistributable