Merge pull request #314 from FinalsClub/208-edit-course-properties-main
authorCharles Connell <charles@connells.org>
Tue, 4 Feb 2014 21:46:26 +0000 (13:46 -0800)
committerCharles Connell <charles@connells.org>
Tue, 4 Feb 2014 21:46:26 +0000 (13:46 -0800)
208 - Edit course properties

1  2 
karmaworld/apps/courses/views.py
karmaworld/apps/users/models.py
karmaworld/assets/js/course-detail.js
karmaworld/settings/common.py
karmaworld/utils/ajax_increment.py

index 191793d463b58bb34dbb308fdd164bc674362e29,541eb4ed3cd13d1f7e1672915f39cf7d36b54332..7ef80ebf7e1685f774a5d17aedcd0d1197741eb7
@@@ -204,5 -199,32 +204,32 @@@ def process_course_flag_events(request_
  
  def flag_course(request, pk):
      """Record that somebody has flagged a note."""
 -    return ajax_increment(Course, request, pk, FLAG_FIELD, process_course_flag_events)
 +    return ajax_increment(Course, request, pk, FLAG_FIELD, USER_PROFILE_FLAGS_FIELD, process_course_flag_events)
  
+ def edit_course(request, pk):
+     """
+     Saves the edited course content
+     """
+     if request.method == "POST" and request.is_ajax():
+         course = Course.objects.get(pk=pk)
+         original_name = course.name
+         course_form = CourseForm(request.POST or None, instance=course)
+         if course_form.is_valid():
+             course_form.save()
+             course_json = serializers.serialize('json', [course,])
+             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:
+             return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'Validation error',
+                                           'errors': course_form.errors}),
+                                           mimetype="application/json")
+     else:
+         return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'Invalid request'}),
+                                       mimetype="application/json")
Simple merge
index d4ea78bf2a8d8a37de99d0b5701a071a8d76af34,70bc4203a9dba37d7d1cdeda08c02319c0906bf5..94871a6d7338800ba55a024fd60b089428fd0163
@@@ -14,10 -13,87 +13,88 @@@ $(function() 
        // this note
        $.ajax({
          url: course_flag_url,
-         dataType: "json",
+         dataType: 'json',
          type: 'POST'
        });
 +
      }
    });
+   $('#edit-button').click(function(event) {
+     $('#edit-course-form').slideToggle();
+   });
+   $('#edit-save-btn').click(function(event) {
+     $.ajax({
+       url: course_edit_url,
+       dataType: 'json',
+       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();
+         $('.validation_error').remove()
+         $('#course_form_errors').empty();
+         $('#course_name').text(data.fields.name);
+         $('#course_instructor_name').text(data.fields.instructor_name);
+         var $externalLinkSquare = $('<i>', {'class': 'fa fa-external-link-square'});
+         $('#course_url').text(data.fields.url.slice(0, 50) + ' ');
+         $('#course_url').append($externalLinkSquare);
+         $('#course_url').attr('href', data.fields.url);
+         if (data.fields.url === '') {
+           $('#course_link').parent().hide();
+         } else {
+           $('#course_link').parent().show();
+         }
+       },
+       error: function(resp) {
+         var json;
+         try {
+           json = JSON.parse(resp.responseText);
+         } catch(e) {
+           json = { message: 'Unknown Error' };
+         }
+         var errors = json.errors;
+         // Delete all errors that currently exist
+         $('.validation_error').remove()
+         $('#course_form_errors').empty();
+         // Failed responses with no errors -> display message
+         if (!errors) {
+           $('#course_form_errors').text(json.message);
+         } else {
+           // Ugly, be works.  Could look into backbone or something similar to make it cleaner.
+           if (errors.__all__) {
+             $.each(errors.__all__, function(index, value) {
+               $('#course_form_errors').append($('<span>', { class: 'validation_error', text: value }));
+             });
+           }
+           if (errors.instructor_email) {
+             $.each(errors.instructor_email, function(index, value) {
+               $('#id_instructor_email').parent().children('legend').append($('<span>', { class: 'validation_error', text: value }));
+             });
+           }
+           if (errors.url) {
+             $.each(errors.url, function(index, value) {
+               $('#id_url').parent().children('legend').append($('<span>', { class: 'validation_error' , text: value }));
+             });
+           }
+         }
+       }
+     });
+   });
+   KARMAWORLD.Course.initCourseNameAutocomplete({});
+   KARMAWORLD.Course.initInstructorNameAutocomplete({});
  });
Simple merge
index 8484480853eaf4aacadbced3d7ca2f89b50c36f5,9e705550a24ac5c2a5403569f998cfe80daaf5ff..f9e772f7f11916fb5516f5f8fbfc09b6adc75d4e
@@@ -36,9 -36,6 +36,8 @@@ def ajax_increment(cls, request, pk, fi
  
          # Record that user has performed this, to prevent
          # them from doing it again
 -        request.session[format_session_increment_field(cls, pk, field)] = True
 +        if user_profile_field:
 +            getattr(request_user.get_profile(), user_profile_field).add(obj)
 +            obj.save()
  
      return ajax_base(cls, request, pk, ajax_increment_work)