Login

All snippets written in Python

Snippet List

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

Model Forms: Clean unique field

Often I want fields in my models to be unique - Django's `unique` works too late in model form validation and will throw an exception unless you check for it by hand. This is a bit of code that cleans up the boiler plate of checking if different fields are unique. Set `exclude_initial` to `False` if you want to raise an error if the unique field cannot be set to whatever value the instance had before. **Update** Thanks fnl for rightly pointing out that Django's `unique=True` does check for this - just make sure to pass `instance=obj` when you're initializing your forms. _HOWEVER_ a problem you'll typically run into with this is though you want a field to unique, you also want multiple entries to be able to be `blank`. This can help you!

  • forms
  • model
  • unique
  • model-forms
Read More

S3 static media uploader

This is a bastardisation of a few of the Amazon s3 file uploader scripts that are around on the web. It's using Boto, but it's pretty easy to use the Amazon supplied S3 library they have for download at [their site](http://developer.amazonwebservices.com/connect/entry.jspa?externalID=134). It's mostly based on [this](http://www.holovaty.com/blog/archive/2006/04/07/0927) and [this](http://www.davidcramer.net/code/112/writing-a-build-bot.html). It's fairly limited in what it does (i didn't bother os.walking the directory structure), but I use it to quickly upload updated css or javascript. I'm sure it's a mess code wise, but it does the job. This will first YUI compress the files, and then gzip them before uploading to s3. Hopefully someone might find this useful. It will also retain the path structure of the files in your MEDIA_ROOT directory. To use it, set up your Amazon details, download the [YUI Compressor](http://developer.yahoo.com/yui/compressor/), and then enter the folder you wish to upload to s3, and basically run the script - python /path/to/s3_uploader.py

  • s3
  • amazon
Read More

Base class for RESTful Views

Subclass `Resource` to create a view that will dispatch based on the HTTP method of the request. class View(Request): def DELETE(self, request): ... def GET(self, request): ... def PUT(self, request): ... Other snippets provided inspiration: * [436](http://www.djangosnippets.org/snippets/436/) * [437](http://www.djangosnippets.org/snippets/437/) * [1071](http://www.djangosnippets.org/snippets/1071/) * [1072](http://www.djangosnippets.org/snippets/1072/) The code is also available on [GitHub](http://github.com/jpwatts/django-restviews/).

  • views
  • rest
  • http
  • view
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

FlatPageOverrideMiddleware

This snippet is a piece of middleware that can be used to allow an administrator to create a flatpage that will override an existing URL. Why would you want such a thing? Maybe an admin needs to be able to override individual detail pages of an item list at will and doesn't want to be bothered mucking around with urlconf and restarting apache2. Obviously, it's not the ideal situation for permanent overrides (in that case, you'd be better off writing an exception in the urlconf), but it's good for the temporary, one-off, situations that may arise for whatever reasons.

  • flatpage
  • override
Read More

static tag

Django template tag for easy static files linking into your html. See docstring for documentation.

  • static
Read More

Require Login Middleware

Wraps specified URL patterns with login_required decorator. Allows you to quickly require login for an area of your site based only on a URL pattern. Similar to [zbyte64's snippet](http://www.djangosnippets.org/snippets/966/)

  • middleware
  • authentication
  • url
  • login
  • auth
Read More

Permission Required Middleware

Wraps specified URL patterns with permission_required decorator. Allows you to quickly require a specific permission for an area of your site based only on a URL pattern. Assumes a passing knowledge of how Django permissions work and how to use them. See [User authentication in Django](http://docs.djangoproject.com/en/dev/topics/auth/#permissions) for more information.

  • middleware
  • permissions
Read More

New Pattern

Now you can use object-oriented URL resolve and include and you can add function cp_before wich will be running before any any cp__* functions in his module

  • url
  • resolving
  • object-oriented
  • newpattern
  • pattern
Read More

Restructuredtext directive for photolouge

Directive for inserting images using photolouge in django. Usage: Just make a .py with this code and import it in some apps models.py Or make a custom formatter with django-template-utils.

  • image
  • insert
  • rst
  • directive
  • photolouge
Read More

Preferred Domain decorator function.

From time to time I often have to work with a site that has two domain names. This can be an issue when dealing with mapping api keys so to solve this problem I whipped up a decorator function that allows a preferred domain to be enforced on a per view basis. It wouldn't be too much to take this onto a middleware, but for my usage I just wanted a per view granularity and a decorator function suited my needs quite nicely.

  • decorator
  • domain
  • preferred-domain
Read More

Pygments Syntax highlighting template tag

This template tag will attempt to apply pygments syntax highlighting to anything inside {% code %} and {% endcode %} tags. You can optionally pass in the language to highlight in the form of {% code 'lang' %} - if you don't, it will first try to guess the syntax, and then fall back to Python syntax highlighting.

  • templatetag
  • pygments
  • highlighting
  • syntax
Read More

Safer cache key generation

If any cache keys you generate include user (staff or public) supplied data, they may: be too long, contain invalid characters (at least by memcache's standards), or both. This helper will sub out any invalid characters and md5 the key if it's too long. Additionally, it'll insert the CACHE_MIDDLEWARE_KEY_PREFIX from django.conf.settings for you. In your memcache instances are used by multiple applications (Django or otherwise) this can help ensure your keys are unique to that a particular app on a particular site.

  • memcache
  • cache
  • caching
Read More

2955 snippets posted so far.