From 624c1b42cf124eb2611fbb9ebc5b880eefdc4254 Mon Sep 17 00:00:00 2001 From: Charles Holbrow Date: Mon, 25 Mar 2013 17:14:54 -0400 Subject: [PATCH] fixing bug on import allowing two notes with the same name to create non-unique slugs --- .../apps/courses/management/commands/import_json.py | 4 ++++ karmaworld/apps/notes/models.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) 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: -- 2.25.1