From 94b63acb8676fe52e61d6f5cc419cdfe982b2989 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Sat, 4 Jan 2014 17:27:10 -0500 Subject: [PATCH] Remember who thanked or flagged notes by session cookies #250 --- karmaworld/apps/notes/views.py | 20 ++++++++++++++++++-- karmaworld/settings/common.py | 6 ++++++ karmaworld/templates/notes/note_detail.html | 8 ++++---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index 1928721..43f42a1 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -39,6 +39,11 @@ def is_ppt(self): return True return False +def format_session_increment_field(id, field): + return field + '-' + str(id) + +THANKS_FIELD = 'thanks' +FLAG_FIELD = 'flags' class NoteDetailView(DetailView): """ Class-based view for the note html page """ @@ -59,6 +64,12 @@ class NoteDetailView(DetailView): if self.object.mimetype == 'application/pdf': kwargs['pdf_controls'] = True + if self.request.session.get(format_session_increment_field(self.object.id, THANKS_FIELD), False): + kwargs['already_thanked'] = True + + if self.request.session.get(format_session_increment_field(self.object.id, FLAG_FIELD), False): + kwargs['already_flagged'] = True + return super(NoteDetailView, self).get_context_data(**kwargs) @@ -213,9 +224,14 @@ def ajaxIncrementBase(request, pk, field): mimetype="application/json") try: + # Increment counter note = Note.objects.get(pk=pk) note.__dict__[field] += 1 note.save() + + # Record that user has performed this, to prevent + # them from doing it again + request.session[format_session_increment_field(pk, field)] = True except ObjectDoesNotExist: return HttpResponseNotFound(json.dumps({'status': 'fail', 'message': 'note id does not match a note'}), mimetype="application/json") @@ -224,10 +240,10 @@ def ajaxIncrementBase(request, pk, field): def thank_note(request, pk): """Record that somebody has thanked a note.""" - return ajaxIncrementBase(request, pk, 'thanks') + return ajaxIncrementBase(request, pk, THANKS_FIELD) def flag_note(request, pk): """Record that somebody has flagged a note.""" - return ajaxIncrementBase(request, pk, 'flags') + return ajaxIncrementBase(request, pk, FLAG_FIELD) diff --git a/karmaworld/settings/common.py b/karmaworld/settings/common.py index 0e7b0a5..bfc6fb1 100644 --- a/karmaworld/settings/common.py +++ b/karmaworld/settings/common.py @@ -283,6 +283,12 @@ COMPRESS_JS_FILTERS = [ ] ########## END COMPRESSION CONFIGURATION +########## SESSION CONFIGURATION + +SESSION_COOKIE_AGE = 63072000 # 2 years in seconds + +########## END SESSION CONFIGURATION + ########## TAGGIT CONFIGURATION # From https://github.com/yedpodtrzitko/django-taggit diff --git a/karmaworld/templates/notes/note_detail.html b/karmaworld/templates/notes/note_detail.html index 125c0c6..447d1c7 100644 --- a/karmaworld/templates/notes/note_detail.html +++ b/karmaworld/templates/notes/note_detail.html @@ -50,12 +50,12 @@
- note_flag - note_flag + note_flag + note_flag
- note_thank - note_thank + note_thank + note_thank