Flag button working
authorCharles Connell <charles@connells.org>
Thu, 2 Jan 2014 15:54:41 +0000 (10:54 -0500)
committerCharles Connell <charles@connells.org>
Thu, 2 Jan 2014 15:55:14 +0000 (10:55 -0500)
karmaworld/apps/notes/views.py
karmaworld/assets/css/note_course_pages.css
karmaworld/assets/img/note_flag_disabled.png [new file with mode: 0644]
karmaworld/assets/js/note-detail.js
karmaworld/templates/notes/note_detail.html
karmaworld/urls.py

index bc43cb22bbdfb7643b9184d97a0dfe1fca7da6a1..647cb97f56f10620b6c52e4dd73fe9210cb0a631 100644 (file)
@@ -180,8 +180,8 @@ class NoteSearchView(ListView):
         return super(NoteSearchView, self).get_context_data(**kwargs)
 
 
-def thank_note(request, pk):
-    """Record that somebody has thanked a note."""
+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'}),
@@ -189,7 +189,7 @@ def thank_note(request, pk):
 
     try:
         note = Note.objects.get(pk=pk)
-        note.thanks += 1
+        note.__dict__[field] += 1
         note.save()
     except ObjectDoesNotExist:
         return HttpResponseNotFound(json.dumps({'status': 'fail', 'message': 'note id does not match a note'}),
@@ -197,3 +197,12 @@ def thank_note(request, pk):
 
     return HttpResponse(status=204)
 
+def thank_note(request, pk):
+    """Record that somebody has thanked a note."""
+    return ajaxIncrementBase(request, pk, 'thanks')
+
+def flag_note(request, pk):
+    """Record that somebody has flagged a note."""
+    return ajaxIncrementBase(request, pk, 'flags')
+
+
index 70d24e07bb9c9f88510cd56bbab9db56f1346344..ed752d8830b64a4c5586a9e617f0ee7a554524bb 100644 (file)
   margin-right: 10px;
 }
 
+#thank-button-disabled,
+#flag-button-disabled
+{
+  cursor: default;
+}
+
 /* COURSES */
 
 #course_meta, #school_meta
diff --git a/karmaworld/assets/img/note_flag_disabled.png b/karmaworld/assets/img/note_flag_disabled.png
new file mode 100644 (file)
index 0000000..b4fd85f
Binary files /dev/null and b/karmaworld/assets/img/note_flag_disabled.png differ
index 9dc0a78fc2a2eae98801a958d03a99f3d5628ca1..a5d175ca68b56a38d49d1f564a8ef74d50143148 100644 (file)
@@ -84,4 +84,22 @@ $(function() {
       type: 'POST'
     });
   });
+
+  $("#flag-button").click(function() {
+    event.preventDefault();
+
+    // 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: note_flag_url,
+      dataType: "json",
+      type: 'POST'
+    });
+  });
 });
index f617e8ec9e4ac9db2ebe530d9e012b459ddb248c..3e0d4bc9e59ba7a5599abf0c9aec788387fb866a 100644 (file)
@@ -12,6 +12,7 @@
 {% block pagescripts %}
   <script type="text/javascript">
     var note_thank_url = "{% url 'thank_note' note.id %}"
+    var note_flag_url = "{% url 'flag_note' note.id %}"
     var csrf_token = "{{ csrf_token }}";
   </script>
   <script src="{{ STATIC_URL }}js/setup-ajax.js"></script>
       </div>
 
       <div class="row">
-        <div id="note_actions" class="large-2 medium-4 small-6 columns small-centered">
+        <div id="note_actions" class="large-3 medium-6 small-12 columns small-centered">
           <div class="row">
-            <!--
             <div class="small-4 column">
-              <img src="{{ STATIC_URL }}img/note_flag.png" alt="note_flag" width="25" height="35"/>
+              <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 class="small-6 column">
+            <div class="small-4 column">
               <a href="#" id="thank-button"><img src="{{ STATIC_URL }}img/note_thank.png" alt="note_thank" width="34" height="34"/></a>
               <a href="#" id="thank-button-disabled" class="hide"><img src="{{ STATIC_URL }}img/note_thank_disabled.png" alt="note_thank" width="34" height="34"/></a>
             </div>
-            <div class="small-6 column">
+            <div class="small-4 column">
               <a href="{{ note.fp_file }}">
                 <img src="{{ STATIC_URL }}img/note_download.png" alt="note_download" width="51" height="36" />
               </a>
index 4d6846c6713abd5f4013a76bb8e227991000c307..a3d3982ca6ada01b3d0fc05551694138049a9084 100644 (file)
@@ -16,7 +16,7 @@ from karmaworld.apps.courses.views import school_list
 from karmaworld.apps.courses.views import school_course_list
 from karmaworld.apps.courses.views import school_course_instructor_list
 from karmaworld.apps.notes.models import Note
-from karmaworld.apps.notes.views import NoteView, thank_note, NoteSearchView
+from karmaworld.apps.notes.views import NoteView, thank_note, NoteSearchView, flag_note
 from karmaworld.apps.notes.views import RawNoteDetailView
 from karmaworld.apps.notes.views import PDFView
 from karmaworld.apps.moderation import moderator
@@ -78,6 +78,8 @@ urlpatterns = patterns('',
     ## NOTE MODEL
     # Ajax endpoint to thank a note
     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'),
 
     # Valid url cases to the Note page
     # a: school/course/id