From: Charles Connell Date: Fri, 24 Jan 2014 05:35:54 +0000 (-0500) Subject: Require login to download a note, shift points around #302 X-Git-Tag: release-20150131~205 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=675f7b061f2a96f3ce87ac92bda5c2f8554a0578;p=oweals%2Fkarmaworld.git Require login to download a note, shift points around #302 --- diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index 2a19ae9..5093b27 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -255,3 +255,15 @@ def flag_note(request, pk): return ajax_increment(Note, request, pk, FLAG_FIELD, process_note_flag_events) +def process_downloaded_note(request_user, note): + """Record that somebody has downloaded a note""" + if request_user.is_authenticated() and request_user != note.user: + NoteKarmaEvent.create_event(request_user, note, NoteKarmaEvent.DOWNLOADED_NOTE) + NoteKarmaEvent.create_event(note.user, note, NoteKarmaEvent.HAD_NOTE_DOWNLOADED) + + +def downloaded_note(request, pk): + """Record that somebody has flagged a note.""" + return ajax_base(Note, request, pk, process_downloaded_note) + + diff --git a/karmaworld/apps/users/models.py b/karmaworld/apps/users/models.py index a8090af..a2e549f 100644 --- a/karmaworld/apps/users/models.py +++ b/karmaworld/apps/users/models.py @@ -136,12 +136,17 @@ class NoteKarmaEvent(BaseKarmaEvent): NOTE_DELETED = 'deleted' GIVE_FLAG = 'give_flag' GET_FLAGGED = 'get_flagged' + DOWNLOADED_NOTE = 'downloaded' + HAD_NOTE_DOWNLOADED = 'was_downloaded' + EVENT_TYPE_CHOICES = ( (UPLOAD, "You uploaded a note"), (THANKS, "You received a thanks for your note"), (NOTE_DELETED, "Your note was deleted"), (GIVE_FLAG, "You flagged a note"), (GET_FLAGGED, "Your note was flagged as spam"), + (DOWNLOADED_NOTE, "You downloaded a note"), + (HAD_NOTE_DOWNLOADED, "Your note was downloaded"), ) note = models.ForeignKey('notes.Note') event_type = models.CharField(max_length=15, choices=EVENT_TYPE_CHOICES) @@ -151,7 +156,9 @@ class NoteKarmaEvent(BaseKarmaEvent): THANKS: 1, NOTE_DELETED: -5, GIVE_FLAG: -1, - GET_FLAGGED: -100 + GET_FLAGGED: -100, + DOWNLOADED_NOTE: -2, + HAD_NOTE_DOWNLOADED: 2, } def get_message(self): diff --git a/karmaworld/assets/img/note_download_disabled.png b/karmaworld/assets/img/note_download_disabled.png new file mode 100644 index 0000000..f9d94de Binary files /dev/null and b/karmaworld/assets/img/note_download_disabled.png differ diff --git a/karmaworld/assets/js/note-detail.js b/karmaworld/assets/js/note-detail.js index 14d3a58..da96df8 100644 --- a/karmaworld/assets/js/note-detail.js +++ b/karmaworld/assets/js/note-detail.js @@ -104,4 +104,18 @@ $(function() { }); } }); + + $("#note-download-button").click(function(event) { + // disable handler so it won't be run again + $(this).unbind('click'); + + // tell server that somebody downloaded + // this note + $.ajax({ + url: note_downloaded_url, + dataType: "json", + type: 'POST', + async: false + }); + }); }); diff --git a/karmaworld/templates/base.html b/karmaworld/templates/base.html index c1ced84..11dd804 100644 --- a/karmaworld/templates/base.html +++ b/karmaworld/templates/base.html @@ -11,6 +11,7 @@ + @@ -52,6 +53,7 @@ {% endblock %} + diff --git a/karmaworld/templates/courses/course_list.html b/karmaworld/templates/courses/course_list.html index 3d3ee2c..7d6b4f1 100644 --- a/karmaworld/templates/courses/course_list.html +++ b/karmaworld/templates/courses/course_list.html @@ -10,7 +10,6 @@ - {% endblock %} {% block pagescripts %} @@ -157,5 +156,4 @@ $(document).ready(function() { {% endblock %} {% block bodyscripts %} - {% endblock bodyscripts %} \ No newline at end of file diff --git a/karmaworld/templates/notes/note_detail.html b/karmaworld/templates/notes/note_detail.html index df01226..187031c 100644 --- a/karmaworld/templates/notes/note_detail.html +++ b/karmaworld/templates/notes/note_detail.html @@ -13,6 +13,7 @@ @@ -93,9 +94,30 @@ note_thank
- - note_download - + {% if user.is_authenticated %} + + note_download + + {% else %} + note_download + {% endif %}
@@ -171,3 +193,4 @@ {% endblock %} + diff --git a/karmaworld/urls.py b/karmaworld/urls.py index 6cbffeb..ee571f2 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -15,7 +15,7 @@ from karmaworld.apps.courses.views import CourseListView 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.views import NoteView, thank_note, NoteSearchView, flag_note +from karmaworld.apps.notes.views import NoteView, thank_note, NoteSearchView, flag_note, downloaded_note from karmaworld.apps.notes.views import RawNoteDetailView from karmaworld.apps.notes.views import PDFView from karmaworld.apps.moderation import moderator @@ -96,6 +96,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 record that somebody downloaded a note + url(r'^ajax/note/downloaded/(?P[\d]+)/$', downloaded_note, name='downloaded_note'), # Ajax endpoint to flag a course url(r'^ajax/course/flag/(?P[\d]+)/$', flag_course, name='flag_course'), diff --git a/karmaworld/utils/ajax_increment.py b/karmaworld/utils/ajax_increment.py index d399443..2e6e466 100644 --- a/karmaworld/utils/ajax_increment.py +++ b/karmaworld/utils/ajax_increment.py @@ -7,29 +7,36 @@ def format_session_increment_field(cls, id, field): return cls.__name__ + '-' + field + '-' + str(id) -def ajax_increment(cls, request, pk, field, event_processor=None): - """Increment a note's field by one.""" +def ajax_base(cls, request, pk, event_processor): + """Handle an AJAX request""" 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: - # Increment counter - note = cls.objects.get(pk=pk) - count = getattr(note, field) - setattr(note, field, count+1) - note.save() + obj = cls.objects.get(pk=pk) + event_processor(request.user, obj) + + except ObjectDoesNotExist: + return HttpResponseNotFound(json.dumps({'status': 'fail', 'message': 'id does not match a ' + cls.__name__}), + mimetype="application/json") + + return HttpResponse(status=204) + + +def ajax_increment(cls, request, pk, field, event_processor=None): + def ajax_increment_work(request_user, obj): + count = getattr(obj, field) + setattr(obj, field, count+1) + obj.save() if event_processor: - event_processor(request.user, note) + event_processor(request.user, obj) # Record that user has performed this, to prevent # them from doing it again request.session[format_session_increment_field(cls, pk, field)] = True - except ObjectDoesNotExist: - return HttpResponseNotFound(json.dumps({'status': 'fail', 'message': 'note id does not match a note'}), - mimetype="application/json") - return HttpResponse(status=204) + return ajax_base(cls, request, pk, ajax_increment_work)