Login

All snippets written in Python

Snippet List

Address model

This is standart address model. Must match US / GB / French address and other... Specifics locales fields are notice by comments.

  • model
  • address
Read More

truncatehtml_at_word

Much stolen from base `truncate_html_words`. The difference is that this filter takes a number of characters as its argument and truncates to the nearest word boundary less than that count, rather than specifying a number of words.

  • filter
  • html
Read More

Url persistance of GET variables

I'm using Django 0.96 for a project, and the url tag does not have all the capabilities I need. I want a way to persist all or some of the GET parameters between requests. I also want a way to add to the current url a list of extra parameters, or nullify certain parameters. In this snippet I am defining 2 tags ... link_persist and link_add. It only works with Django 0.96, as the API changed ... but maybe a good soul can fix it for 1.0, as I haven't had any time available. A tip for usage: if you specify a parameter as being the empty string, or None, the parameter will be removed from the link. When you specify a parameter already available in GET, it will replace it.

  • get
  • url
  • link
  • persistance
Read More

Securely Signed S3 Links With Expiration

I couldn't find a Python implementation of this, so I threw this class together real quick. This will let you share "private" files on S3 via a signed request. It will also have an expiration on the link, so it is only valid until a certain time period. Example Usage: s3 = SecureS3('AWS_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY') s3.get_auth_link('your_bucket', 'your_file') That would return your secure link. eg, http://your_bucket.s3.amazonaws.com/your_file?AWSAccessKeyId=AWS_ACCESS_KEY&Expires=1226198694&Signature=IC5ifWgiuOZ1IcWXRltHoETYP1A%3D

  • s3
  • secure
Read More
Author: pjs
  • 1
  • 4

CachedPaginator

A subclassed version of the standard Django Paginator (django.core.paginator.Paginator) that automatically caches pages as they are requested. Very useful if your object list is expensive to compute. MIT licensed.

  • cache
  • pagination
  • paginator
  • caching
Read More

Generate thumbnails on save

This is an override the save method of our Photo model. This new save method essentially takes the image, thumbnails it into our various sets of dimensions (for … in self.IMAGE_SIZES…), and save each one (into its own ImageField) before finally call the overwritten method to save the original image. Yes, the dimensions are hardcoded, and there is currently not a way to regenerate them in different sizes, but one shouldn't be that hard to come up with, because you just could just load each photo object to regenerate, then save it again (or something along those lines). mattpdx helped a lot with figuring out this code.

  • thumbnail
Read More

Textile Widget

A Textarea widget which appends basic Textile formating instructions in the same way Basecamp's Writboard product displays some basic helper markup alongside the edit area.

  • textile
  • widget
Read More

Batch querysets

Most of the time when I need to iterate over Whatever.objects.all() in a shell script, my machine promptly reminds me that sometimes even 4GB isn't enough memory to prevent swapping like a mad man, and bringing my laptop to a crawl. I've written 10 bazillion versions of this code. Never again. **Caveats** Note that you'll want to order the queryset, as ordering is not guaranteed by the database and you might end up iterating over some items twice, and some not at all. Also, if your database is being written to in between the time you start and finish your script, you might miss some items or process them twice.

  • queryset
  • batch
Read More

MarkupTextField

I updated [MarkdownTextField](http://www.djangosnippets.org/snippets/882/) to have some choices in markup. It currently support for Markdown, Textile, Plain Text, and Plain HTML. It will add `%s_html` for the complied HTML and `%s_markup_choices` for a drop down of markup choices. Usage: class MyModel(models.Model): description = MarkupTextField()

  • model
  • markup
  • markdown
  • textile
  • field
Read More

extends_default

Works exactly like the standard "extends" tag but enables one to fallback on a default template. This tag is LIMITED, as it falls back to the next template with the same name that DOES NOT contain "extends_default" (does NOT simulate full inheritance, just a single level). A partial solution to problems like http://jeffcroft.com/blog/2008/aug/05/default-templates-django/. MIT licensed.

  • template
  • templatetag
  • inheritance
  • default
Read More

Rails Style Controller

Just a quick hack for a Controller style pattern. Similar to the approach taken in django.contrib.admin views things this breaks: 1. {% url %} and reverse() 2. validation provided by regex urlpatterns

  • python
  • controller
  • rubyonrails
Read More

An alternative model serializer for django models

Django's serializer has some limitations which makes it a bit of a pain to use. Basically it will ignore any atributes that have been added to a model object. The code below is for an alternative serializer. This version allows you select what attributes will be serialized on a per object basis. It also allows you to either serialize the data into json or xml. The original json encoder was written by [Wolfram Kriesing](http://wolfram.kriesing.de/blog/) Example Usage: dumper = DataDumper() dumper.selectObjectFields('class_name',[...fields...]) dumper.selectObjectFields('class_name',[...fields...]) dumper.dump(model_instance,'xml') dumper.dump(model_instance,'json') dumper.dump(queryset,'xml')

  • json
  • xml
  • data
  • dumper
Read More

2955 snippets posted so far.