Login

Snippets by simon

Snippet List

Accepting and processing PayPal IPN messages (including using App Engine)

PayPal's [https://www.paypal.com/ipn](IPN mechanism) is ridiculously easy to consume. You can tell PayPal to POST every single transaction on your account to a URL somewhere, then set up a handler at that URL which processes those transactions in some way. Security is ensured by POSTing the incoming data back to PayPal for confirmation that the transaction is legitimate. These classes are probably over-engineered, but they were a fun experiment in creating class-based generic views.

  • appengine
  • paypal
  • payment
  • ipn
Read More

RequestFactory: Easily create mock request objects, for use in testing

Django's testing framework assumes you will be running your tests against "live" views that have been plugged in to your site's URL configuration - but sometimes you might want to run a test against a view function without first wiring it in to the rest of the site. This class makes it easy to do that by providing a "factory" for creating mock request objects, re-using the existing test Client interface (and most of the code). Once you've created a request object in your test you can use it to call your view functions directly, then run assertions against the response object that gets returned.

  • testing
  • httprequest
Read More

DebugFooter middleware

Adds a hidden footer to the bottom of every text/html page containing a list of SQL queries executed and templates that were loaded (including their full filesystem path to help debug complex template loading scenarios). To use, drop in to a file called 'debug_middleware.py' on your Python path and add 'debug_middleware.DebugFooter' to your MIDDLEWARE_CLASSES setting.

  • sql
  • middleware
  • debugging
Read More

module_from_path

I needed to dynamically import a module based on a path to that file on disk, without it necessarily being on the Python Path.

  • import
Read More

sort_by_id_sequence

I needed to sort a set of objects (a QuerySet for example) by an externally provided list of IDs - for example: >>> writers = Writer.objects.all() >>> sort_by_id_sequence(writers, [3, 1, 2]) [<Writer id: 3>, <Writer id: 1>, <Writer id: 2>]

  • sorting
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

Fire Eagle example: views.py from wikinear.com

The views.py used by [wikinear.com](http://wikinear.com/) - see [http://simonwillison.net/2008/Mar/22/wikinear/](http://simonwillison.net/2008/Mar/22/wikinear/) For a more comprehensive API wrapper for Fire Eagle, take a look at [fireeagle_api.py](http://github.com/SteveMarshall/fire-eagle-python-binding/tree/master/fireeagle_api.py)

  • location
  • fireeagle
  • oauth
Read More

Partial OpenID provider implementation from idproxy.net

Lots of people have asked me when the [django-openid](http://code.google.com/p/django-openid/) package will provide tools for running an OpenID provider (in addition to an OpenID consumer). The answer is "when it's done", but since it's such a common request I've decided to post some example code to help people who want to work it out for themselves. This is the openidserver.py file from [idproxy.net](http://idproxy.net/). It is **incomplete** - the urlconf, models, templates and some utility functions are not included. In other words, it's useless for anything other than providing a few hints as to how you can go about implementing a provider. Nonetheless, it's better than nothing. I hope this will prove a useful example for people trying to figure out how to best integrate the JanRain Python OpenID library with Django to build an OpenID provider.

  • openid
  • incomplete
  • partial
Read More

BritishDateField

The date field in the newforms module includes American style mm/dd/yyyy , which anyone outside the US will recognise as being complete madness. This date field behaves sensibly.

  • newforms
Read More

simon has posted 29 snippets.