Takes a float number (23.456) and uses the decimal.quantize to round it to a fixed exponent. This allows you to specify the exponent precision, along with the rounding method. And is perfect for monetary formatting taking into account precision.
DEPRECATED: Django has cookie based messages since version 1.2
This messages stores messages for the user in a cookie. I prefer this to session based messages, because the session column is a hot spot in the database if one user
works with several browser tabs/windows.
I needed the ability to serialize and deserialize my database, which contains millions of objects. The existing XML serializer encountered spurious parse errors; the JSON serializer failed to handle UTF-8 even when it was asked to; and both the JSON and YAML serializers tried to keep all the representations in memory simultaneously.
This custom serializer is the only one that has done the job. It uses YAML's "stream of documents" model so that it can successfully serialize and deserialize large databases.
This piece of code allows the quickly set the locale to a different language. This can be used for instance in cron jobs to send emails to users in their specific language.
This is pretty rough code, it be better to create an object, having two functions: set_locale and reset_locale. set_locale would than contain steps 1 to 4 (and return the request object on succes), reset_locale would be step 6
Enjoy!
`dumpdata` without `MemoryErrors`, with progress notification. Most of the real work is done by snippets [1400](http://www.djangosnippets.org/snippets/1400/) and [1401](http://www.djangosnippets.org/snippets/1401/).
./manage.py dumpdata_stream --format=xml > big_dump.xml
This is basically the stock Django `dumpdata` with a few modifications. Django devs: it's hard to reuse parts of most Django management commands. A little refactoring could go a long way.
This snippet suplies a resolve_to_name function that takes in a path and resolves it to a view name or view function name (given that the path is actually defined in your urlconf).
Example:
=== urlconf ====
urlpatterns = patterns(''
(r'/some/url', 'app.views.view'),
(r'/some/other/url', 'app.views.other.view', {}, 'this_is_a_named_view'),
)
=== example usage in interpreter ===
>>> from some.where import resolve_to_name
>>> print resolve_to_name('/some/url')
'app.views.view'
>>> print resolve_to_name('/some/other/url')
'this_is_a_named_view'
"Thus, if a LOGGER is configured inside settings.py, we use that. Otherwise, we just use vanilla logging functions with the global logging configuration. Nice and sweet."
Naturally, the logger can be anything described [here](http://docs.python.org/library/logging.html), I'm just using the RotatingFileHandler as an example because that's what I was using in my project.
Full write up+shamless plug [here](http://mihasya.com/blog/?p=237)
Entirely based on and with big thanks to:
[http://www.tomanthony.co.uk/](http://www.tomanthony.co.uk/)
Drops in a googlemap with a placemarker based on a uk postcode
Looks like this:
{% googlemap_from_ukpostcode postcode "XxY" zoom %}
e.g.
{% googlemap_from_ukpostcode "SP27AS" "220x290" 16 %}
postcode and zoom can optionally be template variables.
"XxY" is the x/y size of the map you want to drop in.
zoom can be omitted and defaults to 14.
requires:
in settings: GOOGLE_AJAX_API_KEY, GOOGLE_MAPS_API_KEY
google_map_ukpostcodes.js: is slight variation on js at http://www.tomanthony.co.uk/demo/geocode_uk_postcode/gmap.js
For further and better info see:
[http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/](http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/)
For disabling autocomplete and security purpose, this snippet defines a CharField with a randomness name for each request of the form.
This is useful for turning off autocomplete for credit card input in all browsers, without breaking the xhtml validation.
* [https://wiki.mozilla.org/The_autocomplete_attribute_and_web_documents_using_XHTML#Security](https://wiki.mozilla.org/The_autocomplete_attribute_and_web_documents_using_XHTML#Security)
* [http://en.wikipedia.org/wiki/Cryptographic_nonce](http://en.wikipedia.org/wiki/Cryptographic_nonce)
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
});
I've often found myself wanting to store passwords for other web services (e.g. maillist managers, IMAP accounts, IM accounts etc) for use by a web application, but have not wanted to hard-code them in `settings.py` or store them as plaintext in the database.
This uses the [pycrypto][] library to encrypt each bit of information using the concatenation of a salt value and the SECRET_KEY value from your `settings.py`, hopefully leading to a bit more security and flexibility.
[pycrypto]:http://www.dlitz.net/software/pycrypto/
You'll probably want to add some views to let people edit these things as I can't find a way to make the admin interface play nicely with it.
**Example usage:**
In [1]: p = Password(
name='IMAP account', slug='imap',
username='example', password='password',
host='imap.gmail.com'
)
In [2]: p.host
Out[2]: 'imap.gmail.com'
In [3]: p.e_host
Out[3]: '6wdyMDKYy8c=$YXw6t/Q9wI[...]'
In [4]: p.save()
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.