I had a problem: too many fetches from the DB.
So, how to reduce load on the database without major changes to the code?
Cache Manager is the answer. I've managed to reduce number of DB hits as much as 80% in some cases (dictionaries, complex relations). It is using standard cache mechanisms. I'm using it with mathopd.
This is a very simple solution, instead of standard Manager, put this in your model definition as:
`objects = CacheManager()`
Then everythere elase in the code instead of all() or get(...) call all_cached() or get_cached().
I've kept original methods intact, to have an dual access, when you really, really must have frest data from the DB, and you can't wait for cache to expire.
This is much easier to work with, then manually getting fetched data from the cache.No change to your existing code 9except model) and voila!
Additionally if you have some data, you would like to store with your serialized object (e.g. related data, dynamic dictionaries), you can do this in the model method '_init_instance_cache').
Drop me an email if you find this useful. :)
A Django image thumbnail filter, adapted from code by [Batiste Bieler](http://batiste.dosimple.ch/blog/2007-05-13-1/).
This updated version drops support for cropping and just rescales. You should use it in your templates like this:
`<img src='{{ MEDIA_URL }}{{ image.get_image_filename|thumbnail:"300w,listingimages" }}' alt="{{ image.title }}" title="{{ image.title }}" />`
This will produce a 300-pixel wide thumbnail of image, with the height scaled appropriately to keep the same image aspect ratio. 'listingimages' is the path under your MEDIA_ROOT that the image lives in - it'll be whatever upload_to is set to in your ImageField.
If instead you wanted an image scaled to a maximum height of 140px, you'd use something like this:
`<img src='{{ MEDIA_URL }}{{ image.get_image_filename|thumbnail:"140h,listingimages" }}' alt="{{ image.title }}" title="{{ image.title }}" />`
Note the number has changed from 300 to 140, and the trailing letter from 'w' to 'h'.
Please leave feedback and bug reports on [my blog, Stereoplex](http://www.stereoplex.com/two-voices/a-django-image-thumbnail-filter). I've only lightly tested this so you'll probably find something!
This filter will display the time as word(s) indicating roughly the time of day ("Morning", "Afternoon", "Evening", etc). For example, the following template snippet:
Posted in the {{ post.date|fuzzy_time }} of {{ post.date|date:"F j, Y"} }}.
will result in the following (assuming `post.date == datetime.datetime(2007, 6, 13, 20, 57, 55, 765000)`):
Posted in the evening of June 13, 2007.
The terms used and breakpoints (hours only) can be rather arbitrary so you may want to adjust them to your liking. See the docs for [bisect][] for help in understanding the code. Just remember you should have one less breakpoint than periods and the first breakpoint falls at the end of the first period. The idea was inspired by [Dunstan Orchard][1], although the code is *very* different (php case statement). He uses quite a bit more periods in a day, so you might want to take a look.
[bisect]: http://docs.python.org/lib/module-bisect.html
[1]: http://www.1976design.com/blog/archive/2004/07/23/redesign-time-presentation/
Here are a couple of Django decorators for limiting access to a view based on the request's `HTTP_REFERER`. Both raise a Django `PermissionDenied` exception if the referer test fails (or a referer simply isn't provided).
The first, `referer_matches_hostname`, takes a hostname (and port, if specified) and matches it against the referer's. If multiple arguments are supplied a match against any of the hostnames will be considered valid.
The second, `referer_matches_re`, takes a regex pattern (like Django's urlpattern) and tests if it matches the referer. This is obviously more flexible than `referer_matches_hostname` providing the ability to match not just the hostname, but any part of the referer url.
Finally there's an simple example decorator, `local_referer_only`, that limits a view to the current site by using Django's `django.contrib.sites` to look up the current hostname.
- view
- referer
- decorator
- http_referer
- request