Commit 44f7b37b by Rocky Duan

refactored comment_client working

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