Converting PDT to UTC using pytz and dateutil
Every time I have to do this it takes me a solid half hour to figure it out, so I'm throwing it up here for future reference. I hate timezone calculations.
- utc
- pytz
- timezones
- pdt
Every time I have to do this it takes me a solid half hour to figure it out, so I'm throwing it up here for future reference. I hate timezone calculations.
Exporting unicode data to Excel in a CSV file is surprisingly difficult. After much experimentation, it turns out the magic combination is UTF-16, a byte order mark and tab-delimiters. This snippet provides two classes - UnicodeWriter and UnicodeDictWriter - which can be used to output Excel-compatible CSV.
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.
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.
If your URLs need to contain numeric IDs a neat way of shortening them is to use base36.
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.
I needed to dynamically import a module based on a path to that file on disk, without it necessarily being on the Python Path.
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>]
An easy way to add custom methods to the QuerySet used by a Django model. See [simonwillison.net/2008/May/1/orm/](http://simonwillison.net/2008/May/1/orm/) for an in-depth explanation.
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)
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)
I needed this to work around a poorly configured redirecting service.
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.
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.
simon has posted 29 snippets.