some starting refactor ideas
This commit is contained in:
@@ -5,7 +5,7 @@ import os.path
|
||||
import logging
|
||||
import urlparse
|
||||
|
||||
import comment_client
|
||||
import comment_client as c
|
||||
|
||||
from django.core import exceptions
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@@ -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.permissions import check_permissions_by_view
|
||||
from collection import defaultdict
|
||||
import functools
|
||||
|
||||
|
||||
|
||||
def permitted(fn):
|
||||
@functools.wraps(fn)
|
||||
def wrapper(request, *args, **kwargs):
|
||||
def fetch_content():
|
||||
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:
|
||||
content = comment_client.get_comment(kwargs["comment_id"])
|
||||
content = dict(c.Comment.find(kwargs["comment_id"]))
|
||||
else:
|
||||
content = None
|
||||
return content
|
||||
@@ -40,258 +42,240 @@ def permitted(fn):
|
||||
return JsonError("unauthorized")
|
||||
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
|
||||
@login_required
|
||||
@permitted
|
||||
def create_thread(request, course_id, commentable_id):
|
||||
attributes = extract(request.POST, ['body', 'title', 'tags'])
|
||||
attributes['user_id'] = request.user.id
|
||||
attributes['course_id'] = course_id
|
||||
if request.POST.get('anonymous', 'false').lower() == 'true':
|
||||
attributes['anonymous'] = True
|
||||
if request.POST.get('autowatch', 'false').lower() == 'true':
|
||||
attributes['auto_subscribe'] = True
|
||||
response = comment_client.create_thread(commentable_id, attributes)
|
||||
post = request.POST
|
||||
thread = c.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)
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'course_id': course_id,
|
||||
'thread': response,
|
||||
'thread': dict(thread),
|
||||
}
|
||||
html = render_to_string('discussion/ajax_create_thread.html', context)
|
||||
return JsonResponse({
|
||||
'html': html,
|
||||
'content': response,
|
||||
'content': dict(thread),
|
||||
})
|
||||
else:
|
||||
return JsonResponse(response)
|
||||
return JsonResponse(dict(thread))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def update_thread(request, course_id, thread_id):
|
||||
attributes = extract(request.POST, ['body', 'title', 'tags'])
|
||||
response = comment_client.update_thread(thread_id, attributes)
|
||||
thread = c.Thread.find(thread_id)
|
||||
thread.update_attributes(**extract(request.POST, ['body', 'title', 'tags']))
|
||||
thread.save()
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'thread': response,
|
||||
'thread': dict(thread),
|
||||
'course_id': course_id,
|
||||
}
|
||||
html = render_to_string('discussion/ajax_update_thread.html', context)
|
||||
return JsonResponse({
|
||||
'html': html,
|
||||
'content': response,
|
||||
'content': dict(thread),
|
||||
})
|
||||
else:
|
||||
return JsonResponse(response)
|
||||
return JsonResponse(dict(thread))
|
||||
|
||||
def _create_comment(request, course_id, _response_from_attributes):
|
||||
attributes = extract(request.POST, ['body'])
|
||||
attributes['user_id'] = request.user.id
|
||||
attributes['course_id'] = course_id
|
||||
if request.POST.get('anonymous', 'false').lower() == 'true':
|
||||
attributes['anonymous'] = True
|
||||
if request.POST.get('autowatch', 'false').lower() == 'true':
|
||||
attributes['auto_subscribe'] = True
|
||||
response = _response_from_attributes(attributes)
|
||||
def _create_comment(request, course_id, thread_id=None, parent_id=None):
|
||||
post = request.POST
|
||||
comment = c.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)
|
||||
if post.get('auto_subscribe', 'false').lower() == 'true':
|
||||
user = c.User.from_django_user(request.user)
|
||||
user.subscribe(comment.thread)
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'comment': response,
|
||||
'comment': dict(comment),
|
||||
}
|
||||
html = render_to_string('discussion/ajax_create_comment.html', context)
|
||||
return JsonResponse({
|
||||
'html': html,
|
||||
'content': response,
|
||||
'content': dict(comment),
|
||||
})
|
||||
else:
|
||||
return JsonResponse(response)
|
||||
return JsonResponse(dict(comment))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def create_comment(request, course_id, thread_id):
|
||||
def _response_from_attributes(attributes):
|
||||
return comment_client.create_comment(thread_id, attributes)
|
||||
return _create_comment(request, course_id, _response_from_attributes)
|
||||
return _create_comment(request, course_id, thread_id=thread_id)
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def delete_thread(request, course_id, thread_id):
|
||||
response = comment_client.delete_thread(thread_id)
|
||||
return JsonResponse(response)
|
||||
thread = c.Thread.find(thread_id)
|
||||
thread.delete()
|
||||
return JsonResponse(dict(thread))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def update_comment(request, course_id, comment_id):
|
||||
attributes = extract(request.POST, ['body'])
|
||||
response = comment_client.update_comment(comment_id, attributes)
|
||||
comment = c.Comment.find(comment_id)
|
||||
comment.update_attributes(**extract(request.POST, ['body']))
|
||||
comment.save()
|
||||
if request.is_ajax():
|
||||
context = {
|
||||
'comment': response,
|
||||
'comment': dict(comment),
|
||||
'course_id': course_id,
|
||||
}
|
||||
html = render_to_string('discussion/ajax_update_comment.html', context)
|
||||
return JsonResponse({
|
||||
'html': html,
|
||||
'content': response,
|
||||
'content': dict(comment),
|
||||
})
|
||||
else:
|
||||
return JsonResponse(response)
|
||||
return JsonResponse(dict(comment)),
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def endorse_comment(request, course_id, comment_id):
|
||||
attributes = extract(request.POST, ['endorsed'])
|
||||
response = comment_client.update_comment(comment_id, attributes)
|
||||
return JsonResponse(response)
|
||||
comment = c.Comment.find(comment_id)
|
||||
comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true'
|
||||
comment.save()
|
||||
return JsonResponse(dict(response))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def openclose_thread(request, course_id, thread_id):
|
||||
attributes = extract(request.POST, ['closed'])
|
||||
response = comment_client.update_thread(thread_id, attributes)
|
||||
return JsonResponse(response)
|
||||
comment = c.Comment.find(comment_id)
|
||||
comment.endorsed = request.POST.get('closed', 'false').lower() == 'true'
|
||||
comment.save()
|
||||
return JsonResponse(dict(response))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def create_sub_comment(request, course_id, comment_id):
|
||||
def _response_from_attributes(attributes):
|
||||
return comment_client.create_sub_comment(comment_id, attributes)
|
||||
return _create_comment(request, course_id, _response_from_attributes)
|
||||
return _create_comment(request, course_id, parent_id=comment_id)
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def delete_comment(request, course_id, comment_id):
|
||||
response = comment_client.delete_comment(comment_id)
|
||||
return JsonResponse(response)
|
||||
comment = c.Comment.find(comment_id)
|
||||
comment.delete()
|
||||
return JsonResponse(dict(response))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def vote_for_comment(request, course_id, comment_id, value):
|
||||
user_id = request.user.id
|
||||
response = comment_client.vote_for_comment(comment_id, user_id, value)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
comment = c.Comment.find(comment_id)
|
||||
user.vote(comment, value)
|
||||
return JsonResponse(dict(comment))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def undo_vote_for_comment(request, course_id, comment_id):
|
||||
user_id = request.user.id
|
||||
response = comment_client.undo_vote_for_comment(comment_id, user_id)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
comment = c.Comment.find(comment_id)
|
||||
user.unvote(comment)
|
||||
return JsonResponse(dict(comment))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def vote_for_thread(request, course_id, thread_id, value):
|
||||
user_id = request.user.id
|
||||
response = comment_client.vote_for_thread(thread_id, user_id, value)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
thread = c.Thread.find(thread_id)
|
||||
user.vote(thread, value)
|
||||
return JsonResponse(dict(thread))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def undo_vote_for_thread(request, course_id, thread_id):
|
||||
user_id = request.user.id
|
||||
response = comment_client.undo_vote_for_thread(thread_id, user_id)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
thread = c.Thread.find(thread_id)
|
||||
user.unvote(thread)
|
||||
return JsonResponse(dict(thread))
|
||||
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def follow_thread(request, course_id, thread_id):
|
||||
user_id = request.user.id
|
||||
response = comment_client.subscribe_thread(user_id, thread_id)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
thread = c.Thread.find(thread_id)
|
||||
user.follow(thread)
|
||||
return JsonResponse({})
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def follow_commentable(request, course_id, commentable_id):
|
||||
user_id = request.user.id
|
||||
response = comment_client.subscribe_commentable(user_id, commentable_id)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
commentable = c.Commentable.find(commentable_id)
|
||||
user.follow(commentable)
|
||||
return JsonResponse({})
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def follow_user(request, course_id, followed_user_id):
|
||||
user_id = request.user.id
|
||||
response = comment_client.follow(user_id, followed_user_id)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
followed_user = c.User.find(followed_user_id)
|
||||
user.follow(followed_user)
|
||||
return JsonResponse({})
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def unfollow_thread(request, course_id, thread_id):
|
||||
user_id = request.user.id
|
||||
response = comment_client.unsubscribe_thread(user_id, thread_id)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
thread = c.Thread.find(thread_id)
|
||||
user.unfollow(thread)
|
||||
return JsonResponse({})
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
def unfollow_commentable(request, course_id, commentable_id):
|
||||
user_id = request.user.id
|
||||
response = comment_client.unsubscribe_commentable(user_id, commentable_id)
|
||||
return JsonResponse(response)
|
||||
user = c.User.from_django_user(request.user)
|
||||
commentable = c.Commentable.find(commentable_id)
|
||||
user.unfollow(commentable)
|
||||
return JsonResponse({})
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@permitted
|
||||
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_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)
|
||||
user = c.User.from_django_user(request.user)
|
||||
followed_user = c.User.find(followed_user_id)
|
||||
user.unfollow(followed_user)
|
||||
return JsonResponse({})
|
||||
|
||||
@require_GET
|
||||
def search_similar_threads(request, course_id, commentable_id):
|
||||
text = request.GET.get('text', None)
|
||||
if text:
|
||||
return JsonResponse(
|
||||
comment_client.search_similar_threads(
|
||||
c.search_similar_threads(
|
||||
course_id,
|
||||
recursive=False,
|
||||
query_params={
|
||||
@@ -307,7 +291,7 @@ def tags_autocomplete(request, course_id):
|
||||
value = request.GET.get('q', None)
|
||||
results = []
|
||||
if value:
|
||||
results = comment_client.tags_autocomplete(value)
|
||||
results = c.tags_autocomplete(value)
|
||||
return JsonResponse(results)
|
||||
|
||||
@require_POST
|
||||
|
||||
@@ -15,7 +15,7 @@ import django_comment_client.utils as utils
|
||||
from urllib import urlencode
|
||||
|
||||
import json
|
||||
import comment_client
|
||||
import comment_client as c
|
||||
import dateutil
|
||||
|
||||
from django_comment_client.permissions import check_permissions_by_view
|
||||
@@ -59,7 +59,7 @@ def render_discussion(request, course_id, threads, discussion_id=None, \
|
||||
context = {
|
||||
'threads': threads,
|
||||
'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,
|
||||
'request': request,
|
||||
'performed_search': _should_perform_search(request),
|
||||
@@ -86,13 +86,17 @@ def get_threads(request, course_id, discussion_id):
|
||||
'sort_order': request.GET.get('sort_order', 'desc'),
|
||||
'text': request.GET.get('text', ''),
|
||||
'tags': request.GET.get('tags', ''),
|
||||
'commentable_id': discussion_id,
|
||||
'course_id': course_id,
|
||||
}
|
||||
|
||||
if _should_perform_search(request):
|
||||
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))
|
||||
else:
|
||||
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.search(query_params)
|
||||
|
||||
#if _should_perform_search(request):
|
||||
# query_params['commentable_id'] = discussion_id
|
||||
# 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['num_pages'] = num_pages
|
||||
@@ -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, \
|
||||
query_params=query_params)
|
||||
|
||||
recent_active_threads = comment_client.search_recent_active_threads(
|
||||
recent_active_threads = c.search_recent_active_threads(
|
||||
course_id,
|
||||
recursive=False,
|
||||
query_params={'follower_id': request.user.id,
|
||||
'commentable_id': discussion_id},
|
||||
)
|
||||
|
||||
trending_tags = comment_client.search_trending_tags(
|
||||
trending_tags = c.search_trending_tags(
|
||||
course_id,
|
||||
query_params={'commentable_id': discussion_id},
|
||||
)
|
||||
@@ -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):
|
||||
|
||||
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)
|
||||
|
||||
context = {
|
||||
'discussion_id': discussion_id,
|
||||
'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),
|
||||
'course_id': course_id,
|
||||
'request': request,
|
||||
@@ -186,9 +191,9 @@ def single_thread(request, course_id, discussion_id, thread_id):
|
||||
|
||||
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)
|
||||
context = {'thread': thread}
|
||||
context = {'thread': dict(thread)}
|
||||
html = render_to_string('discussion/_ajax_single_thread.html', context)
|
||||
|
||||
return utils.JsonResponse({
|
||||
@@ -218,7 +223,7 @@ def search(request, course_id):
|
||||
commentable_id = request.GET.get('commentable_id', None)
|
||||
tags = request.GET.get('tags', None)
|
||||
|
||||
threads = comment_client.search_threads({
|
||||
threads = c.Threads.search({
|
||||
'text': text,
|
||||
'commentable_id': commentable_id,
|
||||
'tags': tags,
|
||||
|
||||
Reference in New Issue
Block a user