From: Charles Holbrow Date: Mon, 25 Mar 2013 21:14:54 +0000 (-0400) Subject: fixing bug on import allowing two notes with the same name to create non-unique slugs X-Git-Tag: release-20150131~468 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=624c1b42cf124eb2611fbb9ebc5b880eefdc4254;p=oweals%2Fkarmaworld.git fixing bug on import allowing two notes with the same name to create non-unique slugs --- diff --git a/karmaworld/apps/courses/management/commands/import_json.py b/karmaworld/apps/courses/management/commands/import_json.py index 501f1a4..c7bb7a4 100644 --- a/karmaworld/apps/courses/management/commands/import_json.py +++ b/karmaworld/apps/courses/management/commands/import_json.py @@ -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] diff --git a/karmaworld/apps/notes/models.py b/karmaworld/apps/notes/models.py index 877997d..8cf83b3 100644 --- a/karmaworld/apps/notes/models.py +++ b/karmaworld/apps/notes/models.py @@ -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: