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
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')
+
--- /dev/null
+
+$(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'
+ });
+ }
+ });
+});
{% 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 -->
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
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