Example mixin for creating and removing thumbnails on a model. Also adds some methods for accessing an url for an image size. It might be a better idea to use a custom image field instead of this approach.
Idea for getting the image url for different sizes from http://code.google.com/p/django-photologue
Example usage
<pre>
>>> p.get_small_image_url()
u'http://127.0.0.1:8000/site_media/photos/small_someimage.jpg'
</pre>
Based on [svetlyak](http://www.djangosnippets.org/users/svetlyak/)'s [nofollow filter](http://www.djangosnippets.org/snippets/312/). This one processes only external URL's. Links with internal URL's will be returned unmodified.
There's one gotcha; I preferred to have false positives instead of false negatives. So, it will nofollow `href="some/relative/path/"` for example. If you must do relative paths do it like this:
<a href="./some/relative/path">link text</a>
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.
**Mobilize your Django site**
This is the code for a Django middleware class to allow you to easily mobilize your Django site. It makes use of [Wapple.net](http://wapple.net)'s Web services to provide device profiling and markup generation.
Using this middleware plugin, you can deliver your site to both mobile and web browsers using the same domain and exactly the same url structure and Python views. The WAPL markup language allows you to render sites to every single mobile device without worrying about the individual devices yourself.
**Requirements**
1. The [SUDS](https://fedorahosted.org/suds/) Python SOAP client.
2. A WAPL dev key. Sign up for one at [http://wapl.info](http://wapl.info)
3. The Django sessions framework must be enabled. See [http://docs.djangoproject.com/en/dev/topics/http/sessions/](http://docs.djangoproject.com/en/dev/topics/http/sessions/) for how to install.
**How To Use**
1. Save the code above as 'wapl_middleware.py' in the root of your project.
2. Replace 'YOUR-DEV-KEY-HERE' with your WAPL dev key.
3. In your project's 'settings'py', add the following to the bottom of your 'MIDDLEWARE_CLASSES' setting:
`'myapp.wapl_middleware.WAPLMiddleware',`
4. For each line in your 'TEMPLATE_DIRS' setting, create a new folder under that folder called 'wapl' e.g. for 'myapp/templates/', you would create the folder under 'myapp/templates/wapl/'.
5. For each template used in your application, write a WAPL version and save it in the corresponding 'wapl' directory. See [http://wapl.info/docs/chapter/Developing-with-WAPL/](the WAPL docs) for information about the WAPL markup language.
6. Django template inheritance and includes can be used as normal, so I recommend creating a 'base.html' like this one.
`<?xml version="1.0" encoding="UTF-8" ?>
<wapl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://wapl.wapple.net/wapl.xsd">
<head>
{% block wapl_head %}{% endblock %}
</head>
<layout>
{% block wapl_layout %}{% endblock %}
</layout>
</wapl>`
7. View your site from a mobile device, and you should see a nice mobile version.
**How It Works**
1. When a request is made, the middleware checks to see if a session variable is held telling us if the device is mobile or not.
2. If we don't already know, it calls the WAPL web services to check. It then stores this in the session for subsequent requests.
3. If the device is a mobile device, it appends 'wapl' to each line in your 'TEMPLATE_DIRS' setting. Your view code will work exactly the same as normal, and the 'wapl' templates will be used whenever a response is rendered.
4. When a response is about to be rendered, the middleware checks to see if the device is a mobile one.
5. If it is mobile, and the response about to be sent has a status code of 200 (OK), it sends the WAPL markup to the WAPL web service to generate the correct markup for that device.
6. Otherwise, it outputs the response unmodified.
**Tips**
1. Don't try to migrate your whole site to mobile - design for mobile and consider the user's goals on a handset.
2. If leaving sections out, don't just leave the wapl view out. Include one that says 'This page is not available on mobile'. This will make sure none of your external links are dead on the mobile version.
3. For full developer reference, information and schemas, see [http://wapl.info](WAPL.info).
If you have JSON objects with `datetime` attributes that you want to decode to python [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) objects, you can use `decode_datetime` as a [simplejson](http://simplejson.googlecode.com/svn/tags/simplejson-2.0.9/docs/index.html) object hook. `simplejson.loads(s, object_hook=decode_datetime)`.
If you want to do your own JSON serialization of [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) objects instead of using DjangoJSONEncoder, use `simplejson.dumps(o, default=encode_datetime)`. The `encode_datetime` method will convert the datetime object to UTC and output an ISO format string just like the [isoutc template filter](http://www.djangosnippets.org/snippets/1424/).
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.
While working on my website projects today I had the idea to use HTML/JS instead of a IP database to localize the dates and times shown on the websites.
Here are the snippets to use the JS snippets as filters for Django running on Google App Engine. You can use those filters on datetime objects.
usage:
{{ object.content|wordbreak:"10" }}
This means if any word is 10 characters or longer, a `­` will be placed every 10 characters. This is to break long words which may break the appearance of a page.
The output of this is HTML safe as the content has been conditionally escaped.
Convert numbers from base 10 integers to base X strings and back again.
Sample usage:
>>> base20 = BaseConverter('0123456789abcdefghij')
>>> base20.from_decimal(1234)
'31e'
>>> base20.to_decimal('31e')
1234
Slightly shonky implementation of a custom URL shortening view, as [described on my blog](http://simonwillison.net/2009/Apr/11/revcanonical/). Depends on [baseconv.py](http://www.djangosnippets.org/snippets/1431/)
This snipped get the latest rss links from delicious from a DELICIOUS_USER that you have to define in your settings.
I use this snippet in the [trespams code blog](http://trespams.com) to inform the visitor about the last links I have bookmarked.
This is a modified version of [snipped 819](http://www.djangosnippets.org/snippets/819/) to allow using any variable in the template to obtain the results.
It also added a 6 hours caché for the rss.
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!
This is based on snippets 29 and 43, both of which had good ideas, and I thought, "why not combine them?"
Given the new_topic string above, the resulting slug will be:
"test-long-string-which-has-many-words-here"
John Crawford
Note - requires python 2.5, for list comprehensions. Otherwise you could just use a for loop to build the clean_list.