Login

Snippets by simon

Snippet List

Amazon S3 browser-based upload form

A Django Form for creating a browser-based upload form that pushes files to Amazon S3. See http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434

  • forms
  • upload
  • s3
  • amazon
Read More

Counter model - run multiple persistent counters

Sometimes you just need to count things (or create unique-for-your-application IDs). This model class allows you to run as many persistent counters as you like. Basic usage looks like this: >>> Counter.next() 0 >>> Counter.next() 1L >>> Counter.next() 2L That uses the "default" counter. If you want to create and use a different counter, pass its name as a string as the parameter to the method: >>> Counter.next('hello') 0 >>> Counter.next('hey') 0 >>> Counter.next('hello') 1L >>> Counter.next('hey') 1L >>> Counter.next('hey') 2L You can also get the value as hex (if you want slightly shorter IDs, for use in URLs for example): >>> Counter.next_hex('some-counter-that-is-quite-high') 40e

  • database
  • counters
Read More

TextField

Alex Gaynor presented this idiom at EuroDjangoCon 2009.

  • forms
  • textfield
Read More

Debug view: show available named URL patterns

Hook the show_url_patterns view function in to your URLconf to get a page which simply lists all of the named URL patterns in your system - useful for if your template developers need a quick reference as to what patterns they can use in the {% url %} tag.

  • urlconf
  • debug
  • debugging
  • urlpatterns
  • documentation
  • docs
Read More

generateChart() for creating a Google Chart API pie chart from JavaScript

I ended up not needing this (there's a good reason it's in JS and not Python, but most people would probably want to do this server-side instead) but I'm stashing it here in case I need it later. It uses jQuery for the .each() method - which is very easy to replace if you need it to work without that dependency. Usage: var src = generateChart({ "foo": 5, "bar": 3, "baz": 6 });

  • javascript
  • googlechartapi
  • piechart
  • graphs
Read More

RestView - class for creating a view that dispatches based on request.method

Sometimes it's useful to dispatch to a different view method based on request.method - e.g. when building RESTful APIs where GET, PUT and DELETE all use different code paths. RestView is an extremely simple class-based generic view which (although it's a stretch to even call it that) which provides a simple mechanism for dividing up view logic based on the HTTP method.

  • rest
  • view
  • classbasedgenericviews
Read More

Orderable inlines using drag and drop with jQuery UI

An easy way of making inlines orderable using drag-and-drop, using [jQuery UI's](http://ui.jquery.com/) sortable() plugin. First, add an "order" field to the inline models which is an IntegerField, and set that model to use 'order' as its default order_by. Then hook in the JavaScript. This should make them drag-and-drop sortable using jQuery UI, and also hide the divs containing those order fields once the page has loaded. This technique is unobtrusive: if JavaScript is disabled, the order fields will be visible and the user will be able to manually set the order by entering numbers themselves.

  • jquery
  • inlines
  • pyconuk2008
Read More

Retrieve a list of countries from GeoNames

GeoNames provides a useful data file with information about every country in the world (DjangoPeople uses this). Here's an importer that grabs the file from the web and turns it in to a list of dictionaries.

  • countries
  • geonames
Read More

Sign a string using SHA1, then shrink it using url-safe base65

Sometimes it's useful to sign data to ensure the user does not tamper with it - for example, cookies or hidden form variables. SHA1 is cryptographically secure but weighs in at 40 characters, which is pretty long if you're going to be passing the data around in a URL or a cookie. These functions knock an SHA1 hash down to just 27 characters, thanks to a base65 encoding that only uses URL-safe characters (defined as characters which are unmodified by Python's urllib.urlencode function). This compressed hash can then be passed around in cookies or URLs, and uncompressed again when the signature needs to be checked. UPDATE: You probably shouldn't use this; see [http://fi.am/entry/urlsafe-base64-encodingdecoding-in-two-lines/](http://fi.am/entry/urlsafe-base64-encodingdecoding-in-two-lines/) for a smarter approach based on Python's built-in base64 module.

  • security
  • base65
  • signing
  • cookies
  • hashlib
  • hashes
  • sha1
Read More

simon has posted 29 snippets.