""" 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",)
"""
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',
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();