Login

3110 snippets

Snippet List

Error rate limiter

Prevents error flooding on high traffic production sites. Particularly useful to prevent clogging email servers etc. See [discussion](http://groups.google.com/group/django-developers/browse_thread/thread/1dabc9139dd750ca/1d86fdca23189a7d?lnk=gst&q=error#1d86fdca23189a7d) and [closed ticket](http://code.djangoproject.com/ticket/11565). Uses memcache if it can, or falls back to local, in-process memory where unavailable (down, etc). Tweak to your needs using setting ERROR_RATE_LIMIT (seconds). Requires Django 1.3+ or trunk r13981+

  • error
  • logging
  • emails
Read More
Author: s29
  • 2
  • 3

slugify with transliteration

This slugify correctly transliterates special characters using the translitcodec package from PyPI. Make sure you've installed http://pypi.python.org/pypi/translitcodec/ before using this.

  • slug
  • slugify
  • special chars
  • trans
  • transliteration
  • umlauts
Read More

Remove named field from fieldsets

This snipped removes a specific fields from the fieldsets. This is very useful to leave a field 'out' in the admin, likewise: def get_fieldsets(self, request, obj=None): fieldsets = super(BlaModelAdmin, self).get_fieldsets(request, obj) if not request.user.has_perm('change_blah'): remove_from_fieldsets(fieldsets, ('blah',))

  • admin
  • fieldset
  • form
  • field
  • fieldsets
Read More

Declaring django views like web.py views

I work a little with [web.py framework](http://webpy.org/) and I like a lot the view definition. For each view you define a class and in that class you can define two method, GET and POST. If the http request is a GET request the GET method will be called and if http request is a POST request the POST method is called. Then you can define common stuff in another method that could be called inside each method, and you have a class for each view.

  • views
  • class
  • web.py
Read More

send a simple Campfire chat message from fabric script

look ma, no api! a python method for [fabric](fabfile.org) script to send a message to your [campfire](campfirenow.com) chat room. not really a django script but I didn't know where else to put it. I use it to send a deployment messages to campfire when we deploy new revisions. like the comment mentions, put your api key in ~/.fabricrc. the example api key is garbage so don't waste your time.

  • basecamp
  • campfire
  • chat
  • fabric
Read More

models with order (+admin editing)

Use this abstract model if you want to add "order" to a given model. Once you do, you will get automatic "up" and "down" links for each model row. One problem is that if the user sorts by another row, the up and down links are still there, but now don't make a lot of sense.

  • models
  • admin
  • order
Read More

Print Exceptions to the Console

Put this in an __init__.py somewhere that will be executed on initialization and all errors will be printed out to stderr. Useful for debugging Facebook apps, javascript calls, etc.

  • print
  • console
  • exception
  • signal
Read More

Use class name in templates

For use in templates: {% if object|classname:"entry" %} ... class="{{ object|classname }}" ... I couldn't find simpler solution for checking weather specific object belongs to specific class name, or just to output object's class name.

  • filter
  • classname
Read More

LogTrace

This small decorator will trace the execution of your code every time it enters or exits a decorated function (by thread) and will insert appropriate indent into the log file along with exception information.

  • decorator
  • logging
  • hax
  • trace
Read More

Image inlining template tag lib

A custom templatetag for inlining image in the browser. The principe is to base64 encode the image and avoid a http request. There is a cache handling, you just have to specify a writable directory. An example of the utilisation (template part): [http://djangosnippets.org/snippets/2267/](http://djangosnippets.org/snippets/2267/) The explication on [http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/](http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/)

  • tag
  • image
  • templatetag
  • base64
Read More

Django Admin Replacer Code

Ok let's descrive what i have done I subclassed the django admin to create a form that makes you choose if activate a delete and replace login inside your admin. Then i have added a form with a modelChoiceField to make you select another model instance when you are selecting an istance to delete. If you select another instance the current instance will be replaced.

  • admin
  • object
  • delete
  • modeladmin
  • replacer
Read More

widget to capture a geographic Point

The class LocationField renders a form field with map (from google maps) and a mark. It allows the user to drag the mark to point at some particular location, whose value (lat, lng) is saved in a hidden field. It requires jquery and google maps.

  • newforms
  • forms
  • gis
  • google
  • field
  • maps
  • widget
Read More

Test runner with coverage

A replacement test runner which outputs a coverage report after the tests. Simply change your ``TEST_RUNNER`` setting to point to ``run_tests_with_coverage`` and you're good to go. Note that 'as-is' this snippet reports the coverage of all modules underneath the app, by walking the directory tree and loading all of the .py modules (this may be a naive approach). If you change it to use `get_coverage_modules()` instead, it will only display the coverage of modules that have been imported by the test suite, using the Python `inspect` lib, which may be more reliable. Uses [coverage.py](http://nedbatchelder.com/code/modules/coverage.html). Based on ideas from: [1](http://www.thoughtspark.org/node/6), [2](http://blogs.23.nu/c0re/stories/15428/) and [3](http://siddhi.blogspot.com/2007/04/code-coverage-for-your-django-code.html)

  • test
  • runner
  • coverage
Read More