This code provides a primary key field that is globally unique. It uses the pre_save method to auto-populate the field with a Universal Unique Id as the record is saved the first time.
This uses the Really Simple Color Picker in jQuery:
http://www.web2media.net/laktek/2008/10/27/really-simple-color-picker-in-jquery/
Get source from there or GitHub:
http://github.com/laktek/really-simple-color-picker/tree/master
Easy way to generate image thumbnails for your models. Works with any Storage Backend.
From: [http://code.google.com/p/django-thumbs/](http://code.google.com/p/django-thumbs/)
**Usage example:**
==============
photo = ImageWithThumbsField(upload_to='images', sizes=((125,125),(300,200),)
To retrieve image URL, exactly the same way as with ImageField:
my_object.photo.url
To retrieve thumbnails URL's just add the size to it:
my_object.photo.url_125x125
my_object.photo.url_300x200
Note: The 'sizes' attribute is not required. If you don't provide it,
ImageWithThumbsField will act as a normal ImageField
**How it works:**
=============
For each size in the 'sizes' atribute of the field it generates a
thumbnail with that size and stores it following this format:
available_filename.[width]x[height].extension
Where 'available_filename' is the available filename returned by the storage
backend for saving the original file.
Following the usage example above: For storing a file called "photo.jpg" it saves:
photo.jpg (original file)
photo.125x125.jpg (first thumbnail)
photo.300x200.jpg (second thumbnail)
With the default storage backend if photo.jpg already exists it will use these filenames:
photo_.jpg
photo_.125x125.jpg
photo_.300x200.jpg
**Note:** It assumes that if filename "any_filename.jpg" is available
filenames with this format "any_filename.[widht]x[height].jpg" will be available, too.
Truncates a string after a given length, keeping the last word complete.
This filter is more precise than the default `truncatewords` filter.
Words length vary too much, 10 words may result in 40 or 70 characters, so cutting by character count makes more sense.
There is a [blog post](http://ricobl.wordpress.com/2008/12/23/templates-django-filtro-truncatewords-melhorado/) about this snippet (in Portuguese).
Some times I want to change the `owner` of an object to another user - problem is the object often has a lot of other objects pointing to them - I also want to update those fields.
This is a generic snippet for doing just that!
For instance:
change_owner(obj, new_owner_id):
return update_related_field(obj, new_owner_id, field="user")
Something I end up doing all the time, making a boolean variable show up as a nice image. With this code you can do the following:
`{% boolean_img user.is_active %}`
And get the following output:
`<img src="/media/icons/accept.png" alt="True" />`
All you need to do is use the custom templatetag code, load it in your template and use the `boolean_img` tag.
**Adjust templates, html and images where needed**
A simple way to log in and log out users. It's most likely nowhere near as good as the built-in login forms. This was more of a learning experience for me. Nevertheless, the script works.
Usage:
def some_view(request):
check_login(request)
Some explanation can be found here: http://bit.ly/ete0
My modified version of the [MultiQuerySet by mattdw](http://www.djangosnippets.org/snippets/1103/) (see the link for further information).
My purpose for this was to enable me to combine multiple different types of querysets together, which could then be iterated on as one object (i.e. like a tumblelog).
This middleware will look for the cookies set when a Facebook Connect user authenticates to your site, read those cookies, determine if the logged in user is your Facebook friend and then log that user into your Django-powered site.
If you don't need the bit about friend verification, it should be trivial to strip out.
There are a couple of other things that are needed to get FB Connect working with your site, and you can find a more detailed entry [here (http://nyquistrate.com/django/facebook-connect/)](http://nyquistrate.com/django/facebook-connect/).
Personally I hate using markdown for text input just so it can be converted into HTML. Markdown languages almost always don't support some thing I want to do; thus, why not just use HTML in the first place. Well because you don't want anybody posting any kind of HTML on your site.
Solution, instead of making your users learn markdown, let them enter HTML and filter out bad tags. This is a filter I use to filter HTML for only certain allowed tags. The allowed tags can be configured with the **allowedhtml** list.
To make your text input even more user friendly use a Javascript HTML editor like [FCK Editor](http://www.fckeditor.net/) so your users will have a nice GUI editor.
Invoice numbers like "0000004" are a little unprofessional in that they expose how many sales a system has made, and can be used to monitor the rate of sales over a given time. They are also harder for customers to read back to you, especially if they are 10 digits long.
This is simply a perfect hash function to convert an integer (from eg an ID AutoField) to a unique number. The ID is then made shorter and more user-friendly by converting to a string of letters and numbers that wont be confused for one another (in speech or text).
To use it:
import friendly_id
class MyModel(models.Model):
invoice_id = models.CharField(max_length=6, null=True, blank=True, unique=True)
def save(self, *args, **kwargs):
super(MyModel, self).save(*args, **kwargs)
# Populate the invoice_id if it is missing
if self.id and not self.invoice_id:
self.invoice_id = friendly_id.encode(self.id)
super(MyModel, self).save(*args, **kwargs)
if self.id and not self.invoice_id
When an object from this model is saved, an invoice ID will be generated that does not resemble those surrounding it. For example, where you are expecting millions of invoices the IDs generated from the AutoField primary key will be:
obj.id obj.invoice_id
1 TTH9R
2 45FLU
3 6ACXD
4 8G98W
5 AQ6HF
6 DV3TY
...
9999999 J8UE5
The functions are deterministic, so running it again sometime will give the same result, and generated strings are unique for the given range (the default max is 10,000,000). Specifying a higher range allows you to have more IDs, but all the strings will then be longer. You have to decide which you need: short strings or many strings :-)
This problem could have also been solved using a random invoice_id generator, but that might cause collisions which cost time to rectify, especially when a decent proportion of the available values are taken (eg 10%). Anyhow, someone else has now already written this little module for you, so now you don't have to write your own :-)
As I was unable to find good examples to render a Form with two or more inlineformsets.
Therefor I have posted this to Django snippets.
This code is little different from another snippet with a Form with one InlineFormSet (the prefixes are necessary in this situation).
The example shows a person's data together with two inline formsets (phonenumbers and addresses) for a person.
You can add, update and delete from this form.