diff --git a/lms/djangoapps/django_comment_client/helpers.py b/lms/djangoapps/django_comment_client/helpers.py index bb3bac0926..a168f53021 100644 --- a/lms/djangoapps/django_comment_client/helpers.py +++ b/lms/djangoapps/django_comment_client/helpers.py @@ -1,6 +1,10 @@ from django.core.urlresolvers import reverse from mitxmako.shortcuts import render_to_string -from utils import render_mustache +from utils import * +from mustache_helpers import mustache_helpers +from functools import partial + +import pystache_custom as pystache import urllib def pluralize(singular_term, count): @@ -14,20 +18,17 @@ def show_if(text, condition): else: return '' -def close_thread_text(content): - if content.get('closed'): - return 'Re-open thread' - else: - return 'Close thread' - -def url_for_user(course_id, user_id): - return reverse('django_comment_client.forum.views.user_profile', args=[course_id, user_id]) - -def url_for_tags(course_id, tags): - return reverse('django_comment_client.forum.views.forum_form_discussion', args=[course_id]) + '?' + urllib.urlencode({'tags': ",".join(tags)}) - -def render_content(content): - context = { - 'content': content, +def render_content(content, additional_context={}): + content_info = { + 'displayed_title': content.get('highlighted_title') or content.get('title', ''), + 'displayed_body': content.get('highlighted_body') or content.get('body', ''), + 'raw_tags': ','.join(content.get('tags', [])), } + context = { + 'content': merge_dict(content, content_info), + content['type']: True, + } + context = merge_dict(context, additional_context) + partial_mustache_helpers = {k: partial(v, content) for k, v in mustache_helpers.items()} + context = merge_dict(context, partial_mustache_helpers) return render_mustache('discussion/_content.mustache', context) diff --git a/lms/djangoapps/django_comment_client/mustache_helpers.py b/lms/djangoapps/django_comment_client/mustache_helpers.py new file mode 100644 index 0000000000..48a38be29b --- /dev/null +++ b/lms/djangoapps/django_comment_client/mustache_helpers.py @@ -0,0 +1,28 @@ +from django.core.urlresolvers import reverse +import urllib + +def pluralize(content, text): + num, word = text.split(' ') + if int(num or '0') >= 2: + return num + ' ' + word + 's' + else: + return num + ' ' + word + +def url_for_user(content, user_id): + return reverse('django_comment_client.forum.views.user_profile', args=[content['course_id'], user_id]) + +def url_for_tags(content, tags): # assume that tags is in the format u'a, b, c' + return reverse('django_comment_client.forum.views.forum_form_discussion', args=[content['course_id']]) + '?' + urllib.urlencode({'tags': tags}) + +def close_thread_text(content): + if content.get('closed'): + return 'Re-open thread' + else: + return 'Close thread' + +mustache_helpers = { + 'pluralize': pluralize, + 'url_for_tags': url_for_tags, + 'url_for_user': url_for_user, + 'close_thread_text': close_thread_text, +} diff --git a/lms/templates/discussion/_content.mustache b/lms/templates/discussion/_content.mustache index 1b860af1a8..cdad862dac 100644 --- a/lms/templates/discussion/_content.mustache +++ b/lms/templates/discussion/_content.mustache @@ -10,7 +10,9 @@