From 29c65044bf1af13161e52801a1589ec0f3d93fd9 Mon Sep 17 00:00:00 2001 From: Elrond Date: Fri, 25 Jan 2013 21:53:28 +0100 Subject: [PATCH] Refactor generate_slug into a mixin. Refactor the new generate_slug into a mixin to allow it to be used for collections also. --- mediagoblin/db/mixin.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index 0832c21a..a55a1905 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -52,7 +52,7 @@ class UserMixin(object): return cleaned_markdown_conversion(self.bio) -class MediaEntryMixin(object): +class GenerateSlugMixin(object): def generate_slug(self): """ Generate a unique slug for this MediaEntry. @@ -76,10 +76,6 @@ class MediaEntryMixin(object): 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 - #Is already a slug assigned? Check if it is valid if self.slug: self.slug = slugify(self.slug) @@ -100,14 +96,13 @@ class MediaEntryMixin(object): return # giving up! # Otherwise, let's see if this is unique. - if check_media_slug_used(self.uploader, self.slug, self.id): + if self.check_slug_used(self.slug): # 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): + if not self.check_slug_used(slug_with_id): self.slug = slug_with_id return # success! @@ -115,9 +110,18 @@ class MediaEntryMixin(object): # 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): + while self.check_slug_used(self.slug): self.slug += uuid.uuid4().hex[:4] + +class MediaEntryMixin(GenerateSlugMixin): + def check_slug_used(self, 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 + + return check_media_slug_used(self.uploader, slug, self.id) + @property def description_html(self): """ -- 2.25.1