Commit 0c311b8f by Rocky Duan

search for similar posts without any styling..

parent 77900bd7
......@@ -23,8 +23,8 @@ urlpatterns = patterns('django_comment_client.base.views',
url(r'comments/(?P<comment_id>[\w\-]+)/unvote$', 'undo_vote_for_comment', name='undo_vote_for_comment'),
url(r'(?P<commentable_id>[\w\-]+)/threads/create$', 'create_thread', name='create_thread'),
# TODO should we search within the board?
url(r'(?P<commentable_id>[\w\-]+)/threads/search_similar$', 'search_similar_threads', name='search_similar_threads'),
url(r'(?P<commentable_id>[\w\-]+)/follow$', 'follow_commentable', name='follow_commentable'),
url(r'(?P<commentable_id>[\w\-]+)/unfollow$', 'unfollow_commentable', name='unfollow_commentable'),
url(r'search$', 'search', name='search'),
)
......@@ -235,17 +235,28 @@ def unfollow_user(request, course_id, followed_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)
@require_GET
def search(request, course_id):
def search_similar_threads(request, course_id, commentable_id):
text = request.GET.get('text', None)
commentable_id = request.GET.get('commentable_id', None)
tags = request.GET.get('tags', None)
response = comment_client.search_threads({
'text': text,
'commentable_id': commentable_id,
'tags': tags,
})
return JsonResponse(response)
if text:
return JsonResponse(
comment_client.search_similar_threads(
course_id,
recursive=False,
query_params={
'text': text,
'commentable_id': commentable_id,
},
))
else:
return JsonResponse([])
@require_GET
def tags_autocomplete(request, course_id):
......
......@@ -16,19 +16,24 @@ def delete_threads(commentable_id, *args, **kwargs):
return _perform_request('delete', _url_for_commentable_threads(commentable_id), *args, **kwargs)
def get_threads(commentable_id, recursive=False, query_params={}, *args, **kwargs):
default_params = {'page': 1, 'per_page': 20}
default_params = {'page': 1, 'per_page': 20, 'recursive': recursive}
attributes = dict(default_params.items() + query_params.items())
response = _perform_request('get', _url_for_threads(commentable_id), \
attributes, *args, **kwargs)
return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1)
def search_threads(course_id, recursive=False, query_params={}, *args, **kwargs):
default_params = {'page': 1, 'per_page': 20, 'course_id': course_id}
default_params = {'page': 1, 'per_page': 20, 'course_id': course_id, 'recursive': recursive}
attributes = dict(default_params.items() + query_params.items())
response = _perform_request('get', _url_for_search_threads(), \
attributes, *args, **kwargs)
return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1)
def search_similar_threads(course_id, recursive=False, query_params={}, *args, **kwargs):
default_params = {'course_id': course_id, 'recursive': recursive}
attributes = dict(default_params.items() + query_params.items())
return _perform_request('get', _url_for_search_similar_threads(), attributes, *args, **kwargs)
def create_user(attributes, *args, **kwargs):
return _perform_request('post', _url_for_users(), attributes, *args, **kwargs)
......@@ -159,6 +164,9 @@ def _url_for_user(user_id):
def _url_for_search_threads():
return "{prefix}/search/threads".format(prefix=PREFIX)
def _url_for_search_similar_threads():
return "{prefix}/search/threads/more_like_this".format(prefix=PREFIX)
def _url_for_threads_tags():
return "{prefix}/threads/tags".format(prefix=PREFIX)
......@@ -167,3 +175,4 @@ def _url_for_threads_tags_autocomplete():
def _url_for_users():
return "{prefix}/users".format(prefix=PREFIX)
......@@ -61,6 +61,29 @@ initializeFollowDiscussion = (discussion) ->
$local(".new-post-form").hide()
$local(".discussion-new-post").show()
handleSimilarPost = (elem) ->
Discussion.safeAjax
$elem: $(elem)
url: Discussion.urlFor 'search_similar_threads', id
type: "GET"
dateType: 'json'
data:
text: $local(".new-post-title").val()
success: (response, textStatus) ->
$wrapper = $local(".new-post-similar-posts-wrapper")
$similarPosts = $local(".new-post-similar-posts")
$similarPosts.empty()
if $.type(response) == "array" and response.length
$wrapper.show()
for thread in response
#singleThreadUrl = Discussion.urlFor 'retrieve_single_thread
$similarPost = $("<a>").addClass("simialr-post")
.html(thread["title"])
.attr("href", "javascript:void(0)") #TODO
.appendTo($similarPosts)
else
$wrapper.hide()
handleNewPost = (elem) ->
newPostForm = $local(".new-post-form")
if newPostForm.length
......@@ -76,6 +99,9 @@ initializeFollowDiscussion = (discussion) ->
$local(".new-post-tags").tagsInput Discussion.tagsInputOptions()
$local(".new-post-title").blur ->
handleSimilarPost(this)
$local(".discussion-submit-post").click ->
handleSubmitNewPost(this)
$local(".discussion-cancel-post").click ->
......
......@@ -10,6 +10,10 @@ Discussion = @Discussion
<form class="new-post-form" _id="{{discussion_id}}">
<ul class="discussion-errors"></ul>
<input type="text" class="new-post-title title-input" placeholder="Title"/>
<div class="new-post-similar-posts-wrapper" style="display: none">
Do you mean...
<div class="new-post-similar-posts"></div>
</div>
<div class="new-post-body body-input"></div>
<input class="new-post-tags" placeholder="Tags"/>
<div class = "new-post-control">
......
......@@ -21,6 +21,7 @@ wmdEditors = {}
follow_discussion : "/courses/#{$$course_id}/discussion/#{param}/follow"
unfollow_discussion : "/courses/#{$$course_id}/discussion/#{param}/unfollow"
create_thread : "/courses/#{$$course_id}/discussion/#{param}/threads/create"
search_similar_threads : "/courses/#{$$course_id}/discussion/#{param}/threads/search_similar"
update_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/update"
create_comment : "/courses/#{$$course_id}/discussion/threads/#{param}/reply"
delete_thread : "/courses/#{$$course_id}/discussion/threads/#{param}/delete"
......
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