From 2869de16dd6a728e5ecb6472c2078ed08eb31a82 Mon Sep 17 00:00:00 2001 From: Rocky Duan Date: Mon, 30 Jul 2012 17:15:53 -0400 Subject: [PATCH 1/4] refactored thread.html --- lms/templates/discussion/thread.html | 136 +++++++++++++-------------- 1 file changed, 66 insertions(+), 70 deletions(-) diff --git a/lms/templates/discussion/thread.html b/lms/templates/discussion/thread.html index 41b6d086e2..1f44af9d18 100644 --- a/lms/templates/discussion/thread.html +++ b/lms/templates/discussion/thread.html @@ -4,70 +4,78 @@ <%! import urllib %> <%def name="render_thread(course_id, thread, edit_thread=False, show_comments=False)"> - <% - if show_comments: - url_for_thread = "" - else: - thread_id = thread['id'] - url_for_thread = reverse('django_comment_client.forum.views.single_thread', args=[course_id, thread_id]) - def url_for_tags(tags): - return reverse('django_comment_client.forum.views.search', args=[course_id]) + '?' + urllib.urlencode({'tags': ",".join(tags)}) - %> +
-
-
- ${render_vote(thread)} -
- ${thread['title'] | h} -
-
${thread['body'] | h}
-
- % for tag in thread['tags']: - ${tag} - % endfor -
-
- ${render_info(thread)} - % if edit_thread: - ${render_reply()} - ${render_edit()} - % endif -
-
-
-
-
+ ${render_content(thread, "thread", edit_thread=edit_thread, show_comments=show_comments)} % if show_comments: -
- ${render_comments(thread['children'])} -
+ ${render_comments(thread['children'])} % endif
<%def name="render_comments(comments)"> - % for comment in comments: -
-
-
- ${render_vote(comment)} -
-
- ${comment['body'] | h} -
- ${render_info(comment)} - ${render_reply()} - ${render_edit()} -
-
-
+
+ % for comment in comments: +
+ ${render_content(comment, "comment")} +
+ ${render_comments(comment['children'])}
-
- ${render_comments(comment['children'])} + % endfor +
+ + +<%def name="render_content(content, type, **kwargs)"> +
+
+ ${render_vote(content)} +
+ ${render_title(content, type, **kwargs)} +
+
${content['body'] | h}
+ ${render_tags(content, type, **kwargs)} + ${render_bottom_bar(content, type, **kwargs)} +
- % endfor +
+ + +<%def name="render_title(content, type, **kwargs)"> + <% + if kwargs.get('show_comments', False): + url_for_thread = "" + else: + url_for_thread = reverse('django_comment_client.forum.views.single_thread', args=[course_id, thread['id']]) + %> + % if type == "thread": + ${content['title'] | h} + % endif + + +<%def name="render_tags(content, type, **kwargs)"> + <% + def url_for_tags(tags): + return reverse('django_comment_client.forum.views.search', args=[course_id]) + '?' + urllib.urlencode({'tags': ",".join(tags)}) + %> + % if type == "thread": +
+ % for tag in content['tags']: + ${tag} + % endfor +
+ % endif + + +<%def name="render_bottom_bar(content, type, **kwargs)"> +
+ ${render_info(content)} + % if type == "thread" and kwargs['edit_thread'] or type == "comment": + ${render_link("discussion-link discussion-reply", "Reply")} + ${render_link("discussion-link discussion-edit", "Edit")} + % endif +
<%def name="render_info(content)"> @@ -79,26 +87,14 @@ % endif -<%def name="render_reply()"> - Reply - - -<%def name="render_edit()"> - Edit - - -<%def name="render_watch_thread()"> - Watch +<%def name="render_link(cls, html)"> + ${html} <%def name="render_vote(content)"> - <% - upvote = "˄" - downvote = "˅" - %> -
- ${upvote} +
+ ${render_link("discussion-vote discussion-vote-up", "˄")} ${content['votes']['point']} - ${downvote} + ${render_link("discussion-vote discussion-vote-down", "˅")}
From 941ae0f06812e9b16d659a32013b3e0bf6c3e8a2 Mon Sep 17 00:00:00 2001 From: Rocky Duan Date: Mon, 30 Jul 2012 17:16:44 -0400 Subject: [PATCH 2/4] basic error handling --- .../django_comment_client/base/views.py | 19 +------------ .../django_comment_client/forum/views.py | 2 +- lms/djangoapps/django_comment_client/utils.py | 18 ++++++++++++ lms/envs/common.py | 1 + lms/lib/comment_client.py | 3 +- lms/static/coffee/src/discussion.coffee | 28 +++++++++++++++++-- lms/templates/discussion/inline.html | 3 +- 7 files changed, 50 insertions(+), 24 deletions(-) diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index ac57742ca6..86fe285af0 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -10,30 +10,13 @@ import comment_client from django.core import exceptions from django.contrib.auth.decorators import login_required from django.views.decorators.http import require_POST, require_GET -from django.http import HttpResponse -from django.utils import simplejson 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 -class JsonResponse(HttpResponse): - def __init__(self, data=None): - content = simplejson.dumps(data, - indent=2, - ensure_ascii=False) - super(JsonResponse, self).__init__(content, - mimetype='application/json; charset=utf8') +from django_comment_client.utils import JsonResponse, JsonError -class JsonError(HttpResponse): - def __init__(self, status, error_message=""): - content = simplejson.dumps({'errors': error_message}, - indent=2, - ensure_ascii=False) - super(JsonError, self).__init__(content, - status=status, - mimetype='application/json; charset=utf8') - def thread_author_only(fn): def verified_fn(request, *args, **kwargs): thread_id = args.get('thread_id', False) or \ diff --git a/lms/djangoapps/django_comment_client/forum/views.py b/lms/djangoapps/django_comment_client/forum/views.py index a7eb01e219..17bfa4c687 100644 --- a/lms/djangoapps/django_comment_client/forum/views.py +++ b/lms/djangoapps/django_comment_client/forum/views.py @@ -60,7 +60,7 @@ def forum_form_discussion(request, course_id, discussion_id): search_text = request.GET.get('text', '') if len(search_text) > 0: - threads = comment_client.search(search_text, discussion_id) + threads = comment_client.search_threads({'text': search_text, 'commentable_id': discussion_id}) else: threads = comment_client.get_threads(discussion_id, recursive=False) diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index 35c2a86a9a..30c74c0bb0 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -3,6 +3,8 @@ from courseware.models import StudentModuleCache from courseware.module_render import get_module from xmodule.modulestore import Location from xmodule.modulestore.django import modulestore +from django.http import HttpResponse +from django.utils import simplejson from django.conf import settings import operator @@ -88,3 +90,19 @@ def initialize_discussion_info(request, course): 'discussion_id': url_course_id, 'category': 'General', }] + +class JsonResponse(HttpResponse): + def __init__(self, data=None): + content = simplejson.dumps(data, + indent=2, + ensure_ascii=False) + super(JsonResponse, self).__init__(content, + mimetype='application/json; charset=utf8') + +class JsonError(HttpResponse): + def __init__(self, error_message=""): + content = simplejson.dumps({'errors': error_message}, + indent=2, + ensure_ascii=False) + super(JsonError, self).__init__(content, + mimetype='application/json; charset=utf8') diff --git a/lms/envs/common.py b/lms/envs/common.py index ef4d64839b..17e4decf3b 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -273,6 +273,7 @@ TEMPLATE_LOADERS = ( MIDDLEWARE_CLASSES = ( 'util.middleware.ExceptionLoggingMiddleware', + 'django_comment_client.middleware.AjaxExceptionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', diff --git a/lms/lib/comment_client.py b/lms/lib/comment_client.py index 0b3a14e770..e530527ef3 100644 --- a/lms/lib/comment_client.py +++ b/lms/lib/comment_client.py @@ -6,7 +6,8 @@ SERVICE_HOST = 'http://localhost:4567' PREFIX = SERVICE_HOST + '/api/v1' class CommentClientError(Exception): - pass + def __init__(self, msg): + self.message = msg class CommentClientUnknownError(CommentClientError): pass diff --git a/lms/static/coffee/src/discussion.coffee b/lms/static/coffee/src/discussion.coffee index a60c8ff6f0..42234c6f60 100644 --- a/lms/static/coffee/src/discussion.coffee +++ b/lms/static/coffee/src/discussion.coffee @@ -168,6 +168,9 @@ Discussion = else editView = $("
").addClass("discussion-content-edit") + errorsField = $("
    ").addClass("discussion-errors") + editView.append(errorsField) + textarea = $("
    ").addClass("comment-edit") editView.append(textarea) @@ -221,7 +224,11 @@ Discussion = autowatch = false || $local(".discussion-auto-watch").is(":checked") $.post url, {body: body, anonymous: anonymous, autowatch: autowatch}, (response, textStatus) -> - if textStatus == "success" + if response.errors + errorsField = $local(".discussion-errors").empty() + for error in response.errors + errorsField.append($("
  • ").addClass("new-post-form-error").html(error)) + else Discussion.handleAnchorAndReload(response) , 'json' @@ -232,6 +239,10 @@ Discussion = if textStatus == "success" Discussion.handleAnchorAndReload(response) , 'json' + + handleEditThread = (elem) -> + + handleEditComment = (elem) -> $local(".discussion-reply").click -> handleReply(this) @@ -245,6 +256,13 @@ Discussion = $local(".discussion-vote-down").click -> handleVote(this, "down") + $local(".discussion-edit").click -> + if $content.hasClass("thread") + handleEditThread(this) + else + handleEditComment(this) + + initializeContent: (content) -> $content = $(content) $local = generateLocal($content.children(".discussion-content")) @@ -272,13 +290,17 @@ Discussion = tags = $local(".new-post-tags").val() url = Discussion.urlFor('create_thread', $local(".new-post-form").attr("_id")) $.post url, {title: title, body: body, tags: tags}, (response, textStatus) -> - if textStatus == "success" + if response.errors + errorsField = $local(".discussion-errors").empty() + for error in response.errors + errorsField.append($("
  • ").addClass("new-post-form-error").html(error)) + else Discussion.handleAnchorAndReload(response) , 'json' $local(".discussion-search-form").submit (event) -> event.preventDefault() - text = $local(".discussion-search-text").val() + text = $local(".searchInput").val() isSearchWithinBoard = $local(".discussion-search-within-board").is(":checked") handleSearch(text, isSearchWithinBoard) diff --git a/lms/templates/discussion/inline.html b/lms/templates/discussion/inline.html index 5c2f39f7f8..dbd34305c3 100644 --- a/lms/templates/discussion/inline.html +++ b/lms/templates/discussion/inline.html @@ -7,7 +7,8 @@
  • ${search_bar}
    - +
      +
      New Post From 8bf2eb17e848fa8a53b26b992c4948e61a8005c1 Mon Sep 17 00:00:00 2001 From: Rocky Duan Date: Mon, 30 Jul 2012 17:17:09 -0400 Subject: [PATCH 3/4] middleware file forgot to include --- lms/djangoapps/django_comment_client/middleware.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 lms/djangoapps/django_comment_client/middleware.py diff --git a/lms/djangoapps/django_comment_client/middleware.py b/lms/djangoapps/django_comment_client/middleware.py new file mode 100644 index 0000000000..08e20b0296 --- /dev/null +++ b/lms/djangoapps/django_comment_client/middleware.py @@ -0,0 +1,9 @@ +from comment_client import CommentClientError +from django_comment_client.utils import JsonError +import json + +class AjaxExceptionMiddleware(object): + def process_exception(self, request, exception): + if isinstance(exception, CommentClientError) and request.is_ajax(): + return JsonError(json.loads(exception.message)) + return None From 5fcc12136e2b43bc60f8d2a74bf54b1ca09651b8 Mon Sep 17 00:00:00 2001 From: Rocky Duan Date: Mon, 30 Jul 2012 17:20:30 -0400 Subject: [PATCH 4/4] fixed bug caused by rearranging stuff --- lms/templates/discussion/thread.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lms/templates/discussion/thread.html b/lms/templates/discussion/thread.html index 1f44af9d18..d82005ae51 100644 --- a/lms/templates/discussion/thread.html +++ b/lms/templates/discussion/thread.html @@ -47,10 +47,10 @@ if kwargs.get('show_comments', False): url_for_thread = "" else: - url_for_thread = reverse('django_comment_client.forum.views.single_thread', args=[course_id, thread['id']]) + url_for_thread = reverse('django_comment_client.forum.views.single_thread', args=[course_id, content['id']]) %> % if type == "thread": - ${content['title'] | h} + ${content['title'] | h} % endif