Make generate_slug assign a slug in any case
authorSebastian Spaeth <Sebastian@SSpaeth.de>
Wed, 9 Jan 2013 11:40:18 +0000 (12:40 +0100)
committerSebastian Spaeth <Sebastian@SSpaeth.de>
Tue, 15 Jan 2013 19:08:21 +0000 (20:08 +0100)
generate_slug could assign "none" as slug. Make sure it assigns a unique
slug in any case. We now try based on:

a) existing slug values
b) media.title
c) media.id
d) random garbage

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
mediagoblin/db/mixin.py

index 001b7826425bee1a6ee67a0079166bf89bb95ec0..daeda8cedb48f19617b2f34d9396305f86ca1c0f 100644 (file)
@@ -56,15 +56,22 @@ class MediaEntryMixin(object):
         # (db.models -> db.mixin -> db.util -> db.models)
         from mediagoblin.db.util import check_media_slug_used
 
-        self.slug = slugify(self.title)
-
-        duplicate = check_media_slug_used(self.uploader, self.slug, self.id)
+        #Is already a slug assigned? Check if it is valid
+        if self.slug:
+            self.slug = slugify(self.slug)
+        elif self.title:
+            #assign slug based on title
+            self.slug = slugify(self.title)
+        elif self.id:
+            # Does the object already have an ID? (after adding to the session)
+            self.slug = unicode(self.id)
+        else:
+            # Everything else failed, just use random garbage
+            self.slug = unicode(uuid4())[1:4]
 
-        if duplicate:
-            if self.id is not None:
-                self.slug = u"%s-%s" % (self.id, self.slug)
-            else:
-                self.slug = None
+        while check_media_slug_used(self.uploader, self.slug, self.id):
+            # add garbage till it's unique
+            self.slug = self.slug + unicode(uuid4())[1:4]
 
     @property
     def description_html(self):