This is the complete image_processor.py module, allowing you to add an image containing an arbitrary piece of text. I use this to label the horizontal axis of a skills-matrix report.
Credit www.renewtek.com for paying me to do this stuff!
This is an override the save method of our Photo model. This new save method essentially takes the image, thumbnails it into our various sets of dimensions (for … in self.IMAGE_SIZES…), and save each one (into its own ImageField) before finally call the overwritten method to save the original image.
Yes, the dimensions are hardcoded, and there is currently not a way to regenerate them in different sizes, but one shouldn't be that hard to come up with, because you just could just load each photo object to regenerate, then save it again (or something along those lines).
mattpdx helped a lot with figuring out this code.
A Textarea widget which appends basic Textile formating instructions in the same way Basecamp's Writboard product displays some basic helper markup alongside the edit area.
Most of the time when I need to iterate over Whatever.objects.all() in a shell script, my machine promptly reminds me that sometimes even 4GB isn't enough memory to prevent swapping like a mad man, and bringing my laptop to a crawl. I've written 10 bazillion versions of this code. Never again.
**Caveats**
Note that you'll want to order the queryset, as ordering is not guaranteed by the database and you might end up iterating over some items twice, and some not at all. Also, if your database is being written to in between the time you start and finish your script, you might miss some items or process them twice.
I updated [MarkdownTextField](http://www.djangosnippets.org/snippets/882/) to have some choices in markup. It currently support for Markdown, Textile, Plain Text, and Plain HTML. It will add `%s_html` for the complied HTML and `%s_markup_choices` for a drop down of markup choices.
Usage:
class MyModel(models.Model):
description = MarkupTextField()
Request-phase cache middleware that checks to make sure the Cache server is running. Starting it if it is not. This is run for every request, it checks to see if it can get a defined item out of the cache, if that fails it tries to set it. Failing that it decides the server is probably crashed, so goes though and attempts to connect to the server. Failing a connection it will launch a new server.
This is probably not useful on large scale multi server deployments as they likely have their own testing for when services crash, but I am using it in a shared hosting environment where I have to run my own copy of memcache manually and cannot setup proper services testing, so I use this to just make sure the cache server is still running.
This template tag allows easy inclusion of google analytics script. If Google changes the script in the future, it remains easy to update the template tag with the the new code. This script is tested against Django 1.0 trunk Oct 9 2008.
**Readme**
After signing up for a Google Analytics account for your domain, define ANALYTICS_ID = "UA-XXXXXXX-X" in your settings.py with the supplied code.
Include {% load analytics %} at the top of your base.html and {% analytics %} tag at just before the closing 'body' tag of the base template.
Make a template to hold the analytics script.
templates/analytics/analytics_html
This class acts as a wrapper around multiple querysets. Use it if you want to chain multiple QSs together without combining them with | or &. eg., to put title matches ahead of body matches:
>>> qs1 = Event.objects.filter(## title matches ##)
>>> qs2 = Event.objects.filter(## matches in other fields ##)
>>> qs = MultiQuerySet(qs1, qs2)
>>> len(qs)
>>> paginator = Paginator(qs)
>>> first_ten = qs[:10]
It effectively acts as an immutable, sliceable QuerySet (with only a very limited subset of the QuerySet api)
An old snippet I made in my first django project. Nowadays I code menus in HTML and just use the perms proxy: https://docs.djangoproject.com/en/1.0/topics/auth/#id6
Credits, (awsome persons that helped me getting it to work efficiently and for free):
* Yhg1s and waveform from #python@freenode
* zendak from #django@freenode
Thank you!
Take the legwork out of processing forms.
Most people have a very specific structure to how they process forms in views. If the request method is "GET", then some HTML (with the blank form) is rendered. If the method is "POST", then the form is validated. If the form is invalid, some other HTML is displayed. If valid, the data is submitted and processed in some way.
In order to do this all in a much nicer way, simply subclass `FormHandler`, define three methods (`valid`, `invalid` and `unbound`), point to the form, and use the subclass as your view in the URLconf.
This is a custom block tag and is used like this:
{% load whitespaceoptimize %}
{% whitespaceoptimize "css" %}
/* CSS comment */
body {
color: #CCCCCC;
}
{% endwhitespaceoptimize %}
And when rendered you get this output:
body{color:#CCC}
To install it, download the snippet and call it `myapp/templatetags/whitespaceoptimize.py`. (Make sure you have a `__init__.py` in the `templatetags` directory)
You need to download and install [slimmer](http://www.issuetrackerproduct.com/Download#slimmer) and put on your Python path.
The block tag can be used for `javascript` and `html` as well as `css`. You can also let it guess the format; this would work for example:
{% whitespaceoptimize %}
<table>
<tr> ...
{% endwhitespaceoptimize %}
...but this would just replicate the functonality of the [built-in spaceless tag](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#spaceless)
The WorldIP database provides real-world geographical location. Database is more correct than [Whois records and Whois-based databases](http://www.wipmania.com/en/blog/why-worldip-data-rather-than-whois-data-examples/), that show geographic locations of network owners, and not the geographic location of Internet-connected PC or appliance itself.
See more: [WIPmania.com](http://www.wipmania.com)
This is the approach I've taken to access instances of child models from their parent. Functionally it's very similar to snippets [1031](http://www.djangosnippets.org/snippets/1031/) and [1034](http://www.djangosnippets.org/snippets/1034/), but without the use of `django.contrib.contenttypes`.
Usage:
class Post(ParentModel):
title = models.CharField(max_length=50)
objects = models.Manager()
children = ChildManager()
def __unicode__(self):
return self.title
def get_parent_model(self):
return Post
class Article(Post):
text = models.TextField()
class Photo(Post):
image = models.ImageField(upload_to='photos/')
class Link(Post):
url = models.URLField()
In this case, the `Post.children` manager will return a queryset containing instances of the appropriate child model, rather than instances of `Post`.
>>> Post.objects.all()
[<Post: Django>, <Post: Make a Tumblelog>, <Post: Self Portrait>]
>>> Post.children.all()
[<Link: Django>, <Article: Make a Tumblelog>, <Photo: Self Portrait>]
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.