From e76024911a9863c4f808cc5ef21792eea6f82c93 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Thu, 2 Jan 2014 14:03:50 -0500 Subject: [PATCH] UI for flagging courses --- karmaworld/apps/courses/views.py | 23 ++++++++++++++++++- karmaworld/assets/js/course-detail.js | 22 ++++++++++++++++++ .../templates/courses/course_detail.html | 14 +++++++++++ karmaworld/urls.py | 4 +++- 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 karmaworld/assets/js/course-detail.js diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index 6b7cfc6..657d756 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -8,7 +8,7 @@ import json from django.core.exceptions import MultipleObjectsReturned from django.core.exceptions import ObjectDoesNotExist -from django.http import HttpResponse, HttpResponseBadRequest +from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.views.generic import DetailView from django.views.generic import TemplateView from django.views.generic.edit import ProcessFormView @@ -180,3 +180,24 @@ def school_course_instructor_list(request): return HttpResponse(json.dumps({'status':'success', 'instructors': instructors}), mimetype="application/json") +def ajaxIncrementBase(request, pk, field): + """Increment a note's field by one.""" + if not (request.method == 'POST' and request.is_ajax()): + # return that the api call failed + return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'must be a POST ajax request'}), + mimetype="application/json") + + try: + note = Course.objects.get(pk=pk) + note.__dict__[field] += 1 + note.save() + except ObjectDoesNotExist: + return HttpResponseNotFound(json.dumps({'status': 'fail', 'message': 'note id does not match a note'}), + mimetype="application/json") + + return HttpResponse(status=204) + +def flag_course(request, pk): + """Record that somebody has flagged a note.""" + return ajaxIncrementBase(request, pk, 'flags') + diff --git a/karmaworld/assets/js/course-detail.js b/karmaworld/assets/js/course-detail.js new file mode 100644 index 0000000..c0149a5 --- /dev/null +++ b/karmaworld/assets/js/course-detail.js @@ -0,0 +1,22 @@ + +$(function() { + $("#flag-button").click(function(event) { + event.preventDefault(); + + if (confirm('Do you wish to flag this course for deletion?')) { + // disable thank button so it can't + // be pressed again + $(this).hide(); + $('#flag-button-disabled').show(); + $(this).unbind('click'); + + // tell server that somebody thanked + // this note + $.ajax({ + url: course_flag_url, + dataType: "json", + type: 'POST' + }); + } + }); +}); diff --git a/karmaworld/templates/courses/course_detail.html b/karmaworld/templates/courses/course_detail.html index 72d0dc1..4722655 100644 --- a/karmaworld/templates/courses/course_detail.html +++ b/karmaworld/templates/courses/course_detail.html @@ -4,8 +4,11 @@ {% block pagescripts %} + + {% endblock %} @@ -49,6 +52,17 @@ {% endif %} + +
+
+
+
+ note_flag + note_flag +
+
+
+
diff --git a/karmaworld/urls.py b/karmaworld/urls.py index a3d3982..7a88d16 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -9,7 +9,7 @@ from django.conf.urls.defaults import patterns, include, url from django.views.generic.simple import direct_to_template from karmaworld.apps.courses.models import Course -from karmaworld.apps.courses.views import AboutView +from karmaworld.apps.courses.views import AboutView, flag_course from karmaworld.apps.courses.views import CourseDetailView from karmaworld.apps.courses.views import CourseListView from karmaworld.apps.courses.views import school_list @@ -80,6 +80,8 @@ urlpatterns = patterns('', url(r'^ajax/note/thank/(?P[\d]+)/$', thank_note, name='thank_note'), # Ajax endpoint to flag a note url(r'^ajax/note/flag/(?P[\d]+)/$', flag_note, name='flag_note'), + # Ajax endpoint to flag a course + url(r'^ajax/course/flag/(?P[\d]+)/$', flag_course, name='flag_course'), # Valid url cases to the Note page # a: school/course/id -- 2.25.1