Require authentication to set keywords, fixes #365
authorCharles Connell <charles@connells.org>
Wed, 30 Apr 2014 16:22:47 +0000 (12:22 -0400)
committerCharles Connell <charles@connells.org>
Wed, 30 Apr 2014 16:29:35 +0000 (12:29 -0400)
karmaworld/apps/notes/views.py
karmaworld/apps/quizzes/views.py
karmaworld/assets/js/note-detail.js
karmaworld/templates/notes/note_detail.html

index d1db236ebf1649efb0544ba891c6dba5e3cca2e6..97e88439fadec702f357aefb87724994b266d7aa 100644 (file)
@@ -16,7 +16,7 @@ from karmaworld.apps.quizzes.models import Keyword
 from karmaworld.apps.users.models import NoteKarmaEvent
 from karmaworld.utils.ajax_utils import *
 
-from django.http import HttpResponse, HttpResponseBadRequest
+from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from django.views.generic import DetailView, ListView
 from django.views.generic import FormView
 from django.views.generic import View
@@ -164,6 +164,9 @@ class NoteKeywordsView(FormView, SingleObjectMixin):
 
     def post(self, request, *args, **kwargs):
         self.object = self.get_object()
+        if not self.request.user.is_authenticated():
+            raise ValidationError("Only authenticated users may set keywords.")
+
         formset = self.form_class(request.POST)
         if formset.is_valid():
             self.keyword_form_valid(formset)
index 0379a07f7edfaa835a406f53669b14c4af09ae55..62b0b39d8ceecad6f2893646ddec36cb34657a8c 100644 (file)
@@ -150,6 +150,10 @@ def process_set_delete_keyword(request):
     definition = annotator_data['text']
     ranges = json.dumps(annotator_data['ranges'])
 
+    if not request.user.is_authenticated():
+        return HttpResponseForbidden(json.dumps({'status': 'fail', 'message': "Only authenticated users may set keywords"}),
+                                     mimetype="application/json")
+
     try:
         if request.method in ('POST', 'PUT'):
             set_keyword(annotation_uri, keyword, definition, ranges)
index 4fa08b7abc2ca6786490ee7286e1d7cd85e26429..81f2b96e394a1b4d3368bc177daa3f09e6ec3fea 100644 (file)
@@ -55,8 +55,8 @@ function writeNoteFrame(contents) {
   dstDoc.close();
 }
 
-function setupAnnotator(noteElement) {
-  noteElement.annotator();
+function setupAnnotator(noteElement, readOnly) {
+  noteElement.annotator({readOnly: readOnly});
   noteElement.annotator('addPlugin', 'Store', {
     prefix: '/ajax/annotations',
     loadFromSearch: {
@@ -218,7 +218,7 @@ function initNoteContentPage() {
   if ($('#note-markdown').length > 0) {
     var note_markdown = $('#note-markdown');
     note_markdown.html(marked(note_markdown.data('markdown')));
-    setupAnnotator(note_markdown);
+    setupAnnotator(note_markdown, !user_authenticated);
   } else {
     $.ajax(note_contents_url, {
       type: 'GET',
@@ -247,7 +247,7 @@ function initNoteContentPage() {
                   if ($('#page-container').length > 0) { \
                     document_selector = $('#page-container'); \
                   } \
-                  document_selector.annotator(); \
+                  document_selector.annotator({readOnly: " + !user_authenticated + "}); \
                   document_selector.annotator('addPlugin', 'Store', { \
                     prefix: '/ajax/annotations', \
                     loadFromSearch: { \
index 186a631215d1392578c15fe90218466dfd1eb3e7..0cb7f8aa8a71569fd4feadbba82a7a60bac194c7 100644 (file)
@@ -27,6 +27,7 @@
     var annotator_css_url = "{{ STATIC_URL }}css/annotator.min.css";
     var setup_ajax_url = "{{ STATIC_URL }}js/setup-ajax.js";
     var note_edit_url = "{% url 'edit_note' note.id %}";
+    var user_authenticated = {% if user.is_authenticated %}true{% else %}false{% endif %};
   </script>
   {% compress js %}
     <script src="{{ STATIC_URL }}js/setup-ajax.js"></script>
           <div id="keywords" class="content">
             <div class="row">
               <div class="small-12 columns">
-                <p id="keyword-intro">These key terms and definitions have been defined by KarmaNotes users.
-                You can edit them for accuracy and add more if you like.</p>
-                <p><button id="edit-keywords-button" class="museo700"><i class="fa fa-edit"></i> Edit Key Terms & Definitions</button></p>
+                {% if user.is_authenticated %}
+                  <p id="keyword-intro">These key terms and definitions have been defined by KarmaNotes users.
+                  You can edit them for accuracy and add more if you like.</p>
+                  <p><button id="edit-keywords-button" class="museo700"><i class="fa fa-edit"></i> Edit Key Terms & Definitions</button></p>
+                {% else %}
+                  <p id="keyword-intro">These key terms and definitions have been defined by KarmaNotes users.</p>
+                {% endif %}
                 <table id="keywords-data-table">
                   <thead>
                     <tr>