From: Jessica Tallon Date: Wed, 22 Oct 2014 17:59:04 +0000 (+0100) Subject: Fix problem where duplicate slug would cause an update exception X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c785f3a09070173ed31fd4f16544c25e12e01b96;p=mediagoblin.git Fix problem where duplicate slug would cause an update exception --- diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index 39690cfc..e8285e0c 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -87,42 +87,45 @@ class GenerateSlugMixin(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. """ + #Is already a slug assigned? Check if it is valid if self.slug: - self.slug = slugify(self.slug) + slug = slugify(self.slug) # otherwise, try to use the title. elif self.title: # assign slug based on title - self.slug = slugify(self.title) + slug = slugify(self.title) - # We don't want any empty string slugs - if self.slug == u"": - self.slug = None + else: + # We don't have any information to set a slug + return - # 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! + # We don't want any empty string slugs + if slug == u"": + return # Otherwise, let's see if this is unique. - if self.check_slug_used(self.slug): + if self.check_slug_used(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) + slug_with_id = u"%s-%s" % (slug, self.id) if not self.check_slug_used(slug_with_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] + slug += '-' + uuid.uuid4().hex[:4] # keep going if necessary! - while self.check_slug_used(self.slug): - self.slug += uuid.uuid4().hex[:4] + while self.check_slug_used(slug): + slug += uuid.uuid4().hex[:4] + + # self.check_slug_used(slug) must be False now so we have a slug that + # we can use now. + self.slug = slug class MediaEntryMixin(GenerateSlugMixin):