Merge branch '216_cwebber_style_unique_slugs'
This commit is contained in:
@@ -27,6 +27,8 @@ These functions now live here and get "mixed in" into the
|
||||
real objects.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
|
||||
from werkzeug.utils import cached_property
|
||||
|
||||
from mediagoblin import mg_globals
|
||||
@@ -52,19 +54,69 @@ class UserMixin(object):
|
||||
|
||||
class MediaEntryMixin(object):
|
||||
def generate_slug(self):
|
||||
"""
|
||||
Generate a unique slug for this MediaEntry.
|
||||
|
||||
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.
|
||||
"""
|
||||
# import this here due to a cyclic import issue
|
||||
# (db.models -> db.mixin -> db.util -> db.models)
|
||||
from mediagoblin.db.util import check_media_slug_used
|
||||
|
||||
self.slug = slugify(self.title)
|
||||
#Is already a slug assigned? Check if it is valid
|
||||
if self.slug:
|
||||
self.slug = slugify(self.slug)
|
||||
|
||||
# otherwise, try to use the title.
|
||||
elif self.title:
|
||||
# assign slug based on title
|
||||
self.slug = slugify(self.title)
|
||||
|
||||
# We don't want any empty string slugs
|
||||
if self.slug == u"":
|
||||
self.slug = None
|
||||
|
||||
duplicate = check_media_slug_used(self.uploader, self.slug, self.id)
|
||||
|
||||
if duplicate:
|
||||
if self.id is not None:
|
||||
self.slug = u"%s-%s" % (self.id, self.slug)
|
||||
else:
|
||||
self.slug = None
|
||||
# Do we have anything at this point?
|
||||
# If not, we're not going to get a slug
|
||||
# so just return... we're not going to force one.
|
||||
if not self.slug:
|
||||
return # giving up!
|
||||
|
||||
# Otherwise, let's see if this is unique.
|
||||
if check_media_slug_used(self.uploader, self.slug, self.id):
|
||||
# It looks like it's being used... lame.
|
||||
|
||||
# Can we just append the object's id to the end?
|
||||
if self.id:
|
||||
slug_with_id = u"%s-%s" % (self.slug, self.id)
|
||||
if not check_media_slug_used(self.uploader,
|
||||
slug_with_id, self.id):
|
||||
self.slug = slug_with_id
|
||||
return # success!
|
||||
|
||||
# okay, still no success;
|
||||
# let's whack junk on there till it's unique.
|
||||
self.slug += '-' + uuid.uuid4().hex[:4]
|
||||
# keep going if necessary!
|
||||
while check_media_slug_used(self.uploader, self.slug, self.id):
|
||||
self.slug += uuid.uuid4().hex[:4]
|
||||
|
||||
@property
|
||||
def description_html(self):
|
||||
|
||||
Reference in New Issue
Block a user