From: Jacob Hilker Date: Sun, 2 Feb 2014 21:36:24 +0000 (-0500) Subject: redirect to the new page and update the slug on name changes X-Git-Tag: release-20150131~178^2~5 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=f9d44c70c9457969fa05e09c8b03af4bd7a08b0c;p=oweals%2Fkarmaworld.git redirect to the new page and update the slug on name changes --- diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index ef86dc1..adbc3e2 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -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",) diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index 714047c..4be59b8 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -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', diff --git a/karmaworld/assets/js/course-detail.js b/karmaworld/assets/js/course-detail.js index 0149281..443e977 100644 --- a/karmaworld/assets/js/course-detail.js +++ b/karmaworld/assets/js/course-detail.js @@ -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();