Login

All snippets

Snippet List

Image Standarization

Standarization of image fields (for being used when saving models). Automatically creates thumbnail. Change image name to /path/to/images/<my_field>-<id>.<ext>. Resize image and thumbnail to specified size (optionally can crop image to force size).

  • image
  • thumbnail
  • resize
  • rename
Read More

Google Contacts API friend finder

This view will provide a link for AuthSub proxy authentication to the Google Contacts API and then grab the user's Google contacts and compare against User.email for possible user relationships. A couple of things to note: * Dependency on the GData Python client library: http://code.google.com/p/gdata-python-client/ * The domain hosting the view must be verified by Google's domain manager (otherwise API calls will result in a 403): https://www.google.com/accounts/ManageDomains Thanks to Simon for get_url_host and get_full_url.

  • api
  • gmail
  • google
  • gdata
  • contacts
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

Remove self links middleware

This simple middleware replaces all 'a href' links to the current page to the 'span' elements. This very usefule from the usability point of view. For example, user open in bowser page http://svetlyak.ru/blog/, and this middleware will replace all 'a' elements on this page, which refer to the '/blog/'. Because of this, link 'Blog' in the main menu, become a simple 'span'. Next, when user goes to the next page, a post with full comments list ('/blog/123/'), for example, the item 'Blog' in the main menu become a link again! To use this middleware, just add it to the list of middleware classes: MIDDLEWARE_CLASSES = ('utils.middleware.RemoveSelfLinks',)

  • middleware
  • html
  • output
  • usability
  • utils
Read More

Multiple inheritance of newforms and modelforms

If you try to use multiple inheritance with a modelform (to mix in some fields from an already existing form class for example) you'll get the following rather terrifying error: > "Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases" The solution is to first create the ModelForm, then create a NEW class that inherits from both the ModelForm and the form you want to mixin, then finally apply the recipe from here: [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204197)

  • newforms
  • inheritance
  • modelforms
  • multipleinheritance
  • metaclasses
  • metaclass
Read More

sharedance sessions backend

"Sharedance is a high-performance server that centralize ephemeral key/data pairs on remote hosts, without the overhead and the complexity of an SQL database." [Frank DENIS](http://sharedance.pureftpd.org/project/sharedance)

  • session
  • backend
Read More
Author: d2
  • 0
  • 0

Preview tag for fields with choices

django.contrib.formtools in preview displaying only field.data by default. Its not convenient to see integer value for fields with radio buttons or select choices. This custom tag trying to show string value from choices if available.

  • template
  • tag
  • newforms
  • preview
Read More

Auto HTML Linebreak filter

This custom filter is helpful if you want to convert plain text to include html line breaks but you aren't sure if the text is actually plain text or if it already contains html line breaks. First the filter looks for if the text contains any br, p, or table tags, if it does the text is returned as is. If it doesn't then the same functionality as the [linebreaks](http://www.djangoproject.com/documentation/templates/#linebreaks) filter is applied to the text.

  • filter
  • html
  • linebreaks
  • custom-filter
Read More

In page edit object links

This javascript has the functionality of the 'Edit Object' bookmarklet as [described by James Bennett](http://www.b-list.org/weblog/2007/nov/07/bookmarklets/) but instead of putting it within a bookmarklet it creates a div on the page with a link to the main admin screen, an edit the object link, and logout link. Just include the javascript on any page you want the links to show up on and make sure that the page is using the populate_xheaders function to provide the needed headers as [described here](http://www.b-list.org/weblog/2007/nov/07/bookmarklets/). The built in generic views provide the needed headers by default.

  • admin
  • edit-object-links
Read More

Update ContentTypes and Permissions without syncdb

[See blog post](http://paltman.com/2008/04/11/keeping-contenttypes-and-permissions-updated-without-syncdb/) You can put this script in the root of your project and run after deploying updates in your production environment.

  • sql
  • permissions
  • deploy
  • contenttypes
Read More

XhtmlMortifierMiddleware

This middleware checks for xhtml mimetypes if the browser supports a "application/xhtml+xml" response. If not, it converts the response headers to "text/html". To enable this middleware add it the the MIDDLEWARE_CLASSES setting and make sure it appears somewhere after GZipMiddleware, so that it's processed first.

  • middleware
  • xhtml
  • user-agent
Read More

Customizable newforms labels with a template tag

This template tag and suggested template allow greater control over rendering `<label>` tags for your newforms fields than using `field.label_tag`. I save the provided Python code in my app as `templatetags/forms.py` (although this name may conflict in the future). The simplest usage: {% label field %} One use case is adding `class="required"` to the label tag for required fields instead of inserting markup elsewhere--this is done in the given example template. Alternate label text and tag attributes can be passed to the inclusion tag: {% label field "Alt. label" 'class=one,id=mylabel' %}

  • templatetag
  • newforms
  • label
Read More

3109 snippets posted so far.