From d1a8cd3f6536f112b1975c58eac9511a4ed648fb Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Thu, 23 Aug 2012 04:50:03 -0700 Subject: [PATCH] Limiting depth of comments --- lms/djangoapps/django_comment_client/base/views.py | 9 ++++++++- lms/djangoapps/django_comment_client/helpers.py | 8 ++++++++ lms/djangoapps/django_comment_client/utils.py | 2 ++ lms/envs/common.py | 4 ++++ lms/templates/discussion/mustache/_content.mustache | 4 +++- 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index 70396ba064..dd9da857c6 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -8,6 +8,8 @@ import functools import comment_client as cc import django_comment_client.utils as utils +import django_comment_client.settings as cc_settings + from django.core import exceptions from django.contrib.auth.decorators import login_required @@ -15,7 +17,6 @@ from django.views.decorators.http import require_POST, require_GET from django.views.decorators import csrf from django.core.files.storage import get_storage_class from django.utils.translation import ugettext as _ -from django.conf import settings from django.contrib.auth.models import User from mitxmako.shortcuts import render_to_response, render_to_string @@ -114,6 +115,9 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None): @login_required @permitted def create_comment(request, course_id, thread_id): + if cc_settings.MAX_COMMENT_DEPTH is not None: + if cc_settings.MAX_COMMENT_DEPTH < 0: + return JsonError("Comment level too deep") return _create_comment(request, course_id, thread_id=thread_id) @require_POST @@ -158,6 +162,9 @@ def openclose_thread(request, course_id, thread_id): @login_required @permitted def create_sub_comment(request, course_id, comment_id): + if cc_settings.MAX_COMMENT_DEPTH is not None: + if cc_settings.MAX_COMMENT_DEPTH <= cc.Comment.find(comment_id).depth: + return JsonError("Comment level too deep") return _create_comment(request, course_id, parent_id=comment_id) @require_POST diff --git a/lms/djangoapps/django_comment_client/helpers.py b/lms/djangoapps/django_comment_client/helpers.py index c35559255d..671659aed8 100644 --- a/lms/djangoapps/django_comment_client/helpers.py +++ b/lms/djangoapps/django_comment_client/helpers.py @@ -6,6 +6,7 @@ from django.core.urlresolvers import reverse from functools import partial from utils import * +import django_comment_client.settings as cc_settings import pystache_custom as pystache import urllib @@ -39,6 +40,13 @@ def render_content(content, additional_context={}): 'content': extend_content(content), content['type']: True, } + if cc_settings.MAX_COMMENT_DEPTH is not None: + if content['type'] == 'thread': + if cc_settings.MAX_COMMENT_DEPTH < 0: + context['max_depth'] = True + elif content['type'] == 'comment': + if cc_settings.MAX_COMMENT_DEPTH <= content['depth']: + context['max_depth'] = 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) diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index 11403bbaed..fded387462 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -21,6 +21,8 @@ import pystache_custom as pystache _FULLMODULES = None _DISCUSSIONINFO = None + + def extract(dic, keys): return {k: dic.get(k) for k in keys} diff --git a/lms/envs/common.py b/lms/envs/common.py index dc1d631046..99c68afbe3 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -38,6 +38,10 @@ ASKBOT_ENABLED = False GENERATE_RANDOM_USER_CREDENTIALS = False PERFSTATS = False +DISCUSSION_SETTINGS = { + 'MAX_COMMENT_DEPTH': 2, +} + # Features MITX_FEATURES = { 'SAMPLE' : False, diff --git a/lms/templates/discussion/mustache/_content.mustache b/lms/templates/discussion/mustache/_content.mustache index 34926b57b5..91330dd0d8 100644 --- a/lms/templates/discussion/mustache/_content.mustache +++ b/lms/templates/discussion/mustache/_content.mustache @@ -51,7 +51,9 @@ {{/thread}}