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):
# 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
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
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():
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)
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
from karmaworld.apps.notes.models import Note
from karmaworld.apps.notes.forms import NoteForm
+
logger = logging.getLogger(__name__)
PDF_MIMETYPES = (
return True
return False
-def format_session_increment_field(id, field):
- return field + '-' + str(id)
-
THANKS_FIELD = 'thanks'
FLAG_FIELD = 'flags'
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)
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:
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):
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)
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__()
kwargs['object_list'] = result_list
+ kwargs['badge'] = self.request.user.get_profile().get_badge()
+
return super(ProfileView, self).get_context_data(**kwargs)
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;
<div id="course_actions" class="large-3 medium-6 small-12 columns small-centered">
<div class="row">
<div class="small-12 column center">
- <a href="#" id="flag-button"><img src="{{ STATIC_URL }}img/note_flag.png" alt="note_flag" width="25" height="35"/></a>
- <a href="#" id="flag-button-disabled" class="hide"><img src="{{ STATIC_URL }}img/note_flag_disabled.png" alt="note_flag" width="25" height="35"/></a>
+ <a href="#" id="flag-button" {% if already_flagged %} class="hide" {% endif %}><img src="{{ STATIC_URL }}img/note_flag.png" alt="note_flag" width="25" height="35"/></a>
+ <a href="#" id="flag-button-disabled" {% if not already_flagged %} class="hide" {% endif %}><img src="{{ STATIC_URL }}img/note_flag_disabled.png" alt="note_flag" width="25" height="35"/></a>
</div>
</div>
</div><!-- /note_actions -->
<div class="row">
<div class="small-10 columns small-centered center header_title">
{% user_display user %}
- </div>
+ {% if badge %}
+ <span class="badge">{{ badge }}</span>
+ {% endif %}
</div>
+ </div>
<div class="row">
<div class="small-12 medium-8 large-4 columns small-centered">
<div class="row">
--- /dev/null
+from ajax_increment import *
\ No newline at end of file
--- /dev/null
+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)
+