Commit c2f27a5f by Rocky Duan

some starting refactor ideas

parent f1dabe15
...@@ -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 import comment_client as c
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,17 +19,19 @@ from mitxmako.shortcuts import render_to_response, render_to_string ...@@ -19,17 +19,19 @@ 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):
@functools.wraps(fn) @functools.wraps(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 = comment_client.get_thread(kwargs["thread_id"]) content = dict(c.Thread.find(kwargs["thread_id"]))
elif "comment_id" in kwargs: elif "comment_id" in kwargs:
content = comment_client.get_comment(kwargs["comment_id"]) content = dict(c.Comment.find(kwargs["comment_id"]))
else: else:
content = None content = None
return content return content
...@@ -40,258 +42,240 @@ def permitted(fn): ...@@ -40,258 +42,240 @@ def permitted(fn):
return JsonError("unauthorized") return JsonError("unauthorized")
return wrapper return wrapper
def thread_author_only(fn):
def verified_fn(request, *args, **kwargs):
thread_id = kwargs.get('thread_id', False)
thread = comment_client.get_thread(thread_id)
if str(request.user.id) == str(thread['user_id']):
return fn(request, *args, **kwargs)
else:
return JsonError("unauthorized")
return verified_fn
def comment_author_only(fn):
def verified_fn(request, *args, **kwargs):
comment_id = kwargs.get('comment_id', False)
comment = comment_client.get_comment(comment_id)
if str(request.user.id) == str(comment['user_id']):
return fn(request, *args, **kwargs)
else:
return JsonError("unauthorized")
return verified_fn
def instructor_only(fn):
def verified_fn(request, *args, **kwargs):
if not request.user.is_staff:
return JsonError("unauthorized")
else:
return fn(request, *args, **kwargs)
return verified_fn
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def create_thread(request, course_id, commentable_id): def create_thread(request, course_id, commentable_id):
attributes = extract(request.POST, ['body', 'title', 'tags']) post = request.POST
attributes['user_id'] = request.user.id thread = c.Thread(**extract(post, ['body', 'title', 'tags']))
attributes['course_id'] = course_id thread.anonymous = post.get('anonymous', 'false').lower() == 'true'
if request.POST.get('anonymous', 'false').lower() == 'true': thread.course_id = course_id
attributes['anonymous'] = True thread.user_id = request.user.id
if request.POST.get('autowatch', 'false').lower() == 'true': thread.save()
attributes['auto_subscribe'] = True if post.get('auto_subscribe', 'false').lower() == 'true':
response = comment_client.create_thread(commentable_id, attributes) user = c.User.from_django_user(request.user)
user.subscribe(thread)
if request.is_ajax(): if request.is_ajax():
context = { context = {
'course_id': course_id, 'course_id': course_id,
'thread': response, 'thread': dict(thread),
} }
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': response, 'content': dict(thread),
}) })
else: else:
return JsonResponse(response) return JsonResponse(dict(thread))
@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):
attributes = extract(request.POST, ['body', 'title', 'tags']) thread = c.Thread.find(thread_id)
response = comment_client.update_thread(thread_id, attributes) thread.update_attributes(**extract(request.POST, ['body', 'title', 'tags']))
thread.save()
if request.is_ajax(): if request.is_ajax():
context = { context = {
'thread': response, 'thread': dict(thread),
'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': response, 'content': dict(thread),
}) })
else: else:
return JsonResponse(response) return JsonResponse(dict(thread))
def _create_comment(request, course_id, _response_from_attributes): def _create_comment(request, course_id, thread_id=None, parent_id=None):
attributes = extract(request.POST, ['body']) post = request.POST
attributes['user_id'] = request.user.id comment = c.Comment(**extract(post, ['body']))
attributes['course_id'] = course_id comment.anonymous = post.get('anonymous', 'false').lower() == 'true'
if request.POST.get('anonymous', 'false').lower() == 'true': comment.user_id = request.user.id
attributes['anonymous'] = True comment.course_id = course_id
if request.POST.get('autowatch', 'false').lower() == 'true': comment.thread_id = thread_id
attributes['auto_subscribe'] = True comment.parent_id = parent_id
response = _response_from_attributes(attributes) comment.save()
dict_comment = dict(comment)
if post.get('auto_subscribe', 'false').lower() == 'true':
user = c.User.from_django_user(request.user)
user.subscribe(comment.thread)
if request.is_ajax(): if request.is_ajax():
context = { context = {
'comment': response, 'comment': dict(comment),
} }
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': response, 'content': dict(comment),
}) })
else: else:
return JsonResponse(response) return JsonResponse(dict(comment))
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def create_comment(request, course_id, thread_id): def create_comment(request, course_id, thread_id):
def _response_from_attributes(attributes): return _create_comment(request, course_id, thread_id=thread_id)
return comment_client.create_comment(thread_id, attributes)
return _create_comment(request, course_id, _response_from_attributes)
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def delete_thread(request, course_id, thread_id): def delete_thread(request, course_id, thread_id):
response = comment_client.delete_thread(thread_id) thread = c.Thread.find(thread_id)
return JsonResponse(response) thread.delete()
return JsonResponse(dict(thread))
@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):
attributes = extract(request.POST, ['body']) comment = c.Comment.find(comment_id)
response = comment_client.update_comment(comment_id, attributes) comment.update_attributes(**extract(request.POST, ['body']))
comment.save()
if request.is_ajax(): if request.is_ajax():
context = { context = {
'comment': response, 'comment': dict(comment),
'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': response, 'content': dict(comment),
}) })
else: else:
return JsonResponse(response) return JsonResponse(dict(comment)),
@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):
attributes = extract(request.POST, ['endorsed']) comment = c.Comment.find(comment_id)
response = comment_client.update_comment(comment_id, attributes) comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true'
return JsonResponse(response) comment.save()
return JsonResponse(dict(response))
@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):
attributes = extract(request.POST, ['closed']) comment = c.Comment.find(comment_id)
response = comment_client.update_thread(thread_id, attributes) comment.endorsed = request.POST.get('closed', 'false').lower() == 'true'
return JsonResponse(response) comment.save()
return JsonResponse(dict(response))
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def create_sub_comment(request, course_id, comment_id): def create_sub_comment(request, course_id, comment_id):
def _response_from_attributes(attributes): return _create_comment(request, course_id, parent_id=comment_id)
return comment_client.create_sub_comment(comment_id, attributes)
return _create_comment(request, course_id, _response_from_attributes)
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def delete_comment(request, course_id, comment_id): def delete_comment(request, course_id, comment_id):
response = comment_client.delete_comment(comment_id) comment = c.Comment.find(comment_id)
return JsonResponse(response) comment.delete()
return JsonResponse(dict(response))
@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_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.vote_for_comment(comment_id, user_id, value) comment = c.Comment.find(comment_id)
return JsonResponse(response) user.vote(comment, value)
return JsonResponse(dict(comment))
@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_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.undo_vote_for_comment(comment_id, user_id) comment = c.Comment.find(comment_id)
return JsonResponse(response) user.unvote(comment)
return JsonResponse(dict(comment))
@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_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.vote_for_thread(thread_id, user_id, value) thread = c.Thread.find(thread_id)
return JsonResponse(response) user.vote(thread, value)
return JsonResponse(dict(thread))
@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_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.undo_vote_for_thread(thread_id, user_id) thread = c.Thread.find(thread_id)
return JsonResponse(response) user.unvote(thread)
return JsonResponse(dict(thread))
@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_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.subscribe_thread(user_id, thread_id) thread = c.Thread.find(thread_id)
return JsonResponse(response) user.follow(thread)
return JsonResponse({})
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def follow_commentable(request, course_id, commentable_id): def follow_commentable(request, course_id, commentable_id):
user_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.subscribe_commentable(user_id, commentable_id) commentable = c.Commentable.find(commentable_id)
return JsonResponse(response) user.follow(commentable)
return JsonResponse({})
@require_POST @require_POST
@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_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.follow(user_id, followed_user_id) followed_user = c.User.find(followed_user_id)
return JsonResponse(response) user.follow(followed_user)
return JsonResponse({})
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def unfollow_thread(request, course_id, thread_id): def unfollow_thread(request, course_id, thread_id):
user_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.unsubscribe_thread(user_id, thread_id) thread = c.Thread.find(thread_id)
return JsonResponse(response) user.unfollow(thread)
return JsonResponse({})
@require_POST @require_POST
@login_required @login_required
@permitted @permitted
def unfollow_commentable(request, course_id, commentable_id): def unfollow_commentable(request, course_id, commentable_id):
user_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.unsubscribe_commentable(user_id, commentable_id) commentable = c.Commentable.find(commentable_id)
return JsonResponse(response) user.unfollow(commentable)
return JsonResponse({})
@require_POST @require_POST
@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_id = request.user.id user = c.User.from_django_user(request.user)
response = comment_client.unfollow(user_id, followed_user_id) followed_user = c.User.find(followed_user_id)
return JsonResponse(response) user.unfollow(followed_user)
return JsonResponse({})
@require_POST
@login_required
def unfollow_user(request, course_id, followed_user_id):
user_id = request.user.id
response = comment_client.unfollow(user_id, followed_user_id)
return JsonResponse(response)
@require_GET @require_GET
def search_similar_threads(request, course_id, commentable_id): 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(
comment_client.search_similar_threads( c.search_similar_threads(
course_id, course_id,
recursive=False, recursive=False,
query_params={ query_params={
...@@ -307,7 +291,7 @@ def tags_autocomplete(request, course_id): ...@@ -307,7 +291,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 = comment_client.tags_autocomplete(value) results = c.tags_autocomplete(value)
return JsonResponse(results) return JsonResponse(results)
@require_POST @require_POST
......
...@@ -15,7 +15,7 @@ import django_comment_client.utils as utils ...@@ -15,7 +15,7 @@ import django_comment_client.utils as utils
from urllib import urlencode from urllib import urlencode
import json import json
import comment_client import comment_client as c
import dateutil import dateutil
from django_comment_client.permissions import check_permissions_by_view from django_comment_client.permissions import check_permissions_by_view
...@@ -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': comment_client.get_user_info(request.user.id, raw=True), 'user_info': dict(c.User.from_django_user(request.user)),#comment_client.get_user_info(request.user.id, raw=True),
'course_id': course_id, 'course_id': course_id,
'request': request, 'request': request,
'performed_search': _should_perform_search(request), 'performed_search': _should_perform_search(request),
...@@ -86,13 +86,17 @@ def get_threads(request, course_id, discussion_id): ...@@ -86,13 +86,17 @@ def get_threads(request, course_id, discussion_id):
'sort_order': request.GET.get('sort_order', 'desc'), 'sort_order': request.GET.get('sort_order', 'desc'),
'text': request.GET.get('text', ''), 'text': request.GET.get('text', ''),
'tags': request.GET.get('tags', ''), 'tags': request.GET.get('tags', ''),
'commentable_id': discussion_id,
'course_id': course_id,
} }
if _should_perform_search(request): threads, page, num_pages = c.Thread.search(query_params)
query_params['commentable_id'] = discussion_id
threads, page, num_pages = comment_client.search_threads(course_id, recursive=False, query_params=utils.strip_none(query_params)) #if _should_perform_search(request):
else: # query_params['commentable_id'] = discussion_id
threads, page, num_pages = comment_client.get_threads(discussion_id, recursive=False, query_params=utils.strip_none(query_params)) # threads, page, num_pages = c.Thread.searchcomment_client.search_threads(course_id, recursive=False, query_params=utils.strip_none(query_params))
#else:
# threads, page, num_pages = comment_client.get_threads(discussion_id, recursive=False, query_params=utils.strip_none(query_params))
query_params['page'] = page query_params['page'] = page
query_params['num_pages'] = num_pages query_params['num_pages'] = num_pages
...@@ -122,14 +126,14 @@ def forum_form_discussion(request, course_id, discussion_id): ...@@ -122,14 +126,14 @@ def forum_form_discussion(request, course_id, discussion_id):
content = render_forum_discussion(request, course_id, threads, discussion_id=discussion_id, \ content = render_forum_discussion(request, course_id, threads, discussion_id=discussion_id, \
query_params=query_params) query_params=query_params)
recent_active_threads = comment_client.search_recent_active_threads( recent_active_threads = c.search_recent_active_threads(
course_id, course_id,
recursive=False, recursive=False,
query_params={'follower_id': request.user.id, query_params={'follower_id': request.user.id,
'commentable_id': discussion_id}, 'commentable_id': discussion_id},
) )
trending_tags = comment_client.search_trending_tags( trending_tags = c.search_trending_tags(
course_id, course_id,
query_params={'commentable_id': discussion_id}, query_params={'commentable_id': discussion_id},
) )
...@@ -167,15 +171,16 @@ def get_annotated_content_infos(thread, user, is_thread=True): ...@@ -167,15 +171,16 @@ def get_annotated_content_infos(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 = comment_client.get_thread(thread_id, recursive=True) thread = c.Thread.find(thread_id).retrieve_with_comments()
#comment_client.get_thread(thread_id, recursive=True)
annotated_content_info = get_annotated_content_infos(thread=thread, \ annotated_content_info = get_annotated_content_infos(thread=dict(thread), \
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': comment_client.get_user_info(request.user.id, raw=True), 'user_info': dict(c.User.from_django_user(request.user)),#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,
...@@ -186,9 +191,9 @@ def single_thread(request, course_id, discussion_id, thread_id): ...@@ -186,9 +191,9 @@ def single_thread(request, course_id, discussion_id, thread_id):
if request.is_ajax(): if request.is_ajax():
thread = comment_client.get_thread(thread_id, recursive=True) thread = c.Thread.find(thread_id).retrieve_with_comments()#comment_client.get_thread(thread_id, recursive=True)
annotated_content_info = get_annotated_content_infos(thread, request.user) annotated_content_info = get_annotated_content_infos(thread, request.user)
context = {'thread': thread} context = {'thread': dict(thread)}
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({
...@@ -218,7 +223,7 @@ def search(request, course_id): ...@@ -218,7 +223,7 @@ def search(request, course_id):
commentable_id = request.GET.get('commentable_id', None) commentable_id = request.GET.get('commentable_id', None)
tags = request.GET.get('tags', None) tags = request.GET.get('tags', None)
threads = comment_client.search_threads({ threads = c.Threads.search({
'text': text, 'text': text,
'commentable_id': commentable_id, 'commentable_id': commentable_id,
'tags': tags, 'tags': tags,
......
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