Fix problem where duplicate slug would cause an update exception
This commit is contained in:
parent
44c53d3bc4
commit
c785f3a090
@ -87,42 +87,45 @@ class GenerateSlugMixin(object):
|
|||||||
generated bits until it's unique. That'll be a little bit of junk,
|
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.
|
but at least it has the basis of a nice slug.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#Is already a slug assigned? Check if it is valid
|
#Is already a slug assigned? Check if it is valid
|
||||||
if self.slug:
|
if self.slug:
|
||||||
self.slug = slugify(self.slug)
|
slug = slugify(self.slug)
|
||||||
|
|
||||||
# otherwise, try to use the title.
|
# otherwise, try to use the title.
|
||||||
elif self.title:
|
elif self.title:
|
||||||
# assign slug based on title
|
# assign slug based on title
|
||||||
self.slug = slugify(self.title)
|
slug = slugify(self.title)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# We don't have any information to set a slug
|
||||||
|
return
|
||||||
|
|
||||||
# We don't want any empty string slugs
|
# We don't want any empty string slugs
|
||||||
if self.slug == u"":
|
if slug == u"":
|
||||||
self.slug = None
|
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!
|
|
||||||
|
|
||||||
# Otherwise, let's see if this is unique.
|
# 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.
|
# It looks like it's being used... lame.
|
||||||
|
|
||||||
# Can we just append the object's id to the end?
|
# Can we just append the object's id to the end?
|
||||||
if self.id:
|
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):
|
if not self.check_slug_used(slug_with_id):
|
||||||
self.slug = slug_with_id
|
self.slug = slug_with_id
|
||||||
return # success!
|
return # success!
|
||||||
|
|
||||||
# okay, still no success;
|
# okay, still no success;
|
||||||
# let's whack junk on there till it's unique.
|
# 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!
|
# keep going if necessary!
|
||||||
while self.check_slug_used(self.slug):
|
while self.check_slug_used(slug):
|
||||||
self.slug += uuid.uuid4().hex[:4]
|
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):
|
class MediaEntryMixin(GenerateSlugMixin):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user