From: Charles Connell Date: Tue, 4 Feb 2014 21:46:26 +0000 (-0800) Subject: Merge pull request #314 from FinalsClub/208-edit-course-properties-main X-Git-Tag: release-20150131~178 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=11ced2c2929d2d600167088a1b07c19ad19162c4;p=oweals%2Fkarmaworld.git Merge pull request #314 from FinalsClub/208-edit-course-properties-main 208 - Edit course properties --- 11ced2c2929d2d600167088a1b07c19ad19162c4 diff --cc karmaworld/apps/courses/views.py index 191793d,541eb4e..7ef80eb --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@@ -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") diff --cc karmaworld/assets/js/course-detail.js index d4ea78b,70bc420..94871a6 --- a/karmaworld/assets/js/course-detail.js +++ b/karmaworld/assets/js/course-detail.js @@@ -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 = $('', {'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($('', { class: 'validation_error', text: value })); + }); + } + + if (errors.instructor_email) { + $.each(errors.instructor_email, function(index, value) { + $('#id_instructor_email').parent().children('legend').append($('', { class: 'validation_error', text: value })); + }); + } + + if (errors.url) { + $.each(errors.url, function(index, value) { + $('#id_url').parent().children('legend').append($('', { class: 'validation_error' , text: value })); + }); + } + } + } + }); + }); + + KARMAWORLD.Course.initCourseNameAutocomplete({}); + KARMAWORLD.Course.initInstructorNameAutocomplete({}); + }); diff --cc karmaworld/utils/ajax_increment.py index 8484480,9e70555..f9e772f --- a/karmaworld/utils/ajax_increment.py +++ b/karmaworld/utils/ajax_increment.py @@@ -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) -