redirect to the new page and update the slug on name changes
authorJacob Hilker <hilker.j@gmail.com>
Sun, 2 Feb 2014 21:36:24 +0000 (16:36 -0500)
committerJacob Hilker <hilker.j@gmail.com>
Sun, 2 Feb 2014 21:36:24 +0000 (16:36 -0500)
karmaworld/apps/courses/models.py
karmaworld/apps/courses/views.py
karmaworld/assets/js/course-detail.js

index ef86dc1c96a2707f98e3fcf00e2891fcd0286702..adbc3e2d3988e5ec28b4e6fd9de9ed5e2a48c39c 100644 (file)
@@ -254,13 +254,16 @@ class Course(models.Model):
         """ Save school and generate a slug if one doesn't exist """
         super(Course, self).save(*args, **kwargs) # generate a self.id
         if not self.slug:
-            self.slug = slugify(u"%s %s" % (self.name, self.id))
-            self.save() # Save the slug
+            self.set_slug()
 
     def get_updated_at_string(self):
         """ return the formatted style for datetime strings """
         return self.updated_at.strftime("%I%p // %a %b %d %Y")
 
+    def set_slug(self):
+        self.slug = slugify(u"%s %s" % (self.name, self.id))
+        self.save() # Save the slug
+
     @staticmethod
     def autocomplete_search_fields():
         return ("name__icontains",)
index 714047cecc1c1c07887c472334dce25485c79177..4be59b844ba7a28ea596159534a2bae399d6b86d 100644 (file)
@@ -205,20 +205,22 @@ def edit_course(request, pk):
     """
     Saves the edited course content
     """
-    course = Course.objects.get(pk=pk)
-    course_form = CourseForm(request.POST or None, instance=course)
-
     if request.method == "POST" and request.is_ajax():
-        if course_form.is_valid():
+        course = Course.objects.get(pk=pk)
+        original_name = course.name
+        course_form = CourseForm(request.POST or None, instance=course)
 
-            # This will fail if name and school match one that already exists
-            # Same happens for add, should add specialized validation?
+        if course_form.is_valid():
             course_form.save()
 
-            # Make a helper for this? Kinda Ugly...
             course_json = serializers.serialize('json', [course,])
-            return HttpResponse(json.dumps(json.loads(course_json)[0]),
-                                mimetype="application/json")
+            resp = json.loads(course_json)[0]
+
+            if (course.name != original_name):
+                course.set_slug()
+                resp['fields']['new_url'] = course.get_absolute_url()
+
+            return HttpResponse(json.dumps(resp), mimetype="application/json")
         else:
             print course_form.errors
             return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'Validation error',
index 0149281774748c301329d6c7976b6e013c46d71e..443e977d1bd1a932ed816a240c29d493f776ed64 100644 (file)
@@ -30,6 +30,10 @@ $(function() {
       data: $('#edit-course-form').children().serialize(),
       type: 'POST',
       success: function(data) {
+        if (data.fields.new_url) {
+          window.location.href = data.fields.new_url;
+        }
+
         // We might want to use a template here instead of rehashing logic
         // on both the client and server side
         $('#edit-course-form').slideUp();