refactored code to render ajax content
This commit is contained in:
@@ -45,6 +45,19 @@ def permitted(fn):
|
||||
return JsonError("unauthorized")
|
||||
return wrapper
|
||||
|
||||
def ajax_content_response(request, course_id, content, template_name):
|
||||
context = {
|
||||
'course_id': course_id,
|
||||
'content': content,
|
||||
}
|
||||
html = render_to_string(template_name, context)
|
||||
annotated_content_info = utils.get_annotated_content_info(course_id, content, request.user)
|
||||
return JsonResponse({
|
||||
'html': html,
|
||||
'content': content,
|
||||
'annotated_content_info': annotated_content_info,
|
||||
})
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
@@ -60,20 +73,7 @@ def create_thread(request, course_id, commentable_id):
|
||||
user = cc.User.from_django_user(request.user)
|
||||
user.follow(thread)
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'course_id': course_id,
|
||||
'thread': thread.to_dict(),
|
||||
}
|
||||
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({
|
||||
'html': html,
|
||||
'content': thread.to_dict(),
|
||||
'annotated_content_info': annotated_content_info,
|
||||
})
|
||||
return ajax_content_response(request, course_id, thread.to_dict(), 'discussion/ajax_create_thread.html')
|
||||
else:
|
||||
return JsonResponse(thread.to_dict())
|
||||
|
||||
@@ -85,20 +85,7 @@ def update_thread(request, course_id, thread_id):
|
||||
thread.update_attributes(**extract(request.POST, ['body', 'title', 'tags']))
|
||||
thread.save()
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'thread': thread.to_dict(),
|
||||
'course_id': course_id,
|
||||
}
|
||||
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({
|
||||
'html': html,
|
||||
'content': thread.to_dict(),
|
||||
'annotated_content_info': annotated_content_info,
|
||||
})
|
||||
return ajax_content_response(request, course_id, thread.to_dict(), 'discussion/ajax_update_thread.html')
|
||||
else:
|
||||
return JsonResponse(thread.to_dict())
|
||||
|
||||
@@ -116,20 +103,7 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None):
|
||||
user = cc.User.from_django_user(request.user)
|
||||
user.follow(comment.thread)
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'comment': comment.to_dict(),
|
||||
'course_id': course_id,
|
||||
}
|
||||
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({
|
||||
'html': html,
|
||||
'content': comment.to_dict(),
|
||||
'annotated_content_info': annotated_content_info,
|
||||
})
|
||||
return ajax_content_response(request, course_id, comment.to_dict(), 'discussion/ajax_create_comment.html')
|
||||
else:
|
||||
return JsonResponse(comment.to_dict())
|
||||
|
||||
@@ -155,20 +129,7 @@ def update_comment(request, course_id, comment_id):
|
||||
comment.update_attributes(**extract(request.POST, ['body']))
|
||||
comment.save()
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'comment': comment.to_dict(),
|
||||
'course_id': course_id,
|
||||
}
|
||||
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({
|
||||
'html': html,
|
||||
'content': comment.to_dict(),
|
||||
'annotated_content_info': annotated_content_info,
|
||||
})
|
||||
return ajax_content_response(request, course_id, comment.to_dict(), 'discussion/ajax_update_comment.html')
|
||||
else:
|
||||
return JsonResponse(comment.to_dict()),
|
||||
|
||||
@@ -329,15 +290,12 @@ def update_moderator_status(request, course_id, user_id):
|
||||
def search_similar_threads(request, course_id, commentable_id):
|
||||
text = request.GET.get('text', None)
|
||||
if text:
|
||||
return JsonResponse(
|
||||
cc.search_similar_threads(
|
||||
course_id,
|
||||
recursive=False,
|
||||
query_params={
|
||||
'text': text,
|
||||
'commentable_id': commentable_id,
|
||||
},
|
||||
))
|
||||
query_params = {
|
||||
'text': text,
|
||||
'commentable_id': commentable_id,
|
||||
}
|
||||
result = cc.search_similar_threads(course_id, recursive=False, query_params=query_params)
|
||||
return JsonResponse(result)
|
||||
else:
|
||||
return JsonResponse([])
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ def render_discussion(request, course_id, threads, *args, **kwargs):
|
||||
}[discussion_type]()
|
||||
|
||||
print "start annotating"
|
||||
annotated_content_infos = map(lambda x: utils.get_annotated_content_infos(course_id, x, request.user, type='thread'), threads)
|
||||
annotated_content_infos = map(lambda x: utils.get_annotated_content_infos(course_id, x, request.user), threads)
|
||||
print "start merging annotations"
|
||||
annotated_content_info = reduce(utils.merge_dict, annotated_content_infos, {})
|
||||
print "finished annotating"
|
||||
@@ -161,32 +161,11 @@ def forum_form_discussion(request, course_id):
|
||||
}
|
||||
return render_to_response('discussion/index.html', context)
|
||||
|
||||
def get_annotated_content_info(course_id, content, user, is_thread):
|
||||
permissions = {
|
||||
'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"),
|
||||
'can_openclose': check_permissions_by_view(user, course_id, content, "openclose_thread") if is_thread else False,
|
||||
'can_vote': check_permissions_by_view(user, course_id, content, "vote_for_thread" if is_thread else "vote_for_comment"),
|
||||
}
|
||||
return permissions
|
||||
|
||||
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):
|
||||
|
||||
thread = cc.Thread.find(thread_id).retrieve(recursive=True)
|
||||
|
||||
annotated_content_info = utils.get_annotated_content_infos(course_id, thread=thread.to_dict(), \
|
||||
user=request.user, type='thread')
|
||||
annotated_content_info = utils.get_annotated_content_infos(course_id, thread=thread.to_dict(), user=request.user)
|
||||
|
||||
context = {
|
||||
'discussion_id': discussion_id,
|
||||
@@ -203,7 +182,7 @@ def single_thread(request, course_id, discussion_id, thread_id):
|
||||
if request.is_ajax():
|
||||
|
||||
thread = cc.Thread.find(thread_id).retrieve(recursive=True)
|
||||
annotated_content_info = utils.get_annotated_content_infos(course_id, thread, request.user, type='thread')
|
||||
annotated_content_info = utils.get_annotated_content_infos(course_id, thread, request.user)
|
||||
context = {'thread': thread.to_dict(), 'course_id': course_id}
|
||||
html = render_to_string('discussion/_ajax_single_thread.html', context)
|
||||
|
||||
|
||||
@@ -154,23 +154,23 @@ class QueryCountDebugMiddleware(object):
|
||||
logging.info('%s queries run, total %s seconds' % (len(connection.queries), total_time))
|
||||
return response
|
||||
|
||||
def get_annotated_content_info(course_id, content, user, type):
|
||||
def get_annotated_content_info(course_id, content, user):
|
||||
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"),
|
||||
'can_openclose': check_permissions_by_view(user, course_id, content, "openclose_thread") if type == 'thread' else False,
|
||||
'can_vote': check_permissions_by_view(user, course_id, content, "vote_for_thread" if type == 'thread' else "vote_for_comment"),
|
||||
'editable': check_permissions_by_view(user, course_id, content, "update_thread" if content['type'] == 'thread' else "update_comment"),
|
||||
'can_reply': check_permissions_by_view(user, course_id, content, "create_comment" if content['type'] == 'thread' else "create_sub_comment"),
|
||||
'can_endorse': check_permissions_by_view(user, course_id, content, "endorse_comment") if content['type'] == 'comment' else False,
|
||||
'can_delete': check_permissions_by_view(user, course_id, content, "delete_thread" if content['type'] == 'thread' else "delete_comment"),
|
||||
'can_openclose': check_permissions_by_view(user, course_id, content, "openclose_thread") if content['type'] == 'thread' else False,
|
||||
'can_vote': check_permissions_by_view(user, course_id, content, "vote_for_thread" if content['type'] == 'thread' else "vote_for_comment"),
|
||||
}
|
||||
|
||||
def get_annotated_content_infos(course_id, thread, user, type='thread'):
|
||||
def get_annotated_content_infos(course_id, thread, user):
|
||||
infos = {}
|
||||
def _annotate(content, type):
|
||||
infos[str(content['id'])] = get_annotated_content_info(course_id, content, user, type)
|
||||
def _annotate(content):
|
||||
infos[str(content['id'])] = get_annotated_content_info(course_id, content, user)
|
||||
for child in content.get('children', []):
|
||||
_annotate(child, 'comment')
|
||||
_annotate(thread, type)
|
||||
_annotate(child)
|
||||
_annotate(thread)
|
||||
return infos
|
||||
|
||||
def pluralize(singular_term, count):
|
||||
|
||||
@@ -10,7 +10,7 @@ class Comment(models.Model):
|
||||
'endorsed', 'parent_id', 'thread_id',
|
||||
'username', 'votes', 'user_id', 'closed',
|
||||
'created_at', 'updated_at', 'depth',
|
||||
'at_position_list',
|
||||
'at_position_list', 'type',
|
||||
]
|
||||
|
||||
updatable_fields = [
|
||||
|
||||
@@ -10,7 +10,7 @@ class Thread(models.Model):
|
||||
'course_id', 'closed', 'tags', 'votes',
|
||||
'commentable_id', 'username', 'user_id',
|
||||
'created_at', 'updated_at', 'comments_count',
|
||||
'at_position_list', 'children',
|
||||
'at_position_list', 'children', 'type',
|
||||
]
|
||||
|
||||
updatable_fields = [
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<%namespace name="renderer" file="_thread.html"/>
|
||||
|
||||
${renderer.render_comment(comment)}
|
||||
${renderer.render_comment(content)}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<%namespace name="renderer" file="_thread.html"/>
|
||||
|
||||
${renderer.render_thread(course_id, thread)}
|
||||
${renderer.render_thread(course_id, content)}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<%namespace name="renderer" file="_thread.html"/>
|
||||
|
||||
${renderer.render_content(comment, "comment")}
|
||||
${renderer.render_content(content, "comment")}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<%namespace name="renderer" file="_thread.html"/>
|
||||
|
||||
${renderer.render_content(thread, "thread")}
|
||||
${renderer.render_content(content, "thread")}
|
||||
|
||||
Reference in New Issue
Block a user