Login

All snippets written in Python

2956 snippets

Snippet List

simple app using RestViews

Inspired by [Eric Florenzano's](http://www.eflorenzano.com/) post about [writing simple and very fast pure-WSGI applications](http://www.eflorenzano.com/blog/post/writing-blazing-fast-infinitely-scalable-pure-wsgi/). Using [RestView](http://www.djangosnippets.org/snippets/1071/) approach. More about it on [my blog](http://my.opera.com/kubiku/blog/2009/01/16/bare-wsgi-vs-python-frameworks-django-chapter)

  • rest
Read More

Email Munger

Template filter to hide an email address away from any sort of email harvester type web scrapers and so keep away from spam etc. The filter should be applied on a string which represents an email address. You can optionally give the filter a parameter which will represent the name of the resulting email href link. If no extra parameter is given the email address will be used as the href text. {{ email|mungify:"contact me" }} or {{ email|mungify }} The output is javascript which will write out the email href link in a way so as to not actually show the email address in the source code as plain text. Also posted on [my site](http://www.tomcoote.co.uk/DjangoEmailMunger.aspx).

  • filter
  • email
Read More

iter_fetchmany

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.) This snippet, hoisted from django.db.models.sql.query.results_iter, presents a nice, simple iterator over multiple fetchmany calls which hits a sweet spot of minimizing memory and network usage.

  • sql
  • db
  • db-api
  • fetchmany
Read More

ifinlist template tag

I recently had to write a custom template tag which checks to see if a value exists in a list variable from within a Django template. This was my first ever attempt at writing a template tag so any feedback would be appreciated.

  • template-tag
  • if-in-list
  • if-value-in-list
Read More

Play nice with ModelAdmin mixins

There are several nice ModelAdmin subclasses that provide useful functionality (such as django-batchadmin, django-reversion, and others), but unfortunately a ModelAdmin can really only subclass one at a time, making them mutually exclusive. This snippet aims to make mixing these classes in as easy as possible -- you can inherit your model admin from it, add a tuple of mixins, and it will dynamically change the inheritance tree to match. This isn't guaranteed to work with all ModelAdmins, but so long as the mixins play nice with django modeladmin they *should* work.

  • mixin
  • modeladmin
Read More

Media Wiki With mwlib

### Simple wiki with MediaWiki and Markdown Support Once you install the mwlib you can use mwit to convert Mediawiki markup to HTML. I am include the model that uses it to hopefully provide a good example. I maintain a version and only one copy of each wiki entry in the main table and archive replaced markup into another table, you will need to create the archive model or remove that section of code. The line ending changes in mwit are so that it will work with IE.

  • markup
  • markdown
  • mediawiki
  • wiki
  • archive
Read More

Image resize on demand

** Image on demand view ** I often post photos on photography fora. Most fora want you to place a link to a photo somewhere on the net, but different fora have different rules. Some fora want you to stick to a maximum of 800 pixels wide, some 700 pixel and some even strange values like 639 pixels. My own site uses 600 pixels so I end up resizing images all the time. Since I keep my originals with my gallery as well (hidden for public viewing) resizing on the fly would be a nice asset. I'm using my previous snippet to apply a slight unsharp mask for better web display of my photos. ** usage ** This snippet takes the url to my photo application which is a simple link using the pk of my photo table and adds 'width'.jpg to the end (some fora check if the link is an image based on extenstion) The view takes the width requested and creates the resized image from the original full size image or takes it from the cache for display on demand. To prevent a dozen directories I use a setting to specify which widths are allowed, providing room for several versions of the same image. Any improvements are appreciated since I'm still rather inexperienced in Python and Django.

  • image
  • pil
  • resize
  • pythonmagick
Read More

Links with XFN (and Validation)

Slightly different validation to that of jpwatts; developed with knowledge of that snippet but written up from scratch for use as a fallback for when the Javascript Assist is off. I'm no master programmer, so feedback/comments/criticism is more than welcome. The accompanying Javascript (xfn-admin.js) can be found here: [http://www.djangosnippets.org/snippets/1266/](http://www.djangosnippets.org/snippets/1266/).

  • xfn
  • microformats
Read More

Django Akismet

http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/

  • akismet
Read More

(Modified/Improved) MultiQuerySet

My modified version of the [MultiQuerySet by mattdw](http://www.djangosnippets.org/snippets/1103/) (see the link for further information). My purpose for this was to enable me to combine multiple different types of querysets together, which could then be iterated on as one object (i.e. like a tumblelog).

  • multiple
  • queryset
  • chain
Read More

Text Highlighting Filter

This filter can be used to wrap <span class='highlight'> around anything you want to highlight in a block of text. For example, if you had 'foo bar foo baz' inside the context variable MYTEXT, you could do {{ MYTEXT|highlight:'foo' }}, and "<span class='highlight'>foo</span> bar <span class='highlight'>foo</span> baz" would be returned. How you style the highlight class is up to you.

  • filter
  • text
  • highlight
Read More

Clear nullable foreign keys on delete

Django 1.0 is apparently hard-coded for cascading deletes. I find that I often have nullable foreign keys on models whose records must not be deleted along with those they refer to. I override Model.delete() in an intermediate base class and execute this method to clear out all nullable foreign keys before allowing a delete to proceed.

  • model
  • delete
  • cascade
Read More

Dynamic image generator

This is the complete image_processor.py module, allowing you to add an image containing an arbitrary piece of text. I use this to label the horizontal axis of a skills-matrix report. Credit www.renewtek.com for paying me to do this stuff!

  • image
  • pil
Read More

Convert a Couchdb document into a Python object

Originally posted by [Leah Culver on her blog](http://leahculver.com/2008/11/25/couchdb-documents-python-objects/). This allows you to convert a CouchDB document into a python object and work on CouchDB fields as if they were properties of a python object.

  • python
  • couchdb
Read More