From: Charles Connell Date: Thu, 23 Jan 2014 15:44:21 +0000 (-0500) Subject: Badges on user dashboard, refactoring X-Git-Tag: release-20150131~215 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=cf911bd13ea50490d9a623ff1eb3eb19304ecfa3;p=oweals%2Fkarmaworld.git Badges on user dashboard, refactoring --- diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index 36df93f..bb12926 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -20,6 +20,9 @@ from karmaworld.apps.courses.models import Course from karmaworld.apps.courses.models import School from karmaworld.apps.notes.models import Note from karmaworld.apps.users.models import CourseKarmaEvent +from karmaworld.utils import ajax_increment, format_session_increment_field + +FLAG_FIELD = 'flags' class CourseListView(ListView, ModelFormMixin, ProcessFormView): @@ -73,6 +76,9 @@ class CourseDetailView(DetailView): # Include "Add Note" button in header kwargs['display_add_note'] = True + if self.request.session.get(format_session_increment_field(Course, self.object.id, FLAG_FIELD), False): + kwargs['already_flagged'] = True + return kwargs @@ -167,7 +173,7 @@ def school_course_instructor_list(request): school = School.objects.get(id__exact=_school_id) except (MultipleObjectsReturned, ObjectDoesNotExist): return HttpResponseBadRequest(json.dumps({'status': 'fail', - 'message': 'school id did not match exactly one school'}), + 'message': 'school id did not match exactly one school'}), mimetype="application/json") # Look up matching courses @@ -181,34 +187,6 @@ def school_course_instructor_list(request): mimetype="application/json") -def format_session_increment_field(id, field): - return field + '-' + str(id) - - -def ajaxIncrementBase(request, pk, field, event_processor=None): - """Increment a course'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'}), - mimetype="application/json") - - try: - course = Course.objects.get(pk=pk) - count = getattr(course, field) - setattr(course, field, count+1) - course.save() - - event_processor(request.user, course) - - # 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': 'course id does not match a course'}), - mimetype="application/json") - - return HttpResponse(status=204) - def process_course_flag_events(request_user, course): # Take a point away from person flagging this course if request_user.is_authenticated(): @@ -217,5 +195,5 @@ def process_course_flag_events(request_user, course): def flag_course(request, pk): """Record that somebody has flagged a note.""" - return ajaxIncrementBase(request, pk, 'flags', process_course_flag_events) + return ajax_increment(Course, request, pk, FLAG_FIELD, process_course_flag_events) diff --git a/karmaworld/apps/notes/views.py b/karmaworld/apps/notes/views.py index bc66cb1..09b7827 100644 --- a/karmaworld/apps/notes/views.py +++ b/karmaworld/apps/notes/views.py @@ -9,6 +9,7 @@ from django.core.exceptions import ObjectDoesNotExist from karmaworld.apps.courses.models import Course from karmaworld.apps.notes.search import SearchIndex from karmaworld.apps.users.models import NoteKarmaEvent +from karmaworld.utils.ajax_increment import * import os @@ -22,6 +23,7 @@ from django.views.generic.detail import SingleObjectMixin from karmaworld.apps.notes.models import Note from karmaworld.apps.notes.forms import NoteForm + logger = logging.getLogger(__name__) PDF_MIMETYPES = ( @@ -40,9 +42,6 @@ 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' @@ -64,10 +63,10 @@ class NoteDetailView(DetailView): if self.object.mimetype in PDF_MIMETYPES: kwargs['pdf_controls'] = True - if self.request.session.get(format_session_increment_field(self.object.id, THANKS_FIELD), False): + if self.request.session.get(format_session_increment_field(Note, 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): + if self.request.session.get(format_session_increment_field(Note, self.object.id, FLAG_FIELD), False): kwargs['already_flagged'] = True return super(NoteDetailView, self).get_context_data(**kwargs) @@ -230,32 +229,6 @@ class NoteSearchView(ListView): return super(NoteSearchView, self).get_context_data(**kwargs) -def ajaxIncrementBase(request, pk, field, event_processor=None): - """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'}), - mimetype="application/json") - - try: - # Increment counter - note = Note.objects.get(pk=pk) - count = getattr(note, field) - setattr(note, field, count+1) - note.save() - - event_processor(request.user, note) - - # 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") - - return HttpResponse(status=204) - - def process_note_thank_events(request_user, note): # Give points to the person who uploaded this note if note.user != request_user: @@ -264,7 +237,7 @@ def process_note_thank_events(request_user, note): def thank_note(request, pk): """Record that somebody has thanked a note.""" - return ajaxIncrementBase(request, pk, THANKS_FIELD, process_note_thank_events) + return ajax_increment(Note, request, pk, THANKS_FIELD, process_note_thank_events) def process_note_flag_events(request_user, note): @@ -279,6 +252,6 @@ def process_note_flag_events(request_user, note): def flag_note(request, pk): """Record that somebody has flagged a note.""" - return ajaxIncrementBase(request, pk, FLAG_FIELD, process_note_flag_events) + return ajax_increment(Note, request, pk, FLAG_FIELD, process_note_flag_events) diff --git a/karmaworld/apps/users/models.py b/karmaworld/apps/users/models.py index de80967..b13f479 100644 --- a/karmaworld/apps/users/models.py +++ b/karmaworld/apps/users/models.py @@ -65,7 +65,11 @@ class UserProfile(models.Model): for badge in self.BADGES: if points >= self.BADGE_THRESHOLDS[badge]: highest_badge = badge - return highest_badge + + if highest_badge is not self.NO_BADGE: + return self.BADGE_NAMES[highest_badge] + else: + return None def __unicode__(self): return self.user.__unicode__() diff --git a/karmaworld/apps/users/views.py b/karmaworld/apps/users/views.py index 7bd02f6..8d5feab 100644 --- a/karmaworld/apps/users/views.py +++ b/karmaworld/apps/users/views.py @@ -50,5 +50,7 @@ class ProfileView(TemplateView, MultipleObjectMixin): kwargs['object_list'] = result_list + kwargs['badge'] = self.request.user.get_profile().get_badge() + return super(ProfileView, self).get_context_data(**kwargs) diff --git a/karmaworld/assets/css/global.css b/karmaworld/assets/css/global.css index e4d2d97..0116f23 100644 --- a/karmaworld/assets/css/global.css +++ b/karmaworld/assets/css/global.css @@ -559,6 +559,21 @@ a.activity_target:hover font-family: "MuseoSlab-900"; } +.badge +{ + background: none repeat scroll 0 0 #FFFFCD; + border-top-color: rgb(238, 238, 238); + border-right-color: rgb(204, 204, 204); + border-bottom-color: rgb(204, 204, 204); + border-left-color: rgb(238, 238, 238); + border-style: solid; + border-width: 1px; + font-size: 14px; + padding: 4px 6px 4px 6px; + font-weight: bold; + line-height: 250%; +} + .course_meta_action { height: 36px; diff --git a/karmaworld/templates/courses/course_detail.html b/karmaworld/templates/courses/course_detail.html index 410d437..f539f02 100644 --- a/karmaworld/templates/courses/course_detail.html +++ b/karmaworld/templates/courses/course_detail.html @@ -65,8 +65,8 @@
- note_flag - note_flag + note_flag + note_flag
diff --git a/karmaworld/templates/user_profile.html b/karmaworld/templates/user_profile.html index 7d4bd52..c37dc6d 100644 --- a/karmaworld/templates/user_profile.html +++ b/karmaworld/templates/user_profile.html @@ -16,8 +16,11 @@
{% user_display user %} -
+ {% if badge %} + {{ badge }} + {% endif %}
+
diff --git a/karmaworld/utils/__init__.py b/karmaworld/utils/__init__.py new file mode 100644 index 0000000..e9c769d --- /dev/null +++ b/karmaworld/utils/__init__.py @@ -0,0 +1 @@ +from ajax_increment import * \ No newline at end of file diff --git a/karmaworld/utils/ajax_increment.py b/karmaworld/utils/ajax_increment.py new file mode 100644 index 0000000..d399443 --- /dev/null +++ b/karmaworld/utils/ajax_increment.py @@ -0,0 +1,35 @@ +import json +from django.core.exceptions import ObjectDoesNotExist +from django.http import HttpResponseBadRequest, HttpResponseNotFound, HttpResponse + + +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.""" + 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() + + if event_processor: + event_processor(request.user, note) + + # 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) +