+class GenerateSlugMixin(object):
+ """
+ Mixin to add a generate_slug method to objects.
+
+ Depends on:
+ - self.slug
+ - self.title
+ - self.check_slug_used(new_slug)
+ """
+ def generate_slug(self):
+ """
+ Generate a unique slug for this object.
+
+ This one does not *force* slugs, but usually it will probably result
+ in a niceish one.
+
+ The end *result* of the algorithm will result in these resolutions for
+ these situations:
+ - If we have a slug, make sure it's clean and sanitized, and if it's
+ unique, we'll use that.
+ - If we have a title, slugify it, and if it's unique, we'll use that.
+ - If we can't get any sort of thing that looks like it'll be a useful
+ slug out of a title or an existing slug, bail, and don't set the
+ slug at all. Don't try to create something just because. Make
+ sure we have a reasonable basis for a slug first.
+ - If we have a reasonable basis for a slug (either based on existing
+ slug or slugified title) but it's not unique, first try appending
+ the entry's id, if that exists
+ - If that doesn't result in something unique, tack on some randomly
+ generated bits until it's unique. That'll be a little bit of junk,
+ but at least it has the basis of a nice slug.
+ """