Commit 6fa02985 by Rocky Duan

make retrieved single thread/comment after update integrated with permissions

parent 0fa0f2c8
...@@ -17,6 +17,7 @@ from django.conf import settings ...@@ -17,6 +17,7 @@ from django.conf import settings
from mitxmako.shortcuts import render_to_response, render_to_string 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
import django_comment_client.utils as utils
from django_comment_client.permissions import check_permissions_by_view from django_comment_client.permissions import check_permissions_by_view
import functools import functools
...@@ -58,9 +59,14 @@ def create_thread(request, course_id, commentable_id): ...@@ -58,9 +59,14 @@ def create_thread(request, course_id, commentable_id):
'thread': thread.to_dict(), 'thread': thread.to_dict(),
} }
html = render_to_string('discussion/ajax_create_thread.html', context) html = render_to_string('discussion/ajax_create_thread.html', context)
annotated_content_info = utils.get_annotated_content_info(course_id,
thread.to_dict(),
request.user,
'thread')
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': thread.to_dict(), 'content': thread.to_dict(),
'annotated_content_info': annotated_content_info,
}) })
else: else:
return JsonResponse(thread.to_dict()) return JsonResponse(thread.to_dict())
...@@ -78,9 +84,14 @@ def update_thread(request, course_id, thread_id): ...@@ -78,9 +84,14 @@ def update_thread(request, course_id, thread_id):
'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)
annotated_content_info = utils.get_annotated_content_info(course_id,
thread.to_dict(),
request.user,
'thread')
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': thread.to_dict(), 'content': thread.to_dict(),
'annotated_content_info': annotated_content_info,
}) })
else: else:
return JsonResponse(thread.to_dict()) return JsonResponse(thread.to_dict())
...@@ -103,9 +114,14 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None): ...@@ -103,9 +114,14 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None):
'comment': comment.to_dict(), 'comment': comment.to_dict(),
} }
html = render_to_string('discussion/ajax_create_comment.html', context) html = render_to_string('discussion/ajax_create_comment.html', context)
annotated_content_info = utils.get_annotated_content_info(course_id,
comment.to_dict(),
request.user,
'comment')
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': comment.to_dict(), 'content': comment.to_dict(),
'annotated_content_info': annotated_content_info,
}) })
else: else:
return JsonResponse(comment.to_dict()) return JsonResponse(comment.to_dict())
...@@ -137,9 +153,14 @@ def update_comment(request, course_id, comment_id): ...@@ -137,9 +153,14 @@ def update_comment(request, course_id, comment_id):
'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)
annotated_content_info = utils.get_annotated_content_info(course_id,
comment.to_dict(),
request.user,
'comment')
return JsonResponse({ return JsonResponse({
'html': html, 'html': html,
'content': comment.to_dict(), 'content': comment.to_dict(),
'annotated_content_info': annotated_content_info,
}) })
else: else:
return JsonResponse(comment.to_dict()), return JsonResponse(comment.to_dict()),
......
...@@ -18,7 +18,6 @@ import json ...@@ -18,7 +18,6 @@ import json
import comment_client as cc import comment_client as cc
import dateutil import dateutil
from django_comment_client.permissions import check_permissions_by_view
THREADS_PER_PAGE = 5 THREADS_PER_PAGE = 5
PAGES_NEARBY_DELTA = 2 PAGES_NEARBY_DELTA = 2
...@@ -54,7 +53,7 @@ def render_discussion(request, course_id, threads, discussion_id=None, \ ...@@ -54,7 +53,7 @@ def render_discussion(request, course_id, threads, discussion_id=None, \
'forum': (lambda: reverse('django_comment_client.forum.views.forum_form_discussion', args=[course_id, discussion_id])), 'forum': (lambda: reverse('django_comment_client.forum.views.forum_form_discussion', args=[course_id, discussion_id])),
}[discussion_type]() }[discussion_type]()
annotated_content_info = {thread['id']: get_annotated_content_info(course_id, thread, request.user, is_thread=True) for thread in threads} annotated_content_info = {thread['id']: utils.get_annotated_content_info(course_id, thread, request.user, type='thread') for thread in threads}
context = { context = {
'threads': threads, 'threads': threads,
...@@ -151,36 +150,18 @@ def forum_form_discussion(request, course_id, discussion_id): ...@@ -151,36 +150,18 @@ def forum_form_discussion(request, course_id, discussion_id):
} }
return render_to_response('discussion/index.html', context) return render_to_response('discussion/index.html', context)
def get_annotated_content_info(course_id, content, user, is_thread):
return {
'editable': check_permissions_by_view(user, course_id, content, "update_thread" if is_thread else "update_comment"),
'can_reply': check_permissions_by_view(user, course_id, content, "create_comment" if is_thread else "create_sub_comment"),
'can_endorse': check_permissions_by_view(user, course_id, content, "endorse_comment") if not is_thread else False,
'can_delete': check_permissions_by_view(user, course_id, content, "delete_thread" if is_thread else "delete_comment"),
}
def get_annotated_content_infos(course_id, thread, user, is_thread=True):
infos = {}
def _annotate(content, is_thread=is_thread):
infos[str(content['id'])] = get_annotated_content_info(course_id, content, user, is_thread)
for child in content.get('children', []):
_annotate(child, is_thread=False)
_annotate(thread)
return infos
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(recursive=True) 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=thread.to_dict(), \ annotated_content_info = utils.get_annotated_content_infos(course_id, thread=thread.to_dict(), \
user=request.user, is_thread=True) user=request.user, type='thread')
context = { context = {
'discussion_id': discussion_id, 'discussion_id': discussion_id,
'thread': thread, 'thread': thread,
'user_info': cc.User.from_django_user(request.user).to_dict(),#get_user_info(request.user.id, raw=True), 'user_info': cc.User.from_django_user(request.user).to_dict(),
'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,7 +173,7 @@ def single_thread(request, course_id, discussion_id, thread_id): ...@@ -192,7 +173,7 @@ 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(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 = utils.get_annotated_content_infos(course_id, thread, request.user, type='thread')
context = {'thread': thread.to_dict()} 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)
......
...@@ -10,6 +10,8 @@ from django.conf import settings ...@@ -10,6 +10,8 @@ from django.conf import settings
import operator import operator
import itertools import itertools
from django_comment_client.permissions import check_permissions_by_view
_FULLMODULES = None _FULLMODULES = None
_DISCUSSIONINFO = None _DISCUSSIONINFO = None
...@@ -122,3 +124,20 @@ class HtmlResponse(HttpResponse): ...@@ -122,3 +124,20 @@ class HtmlResponse(HttpResponse):
class ViewNameMiddleware(object): class ViewNameMiddleware(object):
def process_view(self, request, view_func, view_args, view_kwargs): def process_view(self, request, view_func, view_args, view_kwargs):
request.view_name = view_func.__name__ request.view_name = view_func.__name__
def get_annotated_content_info(course_id, content, user, type):
return {
'editable': check_permissions_by_view(user, course_id, content, "update_thread" if type == 'thread' else "update_comment"),
'can_reply': check_permissions_by_view(user, course_id, content, "create_comment" if type == 'thread' else "create_sub_comment"),
'can_endorse': check_permissions_by_view(user, course_id, content, "endorse_comment") if type == 'comment' else False,
'can_delete': check_permissions_by_view(user, course_id, content, "delete_thread" if type == 'thread' else "delete_comment"),
}
def get_annotated_content_infos(course_id, thread, user, type='thread'):
infos = {}
def _annotate(content, type):
infos[str(content['id'])] = get_annotated_content_info(course_id, content, user, type)
for child in content.get('children', []):
_annotate(child, 'comment')
_annotate(thread, type)
return infos
...@@ -81,6 +81,7 @@ initializeFollowThread = (thread) -> ...@@ -81,6 +81,7 @@ initializeFollowThread = (thread) ->
Discussion.setWmdContent $content, $local, "reply-body", "" Discussion.setWmdContent $content, $local, "reply-body", ""
Discussion.setContentInfo response.content['id'], 'can_reply', true Discussion.setContentInfo response.content['id'], 'can_reply', true
Discussion.setContentInfo response.content['id'], 'editable', true Discussion.setContentInfo response.content['id'], 'editable', true
Discussion.extendContentInfo response.content['id'], response['annotated_content_info']
Discussion.initializeContent($comment) Discussion.initializeContent($comment)
Discussion.bindContentEvents($comment) Discussion.bindContentEvents($comment)
$local(".discussion-reply-new").hide() $local(".discussion-reply-new").hide()
...@@ -151,6 +152,7 @@ initializeFollowThread = (thread) -> ...@@ -151,6 +152,7 @@ initializeFollowThread = (thread) ->
error: Discussion.formErrorHandler($local(".discussion-update-errors")) error: Discussion.formErrorHandler($local(".discussion-update-errors"))
success: (response, textStatus) -> success: (response, textStatus) ->
$discussionContent.replaceWith(response.html) $discussionContent.replaceWith(response.html)
Discussion.extendContentInfo response.content['id'], response['annotated_content_info']
Discussion.initializeContent($content) Discussion.initializeContent($content)
Discussion.bindContentEvents($content) Discussion.bindContentEvents($content)
...@@ -178,6 +180,7 @@ initializeFollowThread = (thread) -> ...@@ -178,6 +180,7 @@ initializeFollowThread = (thread) ->
error: Discussion.formErrorHandler($local(".discussion-update-errors")) error: Discussion.formErrorHandler($local(".discussion-update-errors"))
success: (response, textStatus) -> success: (response, textStatus) ->
$discussionContent.replaceWith(response.html) $discussionContent.replaceWith(response.html)
Discussion.extendContentInfo response.content['id'], response['annotated_content_info']
Discussion.initializeContent($content) Discussion.initializeContent($content)
Discussion.bindContentEvents($content) Discussion.bindContentEvents($content)
...@@ -207,7 +210,7 @@ initializeFollowThread = (thread) -> ...@@ -207,7 +210,7 @@ initializeFollowThread = (thread) ->
else if text.match(/[Oo]pen/) else if text.match(/[Oo]pen/)
closed = false closed = false
else else
return console.log "Unexpected text " + text + "for open/close thread." console.log "Unexpected text " + text + "for open/close thread."
Discussion.safeAjax Discussion.safeAjax
$elem: $(elem) $elem: $(elem)
...@@ -278,7 +281,7 @@ initializeFollowThread = (thread) -> ...@@ -278,7 +281,7 @@ initializeFollowThread = (thread) ->
type: "GET" type: "GET"
dataType: 'json' dataType: 'json'
success: (response, textStatus) -> success: (response, textStatus) ->
Discussion.bulkExtendContentInfo response['annotated_content_info'] Discussion.extendContentInfo response.content['id'], response['annotated_content_info']
$content.append(response['html']) $content.append(response['html'])
$content.find(".comment").each (index, comment) -> $content.find(".comment").each (index, comment) ->
Discussion.initializeContent(comment) Discussion.initializeContent(comment)
...@@ -368,10 +371,10 @@ initializeFollowThread = (thread) -> ...@@ -368,10 +371,10 @@ initializeFollowThread = (thread) ->
MathJax.Hub.Queue ["Typeset", MathJax.Hub, $contentBody.attr("id")] MathJax.Hub.Queue ["Typeset", MathJax.Hub, $contentBody.attr("id")]
id = $content.attr("_id") id = $content.attr("_id")
if not Discussion.getContentInfo id, 'editable' if not Discussion.getContentInfo id, 'editable'
$local(".discussion-edit").remove() $local(".admin-edit").remove()
if not Discussion.getContentInfo id, 'can_reply' if not Discussion.getContentInfo id, 'can_reply'
$local(".discussion-reply").remove() $local(".discussion-reply").remove()
if not Discussion.getContentInfo id, 'can_endorse' if not Discussion.getContentInfo id, 'can_endorse'
$local(".discussion-endorse-control").remove() $local(".admin-endorse").remove()
if not Discussion.getContentInfo id, 'can_delete' if not Discussion.getContentInfo id, 'can_delete'
$local(".discussion-delete").remove() $local(".admin-delete").remove()
...@@ -51,7 +51,8 @@ initializeFollowDiscussion = (discussion) -> ...@@ -51,7 +51,8 @@ initializeFollowDiscussion = (discussion) ->
$thread = $(response.html) $thread = $(response.html)
$discussion.children(".threads").prepend($thread) $discussion.children(".threads").prepend($thread)
Discussion.setWmdContent $discussion, $local, "new-post-body", "" Discussion.setWmdContent $discussion, $local, "new-post-body", ""
Discussion.setContentInfo response.content['id'], 'editable', true #Discussion.setContentInfo response.content['id'], 'editable', true
Discussion.extendContentInfo response.content['id'], response['annotated_content_info']
Discussion.initializeContent($thread) Discussion.initializeContent($thread)
Discussion.bindContentEvents($thread) Discussion.bindContentEvents($thread)
$(".new-post-form").addClass("collapsed") $(".new-post-form").addClass("collapsed")
......
...@@ -147,6 +147,10 @@ wmdEditors = {} ...@@ -147,6 +147,10 @@ wmdEditors = {}
window.$$annotated_content_info[id] ||= {} window.$$annotated_content_info[id] ||= {}
window.$$annotated_content_info[id][attr] = value window.$$annotated_content_info[id][attr] = value
extendContentInfo: (id, newInfo) ->
if not window.$$annotated_content_info?
window.$$annotated_content_info = {}
window.$$annotated_content_info[id] = newInfo
bulkExtendContentInfo: (newInfos) -> bulkExtendContentInfo: (newInfos) ->
if not window.$$annotated_content_info? if not window.$$annotated_content_info?
window.$$annotated_content_info = {} window.$$annotated_content_info = {}
......
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