UI for flagging courses
authorCharles Connell <charles@connells.org>
Thu, 2 Jan 2014 19:03:50 +0000 (14:03 -0500)
committerCharles Connell <charles@connells.org>
Thu, 2 Jan 2014 19:03:50 +0000 (14:03 -0500)
karmaworld/apps/courses/views.py
karmaworld/assets/js/course-detail.js [new file with mode: 0644]
karmaworld/templates/courses/course_detail.html
karmaworld/urls.py

index 6b7cfc6ee46d19b702e5f875533eddff1ac66d44..657d7565375be9658354f319d009b169a9ff5ca4 100644 (file)
@@ -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 (file)
index 0000000..c0149a5
--- /dev/null
@@ -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'
+      });
+    }
+  });
+});
index 72d0dc1b5ec58e420af0f4c42b776630042a7beb..47226557581a839a2fb3b76e7a621afc2931daaa 100644 (file)
@@ -4,8 +4,11 @@
 
 {% block pagescripts %}
   <script src="{{ STATIC_URL }}js/bootstrap-modal.js" ></script>
+  <script src="{{ STATIC_URL }}js/setup-ajax.js"></script>
+  <script src="{{ STATIC_URL }}js/course-detail.js" ></script>
   <script>
     var csrf_token = "{{ csrf_token }}";
+    var course_flag_url = "{% url 'flag_course' course.id %}"
   </script>
 {% endblock %}
 
           </div><!-- /course_meta -->
         </div>
       {% endif %}
+
+        <div class="row">
+        <div id="course_actions" class="large-3 medium-6 small-12 columns small-centered">
+          <div class="row">
+            <div class="small-12 column center">
+              <a href="#" id="flag-button"><img src="{{ STATIC_URL }}img/note_flag.png" alt="note_flag" width="25" height="35"/></a>
+              <a href="#" id="flag-button-disabled" class="hide"><img src="{{ STATIC_URL }}img/note_flag_disabled.png" alt="note_flag" width="25" height="35"/></a>
+            </div>
+          </div>
+        </div><!-- /note_actions -->
+      </div>
     </div><!-- /course_header -->
 
 
index a3d3982ca6ada01b3d0fc05551694138049a9084..7a88d16ff7a99aeef20ae385fd154601c1b4bd89 100644 (file)
@@ -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<pk>[\d]+)/$', thank_note, name='thank_note'),
     # Ajax endpoint to flag a note
     url(r'^ajax/note/flag/(?P<pk>[\d]+)/$', flag_note, name='flag_note'),
+    # Ajax endpoint to flag a course
+    url(r'^ajax/course/flag/(?P<pk>[\d]+)/$', flag_course, name='flag_course'),
 
     # Valid url cases to the Note page
     # a: school/course/id