From 675f7b061f2a96f3ce87ac92bda5c2f8554a0578 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Fri, 24 Jan 2014 00:35:54 -0500 Subject: [PATCH] Require login to download a note, shift points around #302 --- karmaworld/apps/notes/views.py | 12 +++++++ karmaworld/apps/users/models.py | 9 ++++- .../assets/img/note_download_disabled.png | Bin 0 -> 758 bytes karmaworld/assets/js/note-detail.js | 14 ++++++++ karmaworld/templates/base.html | 2 ++ karmaworld/templates/courses/course_list.html | 2 -- karmaworld/templates/notes/note_detail.html | 29 ++++++++++++++-- karmaworld/urls.py | 4 ++- karmaworld/utils/ajax_increment.py | 31 +++++++++++------- 9 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 karmaworld/assets/img/note_download_disabled.png 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 0000000000000000000000000000000000000000..f9d94ded269a0c917b719bb2cafc282ad14241e3 GIT binary patch literal 758 zcmVbL*^0)t6J zK~z}7?U+4p6G0G$A43WXbQ~R})1fqWx^%360R{;X3d^P;!VR&gs0byMi4=iIgCkdw zknle^r=!Cb5`xa|C~Yee2nm_yU1@Z(vE7~nVXb^-ZYYM$;TCd>jCy$AM|RNs?+qVCL7ptGP(M|#Oh0D+5s z0h_>ZPL(0BDZS)nv%p2)fUP;jw%s3n>q&WYX1CG0J;zHpBB}BaZk>qoC#di1R7$S;*(v@%%Cc8b%89GTrlSTq*e2x zlv(zVX8Jvog1M-7rc^3D2X@5w^lo?|-T`$Wuadn6v^7=)R@HaFdaOT|Yyt3VWMX|! zp~frXy$=KNpX9@JjX5CF+yH0+HJh3XQm6xcIr6~^0%h^*Kvy+ZD8qRJzHMwe|zV$fg@wHr_BlHC{7!4w57f; zNQ<)50d7zDz-!6x%A`9J>gf80x6i(e_kbrd*wOA1JDG;$W}Qwi1~}9h0x-~6af*Hj z3|3-ComZMp#^XN?CJ!)ZpB1G(QHal&C8Vqb{p8a zoXh1_rIZD}o3Q$+g|ff~@DX@m3}t{D`ZLqfCLBu6hu~2x#t)P<{CZg<)0IvLJQIKo o$=s1#sCB;rH^pB8UIM#+0Lf>wkj5Dvz5oCK07*qoM6N<$f-O~1i2wiq literal 0 HcmV?d00001 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) -- 2.25.1