Commit 44f7b37b by Rocky Duan

refactored comment_client working

parent b7d33618
...@@ -5,7 +5,7 @@ import os.path ...@@ -5,7 +5,7 @@ import os.path
import logging import logging
import urlparse import urlparse
import comment_client as c import comment_client as cc
from django.core import exceptions from django.core import exceptions
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
...@@ -19,7 +19,6 @@ from mitxmako.shortcuts import render_to_response, render_to_string ...@@ -19,7 +19,6 @@ from mitxmako.shortcuts import render_to_response, render_to_string
from django_comment_client.utils import JsonResponse, JsonError, extract from django_comment_client.utils import JsonResponse, JsonError, extract
from django_comment_client.permissions import check_permissions_by_view from django_comment_client.permissions import check_permissions_by_view
from collection import defaultdict
import functools import functools
def permitted(fn): def permitted(fn):
...@@ -27,9 +26,9 @@ def permitted(fn): ...@@ -27,9 +26,9 @@ def permitted(fn):
def wrapper(request, *args, **kwargs): def wrapper(request, *args, **kwargs):
def fetch_content(): def fetch_content():
if "thread_id" in kwargs: if "thread_id" in kwargs:
content = dict(c.Thread.find(kwargs["thread_id"])) content = cc.Thread.find(kwargs["thread_id"]).to_dict()
elif "comment_id" in kwargs: elif "comment_id" in kwargs:
content = dict(c.Comment.find(kwargs["comment_id"])) content = cc.Comment.find(kwargs["comment_id"]).to_dict()
else: else:
content = None content = None
return content return content
...@@ -45,71 +44,71 @@ def permitted(fn): ...@@ -45,71 +44,71 @@ def permitted(fn):
@permitted @permitted
def create_thread(request, course_id, commentable_id): def create_thread(request, course_id, commentable_id):
post = request.POST post = request.POST
thread = c.Thread(**extract(post, ['body', 'title', 'tags'])) thread = cc.Thread(**extract(post, ['body', 'title', 'tags']))
thread.anonymous = post.get('anonymous', 'false').lower() == 'true' thread.anonymous = post.get('anonymous', 'false').lower() == 'true'
thread.course_id = course_id thread.course_id = course_id
thread.user_id = request.user.id thread.user_id = request.user.id
thread.save() thread.save()
if post.get('auto_subscribe', 'false').lower() == 'true': if post.get('auto_subscribe', 'false').lower() == 'true':
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
user.subscribe(thread) user.follow(thread)
if request.is_ajax(): if request.is_ajax():
context = { context = {
'course_id': course_id, 'course_id': course_id,
'thread': dict(thread), 'thread': thread.to_dict(),
} }
html = render_to_string('discussion/ajax_create_thread.html', context) html = render_to_string('discussion/ajax_create_thread.html', context)
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': dict(thread), 'content': thread.to_dict(),
}) })
else: else:
return JsonResponse(dict(thread)) return JsonResponse(thread.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def update_thread(request, course_id, thread_id): def update_thread(request, course_id, thread_id):
thread = c.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.update_attributes(**extract(request.POST, ['body', 'title', 'tags'])) thread.update_attributes(**extract(request.POST, ['body', 'title', 'tags']))
thread.save() thread.save()
if request.is_ajax(): if request.is_ajax():
context = { context = {
'thread': dict(thread), 'thread': thread.to_dict(),
'course_id': course_id, 'course_id': course_id,
} }
html = render_to_string('discussion/ajax_update_thread.html', context) html = render_to_string('discussion/ajax_update_thread.html', context)
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': dict(thread), 'content': thread.to_dict(),
}) })
else: else:
return JsonResponse(dict(thread)) return JsonResponse(thread.to_dict())
def _create_comment(request, course_id, thread_id=None, parent_id=None): def _create_comment(request, course_id, thread_id=None, parent_id=None):
post = request.POST post = request.POST
comment = c.Comment(**extract(post, ['body'])) comment = cc.Comment(**extract(post, ['body']))
comment.anonymous = post.get('anonymous', 'false').lower() == 'true' comment.anonymous = post.get('anonymous', 'false').lower() == 'true'
comment.user_id = request.user.id comment.user_id = request.user.id
comment.course_id = course_id comment.course_id = course_id
comment.thread_id = thread_id comment.thread_id = thread_id
comment.parent_id = parent_id comment.parent_id = parent_id
comment.save() comment.save()
dict_comment = dict(comment) dict_comment = comment.to_dict()
if post.get('auto_subscribe', 'false').lower() == 'true': if post.get('auto_subscribe', 'false').lower() == 'true':
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
user.subscribe(comment.thread) user.follow(comment.thread)
if request.is_ajax(): if request.is_ajax():
context = { context = {
'comment': dict(comment), 'comment': comment.to_dict(),
} }
html = render_to_string('discussion/ajax_create_comment.html', context) html = render_to_string('discussion/ajax_create_comment.html', context)
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': dict(comment), 'content': comment.to_dict(),
}) })
else: else:
return JsonResponse(dict(comment)) return JsonResponse(comment.to_dict())
@require_POST @require_POST
@login_required @login_required
...@@ -121,47 +120,47 @@ def create_comment(request, course_id, thread_id): ...@@ -121,47 +120,47 @@ def create_comment(request, course_id, thread_id):
@login_required @login_required
@permitted @permitted
def delete_thread(request, course_id, thread_id): def delete_thread(request, course_id, thread_id):
thread = c.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.delete() thread.delete()
return JsonResponse(dict(thread)) return JsonResponse(thread.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def update_comment(request, course_id, comment_id): def update_comment(request, course_id, comment_id):
comment = c.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
comment.update_attributes(**extract(request.POST, ['body'])) comment.update_attributes(**extract(request.POST, ['body']))
comment.save() comment.save()
if request.is_ajax(): if request.is_ajax():
context = { context = {
'comment': dict(comment), 'comment': comment.to_dict(),
'course_id': course_id, 'course_id': course_id,
} }
html = render_to_string('discussion/ajax_update_comment.html', context) html = render_to_string('discussion/ajax_update_comment.html', context)
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': dict(comment), 'content': comment.to_dict(),
}) })
else: else:
return JsonResponse(dict(comment)), return JsonResponse(comment.to_dict()),
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def endorse_comment(request, course_id, comment_id): def endorse_comment(request, course_id, comment_id):
comment = c.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true' comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true'
comment.save() comment.save()
return JsonResponse(dict(response)) return JsonResponse(comment.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def openclose_thread(request, course_id, thread_id): def openclose_thread(request, course_id, thread_id):
comment = c.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
comment.endorsed = request.POST.get('closed', 'false').lower() == 'true' comment.endorsed = request.POST.get('closed', 'false').lower() == 'true'
comment.save() comment.save()
return JsonResponse(dict(response)) return JsonResponse(comment.to_dict())
@require_POST @require_POST
@login_required @login_required
...@@ -173,53 +172,53 @@ def create_sub_comment(request, course_id, comment_id): ...@@ -173,53 +172,53 @@ def create_sub_comment(request, course_id, comment_id):
@login_required @login_required
@permitted @permitted
def delete_comment(request, course_id, comment_id): def delete_comment(request, course_id, comment_id):
comment = c.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
comment.delete() comment.delete()
return JsonResponse(dict(response)) return JsonResponse(comment.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def vote_for_comment(request, course_id, comment_id, value): def vote_for_comment(request, course_id, comment_id, value):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
comment = c.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
user.vote(comment, value) user.vote(comment, value)
return JsonResponse(dict(comment)) return JsonResponse(comment.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def undo_vote_for_comment(request, course_id, comment_id): def undo_vote_for_comment(request, course_id, comment_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
comment = c.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
user.unvote(comment) user.unvote(comment)
return JsonResponse(dict(comment)) return JsonResponse(comment.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def vote_for_thread(request, course_id, thread_id, value): def vote_for_thread(request, course_id, thread_id, value):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
thread = c.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
user.vote(thread, value) user.vote(thread, value)
return JsonResponse(dict(thread)) return JsonResponse(thread.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def undo_vote_for_thread(request, course_id, thread_id): def undo_vote_for_thread(request, course_id, thread_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
thread = c.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
user.unvote(thread) user.unvote(thread)
return JsonResponse(dict(thread)) return JsonResponse(thread.to_dict())
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def follow_thread(request, course_id, thread_id): def follow_thread(request, course_id, thread_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
thread = c.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
user.follow(thread) user.follow(thread)
return JsonResponse({}) return JsonResponse({})
...@@ -227,8 +226,8 @@ def follow_thread(request, course_id, thread_id): ...@@ -227,8 +226,8 @@ def follow_thread(request, course_id, thread_id):
@login_required @login_required
@permitted @permitted
def follow_commentable(request, course_id, commentable_id): def follow_commentable(request, course_id, commentable_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
commentable = c.Commentable.find(commentable_id) commentable = cc.Commentable.find(commentable_id)
user.follow(commentable) user.follow(commentable)
return JsonResponse({}) return JsonResponse({})
...@@ -236,8 +235,8 @@ def follow_commentable(request, course_id, commentable_id): ...@@ -236,8 +235,8 @@ def follow_commentable(request, course_id, commentable_id):
@login_required @login_required
@permitted @permitted
def follow_user(request, course_id, followed_user_id): def follow_user(request, course_id, followed_user_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
followed_user = c.User.find(followed_user_id) followed_user = cc.User.find(followed_user_id)
user.follow(followed_user) user.follow(followed_user)
return JsonResponse({}) return JsonResponse({})
...@@ -245,8 +244,8 @@ def follow_user(request, course_id, followed_user_id): ...@@ -245,8 +244,8 @@ def follow_user(request, course_id, followed_user_id):
@login_required @login_required
@permitted @permitted
def unfollow_thread(request, course_id, thread_id): def unfollow_thread(request, course_id, thread_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
thread = c.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
user.unfollow(thread) user.unfollow(thread)
return JsonResponse({}) return JsonResponse({})
...@@ -254,8 +253,8 @@ def unfollow_thread(request, course_id, thread_id): ...@@ -254,8 +253,8 @@ def unfollow_thread(request, course_id, thread_id):
@login_required @login_required
@permitted @permitted
def unfollow_commentable(request, course_id, commentable_id): def unfollow_commentable(request, course_id, commentable_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
commentable = c.Commentable.find(commentable_id) commentable = cc.Commentable.find(commentable_id)
user.unfollow(commentable) user.unfollow(commentable)
return JsonResponse({}) return JsonResponse({})
...@@ -263,8 +262,8 @@ def unfollow_commentable(request, course_id, commentable_id): ...@@ -263,8 +262,8 @@ def unfollow_commentable(request, course_id, commentable_id):
@login_required @login_required
@permitted @permitted
def unfollow_user(request, course_id, followed_user_id): def unfollow_user(request, course_id, followed_user_id):
user = c.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
followed_user = c.User.find(followed_user_id) followed_user = cc.User.find(followed_user_id)
user.unfollow(followed_user) user.unfollow(followed_user)
return JsonResponse({}) return JsonResponse({})
...@@ -273,7 +272,7 @@ def search_similar_threads(request, course_id, commentable_id): ...@@ -273,7 +272,7 @@ def search_similar_threads(request, course_id, commentable_id):
text = request.GET.get('text', None) text = request.GET.get('text', None)
if text: if text:
return JsonResponse( return JsonResponse(
c.search_similar_threads( cc.search_similar_threads(
course_id, course_id,
recursive=False, recursive=False,
query_params={ query_params={
...@@ -289,7 +288,7 @@ def tags_autocomplete(request, course_id): ...@@ -289,7 +288,7 @@ def tags_autocomplete(request, course_id):
value = request.GET.get('q', None) value = request.GET.get('q', None)
results = [] results = []
if value: if value:
results = c.tags_autocomplete(value) results = cc.tags_autocomplete(value)
return JsonResponse(results) return JsonResponse(results)
@require_POST @require_POST
......
...@@ -59,7 +59,7 @@ def render_discussion(request, course_id, threads, discussion_id=None, \ ...@@ -59,7 +59,7 @@ def render_discussion(request, course_id, threads, discussion_id=None, \
context = { context = {
'threads': threads, 'threads': threads,
'discussion_id': discussion_id, 'discussion_id': discussion_id,
'user_info': dict(cc.User.from_django_user(request.user)),#comment_client.get_user_info(request.user.id, raw=True), 'user_info': json.dumps(cc.User.from_django_user(request.user).to_dict()),
'course_id': course_id, 'course_id': course_id,
'request': request, 'request': request,
'performed_search': _should_perform_search(request), 'performed_search': _should_perform_search(request),
...@@ -90,7 +90,6 @@ def get_threads(request, course_id, discussion_id): ...@@ -90,7 +90,6 @@ def get_threads(request, course_id, discussion_id):
'course_id': course_id, 'course_id': course_id,
} }
import pdb; pdb.set_trace()
threads, page, num_pages = cc.Thread.search(query_params) threads, page, num_pages = cc.Thread.search(query_params)
#if _should_perform_search(request): #if _should_perform_search(request):
...@@ -172,16 +171,16 @@ def get_annotated_content_infos(course_id, thread, user, is_thread=True): ...@@ -172,16 +171,16 @@ def get_annotated_content_infos(course_id, thread, user, is_thread=True):
def render_single_thread(request, discussion_id, course_id, thread_id): def render_single_thread(request, discussion_id, course_id, thread_id):
thread = cc.Thread.find(thread_id).retrieve_with_comments() thread = cc.Thread.find(thread_id).retrieve(recursive=True)
#comment_client.get_thread(thread_id, recursive=True) #comment_client.get_thread(thread_id, recursive=True)
annotated_content_info = get_annotated_content_infos(course_id, thread=dict(thread), \ annotated_content_info = get_annotated_content_infos(course_id, thread=thread.to_dict(), \
user=request.user, is_thread=True) user=request.user, is_thread=True)
context = { context = {
'discussion_id': discussion_id, 'discussion_id': discussion_id,
'thread': thread, 'thread': thread,
'user_info': dict(cc.User.from_django_user(request.user)),#get_user_info(request.user.id, raw=True), 'user_info': cc.User.from_django_user(request.user).to_dict(),#get_user_info(request.user.id, raw=True),
'annotated_content_info': json.dumps(annotated_content_info), 'annotated_content_info': json.dumps(annotated_content_info),
'course_id': course_id, 'course_id': course_id,
'request': request, 'request': request,
...@@ -192,9 +191,9 @@ def single_thread(request, course_id, discussion_id, thread_id): ...@@ -192,9 +191,9 @@ def single_thread(request, course_id, discussion_id, thread_id):
if request.is_ajax(): if request.is_ajax():
thread = cc.Thread.find(thread_id).retrieve_with_comments()#comment_client.get_thread(thread_id, recursive=True) thread = cc.Thread.find(thread_id).retrieve(recursive=True)
annotated_content_info = get_annotated_content_infos(course_id, thread, request.user) annotated_content_info = get_annotated_content_infos(course_id, thread, request.user)
context = {'thread': dict(thread)} context = {'thread': thread.to_dict()}
html = render_to_string('discussion/_ajax_single_thread.html', context) html = render_to_string('discussion/_ajax_single_thread.html', context)
return utils.JsonResponse({ return utils.JsonResponse({
......
...@@ -183,8 +183,6 @@ initializeFollowThread = (thread) -> ...@@ -183,8 +183,6 @@ initializeFollowThread = (thread) ->
handleEndorse = (elem, endorsed) -> handleEndorse = (elem, endorsed) ->
url = Discussion.urlFor('endorse_comment', id) url = Discussion.urlFor('endorse_comment', id)
console.log endorsed
console.log url
Discussion.safeAjax Discussion.safeAjax
$elem: $(elem) $elem: $(elem)
url: url url: url
......
...@@ -79,7 +79,6 @@ initializeFollowDiscussion = (discussion) -> ...@@ -79,7 +79,6 @@ initializeFollowDiscussion = (discussion) ->
data: data:
text: $local(".new-post-title").val() text: $local(".new-post-title").val()
success: (response, textStatus) -> success: (response, textStatus) ->
console.log "request"
$similarPosts.empty() $similarPosts.empty()
if $.type(response) == "array" and response.length if $.type(response) == "array" and response.length
$wrapper.show() $wrapper.show()
......
...@@ -116,7 +116,6 @@ wmdEditors = {} ...@@ -116,7 +116,6 @@ wmdEditors = {}
appended_id = "-#{cls_identifier}-#{id}" appended_id = "-#{cls_identifier}-#{id}"
imageUploadUrl = Discussion.urlFor('upload') imageUploadUrl = Discussion.urlFor('upload')
editor = Markdown.makeWmdEditor elem, appended_id, imageUploadUrl, Discussion.postMathJaxProcessor editor = Markdown.makeWmdEditor elem, appended_id, imageUploadUrl, Discussion.postMathJaxProcessor
console.log editor
wmdEditors["#{cls_identifier}-#{id}"] = editor wmdEditors["#{cls_identifier}-#{id}"] = editor
$input = $("#wmd-input-#{cls_identifier}-#{id}") $input = $("#wmd-input-#{cls_identifier}-#{id}")
$input.attr("placeholder", "post a new topic...").bind 'focus', (e) -> $input.attr("placeholder", "post a new topic...").bind 'focus', (e) ->
......
<%namespace name="renderer" file="_thread.html"/> <%namespace name="renderer" file="_thread.html"/>
<%! from django.template.defaultfilters import escapejs %>
<section class="discussion inline-discussion" _id="${discussion_id}"> <section class="discussion inline-discussion" _id="${discussion_id}">
...@@ -27,18 +28,11 @@ ...@@ -27,18 +28,11 @@
<%include file="_paginator.html" /> <%include file="_paginator.html" />
</section> </section>
<%!
def escape_quotes(text):
return text.replace('\"', '\\\"').replace("\'", "\\\'")
%>
<script type="text/javascript"> <script type="text/javascript">
var $$user_info = JSON.parse('${user_info | escape_quotes}'); var $$user_info = JSON.parse("${user_info | escapejs}");
var $$course_id = "${course_id}"; var $$course_id = "${course_id | escapejs}";
if (typeof $$annotated_content_info === undefined || $$annotated_content_info === null) { if (typeof $$annotated_content_info === undefined || $$annotated_content_info === null) {
var $$annotated_content_info = {}; var $$annotated_content_info = {};
} }
$$annotated_content_info = $.extend($$annotated_content_info, JSON.parse("${annotated_content_info | escape_quotes}")); $$annotated_content_info = $.extend($$annotated_content_info, JSON.parse("${annotated_content_info | escapejs}"));
</script> </script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment