mustache_helpers.py 1.05 KB
Newer Older
1 2
from .utils import url_for_tags as _url_for_tags

3 4 5
import django.core.urlresolvers as urlresolvers
import sys
import inspect
6

7 8
# This method is used to pluralize the words "discussion" and "comment"
# which is why you need to tack on an "s" for the case of 0 or two or more.
Calen Pennington committed
9 10


11 12
def pluralize(content, text):
    num, word = text.split(' ')
Rocky Duan committed
13 14
    num = int(num or '0')
    if num >= 2 or num == 0:
15
        return word + 's'
16
    else:
17
        return word
18

Calen Pennington committed
19

20
def url_for_user(content, user_id):
21
    return urlresolvers.reverse('django_comment_client.forum.views.user_profile', args=[content['course_id'], user_id])
22

Calen Pennington committed
23 24

def url_for_tags(content, tags):   # assume that attribute 'tags' is in the format u'a, b, c'
25
    return _url_for_tags(content['course_id'], tags)
26

Calen Pennington committed
27

28 29 30 31 32 33
def close_thread_text(content):
    if content.get('closed'):
        return 'Re-open thread'
    else:
        return 'Close thread'

34 35 36 37
current_module = sys.modules[__name__]
all_functions = inspect.getmembers(current_module, inspect.isfunction)

mustache_helpers = {k: v for k, v in all_functions if not k.startswith('_')}