fixing bug on import allowing two notes with the same name to create non-unique slugs
authorCharles Holbrow <charlesholbrow@gmail.com>
Mon, 25 Mar 2013 21:14:54 +0000 (17:14 -0400)
committerCharles Holbrow <charlesholbrow@gmail.com>
Mon, 25 Mar 2013 21:14:54 +0000 (17:14 -0400)
karmaworld/apps/courses/management/commands/import_json.py
karmaworld/apps/notes/models.py

index 501f1a415d9a4fc257fc58d39b0bf01d66218b4d..c7bb7a405f15b3e7e9af092449bd8dc587fd4ec9 100644 (file)
@@ -111,6 +111,10 @@ class Command(BaseCommand):
             del note['id']
             del note['course_id']
 
+            # we need to re-generate the slug
+            if 'slug' in note:
+                del note['slug']
+
             n = Note(**note)
 
             n.course = courses_by_old_id[old_course_id]
index 877997df0c8a0d9ef7fc2fdc8c8f0fcfb28a9515..8cf83b34738839793b78e49b9ac195769767c720 100644 (file)
@@ -78,9 +78,17 @@ class Note(models.Model):
         # TODO: If self.name isn't set, generate one based on uploaded_name
         # if we fail to set the Note.name earlier than this, use the saved filename
 
+        # only generate a slug if the name has been set, and slug hasn't
         if not self.slug and self.name:
-            # only generate a slug if the name has been set, and slug hasn't
-            self.slug = defaultfilters.slugify(self.name)
+            slug = defaultfilters.slugify(self.name)
+            cursor = Note.objects.filter(slug=slug)
+            # If there are no other notes with this slug, then the slug does not need an id
+            if cursor.count() == 0:
+                self.slug = slug
+            else:
+                super(Note, self).save(*args, **kwargs) # generate self.id
+                self.slug = defaultfilters.slugify("%s %s" % (self.name, self.id))
+            super(Note, self).save(*args, **kwargs)
 
         # Check if Note.uploaded_at is after Course.updated_at
         if self.uploaded_at and self.uploaded_at > self.course.updated_at: