Commit 7dd4d6c7 by Matthew Mongeau

Get voting hooked up

parent 505fb681
This diff is collapsed. Click to expand it.
...@@ -182,6 +182,7 @@ def render_single_thread(request, discussion_id, course_id, thread_id): ...@@ -182,6 +182,7 @@ def render_single_thread(request, discussion_id, course_id, thread_id):
annotated_content_info = utils.get_annotated_content_infos(course_id, thread=thread, user=request.user, user_info=user_info) annotated_content_info = utils.get_annotated_content_infos(course_id, thread=thread, user=request.user, user_info=user_info)
log.debug(annotated_content_info)
context = { context = {
'discussion_id': discussion_id, 'discussion_id': discussion_id,
'thread': thread, 'thread': thread,
...@@ -191,6 +192,7 @@ def render_single_thread(request, discussion_id, course_id, thread_id): ...@@ -191,6 +192,7 @@ def render_single_thread(request, discussion_id, course_id, thread_id):
'discussion_data': json.dumps({ discussion_id: [utils.safe_content(thread)] }), 'discussion_data': json.dumps({ discussion_id: [utils.safe_content(thread)] }),
'threads': threads, 'threads': threads,
} }
return render_to_string('discussion/_single_thread.html', context) return render_to_string('discussion/_single_thread.html', context)
def single_thread(request, course_id, discussion_id, thread_id): def single_thread(request, course_id, discussion_id, thread_id):
...@@ -223,10 +225,18 @@ def single_thread(request, course_id, discussion_id, thread_id): ...@@ -223,10 +225,18 @@ def single_thread(request, course_id, discussion_id, thread_id):
course_id, course_id,
) )
user_info = cc.User.from_django_user(request.user).to_dict()
thread = cc.Thread.find(thread_id).retrieve(recursive=True)
annotated_content_info = utils.get_annotated_content_infos(course_id, thread=thread, user=request.user, user_info=user_info)
context = { context = {
'discussion_id': discussion_id, 'discussion_id': discussion_id,
'csrf': csrf(request)['csrf_token'], 'csrf': csrf(request)['csrf_token'],
'init': '', 'init': '',
'thread': json.dumps(utils.safe_content(thread)),
'annotated_content_info': json.dumps(annotated_content_info),
'content': render_single_thread(request, discussion_id, course_id, thread_id), 'content': render_single_thread(request, discussion_id, course_id, thread_id),
'course': course, 'course': course,
'recent_active_threads': recent_active_threads, 'recent_active_threads': recent_active_threads,
......
...@@ -227,12 +227,15 @@ if Backbone? ...@@ -227,12 +227,15 @@ if Backbone?
@model.set('votes_point', response.votes.point) @model.set('votes_point', response.votes.point)
toggleVote: (event) -> toggleVote: (event) ->
console.log("HERE")
$elem = $(event.target) $elem = $(event.target)
value = $elem.attr("value") value = $elem.attr("value")
if @model.get("voted") == value $elem.toggleClass("is-cast")
@unvote(event) return false
else # if @model.get("voted") == value
@vote(event, value) # @unvote(event)
# else
# @vote(event, value)
toggleEndorse: (event) -> toggleEndorse: (event) ->
$elem = $(event.target) $elem = $(event.target)
......
$ -> class @DiscussionUser
constructor: (content_info) ->
@content_info = content_info
following: (thread) ->
@content_info[thread.id]['subscribed'] == true
voted: (thread) ->
@content_info[thread.id]['voted'] == 'up'
class @DiscussionThreadView extends Backbone.View
events:
"click .discussion-vote-up": "toggleVote"
"click .dogear": "toggleFollowing"
initialize: (options) ->
@user = options['user']
@model.bind "change", @updateModelDetails
updateModelDetails: =>
@$(".votes-count-number").html(@model.get("votes")["up_count"])
render: ->
if @user.following(@model)
@$(".dogear").addClass("is-followed")
if @user.voted(@model)
@$(".vote-btn").addClass("is-cast")
toggleVote: ->
@$(".vote-btn").toggleClass("is-cast")
if @$(".vote-btn").hasClass("is-cast")
@vote()
else
@unvote()
toggleFollowing: ->
@$(".dogear").toggleClass("is-followed")
vote: ->
url = @model.urlFor("upvote")
@$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) + 1)
DiscussionUtil.safeAjax
$elem: @$(".discussion-vote")
url: url
type: "POST"
success: (response, textStatus) =>
if textStatus == 'success'
@model.set(response)
unvote: ->
url = @model.urlFor("unvote")
@$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) - 1)
DiscussionUtil.safeAjax
$elem: @$(".discussion-vote")
url: url
type: "POST"
success: (response, textStatus) =>
if textStatus == 'success'
@model.set(response)
$ ->
window.$$contents = {} window.$$contents = {}
window.$$discussions = {} window.$$discussions = {}
$(".discussion-module").each (index, elem) -> # $(".discussion-module").each (index, elem) ->
view = new DiscussionModuleView(el: elem) # view = new DiscussionModuleView(el: elem)
# $("section.discussion").each (index, elem) ->
# discussionData = DiscussionUtil.getDiscussionData($(elem).attr("_id"))
# discussion = new Discussion()
# discussion.reset(discussionData, {silent: false})
# view = new DiscussionView(el: elem, model: discussion)
$("section.discussion").each (index, elem) -> # if window.$$annotated_content_info?
discussionData = DiscussionUtil.getDiscussionData($(elem).attr("_id")) # DiscussionUtil.bulkUpdateContentInfo(window.$$annotated_content_info)
discussion = new Discussion()
discussion.reset(discussionData, {silent: false})
view = new DiscussionView(el: elem, model: discussion)
if window.$$annotated_content_info? # $userProfile = $(".discussion-sidebar>.user-profile")
DiscussionUtil.bulkUpdateContentInfo(window.$$annotated_content_info) # if $userProfile.length
# console.log "initialize user profile"
# view = new DiscussionUserProfileView(el: $userProfile[0])
$userProfile = $(".discussion-sidebar>.user-profile")
if $userProfile.length
console.log "initialize user profile"
view = new DiscussionUserProfileView(el: $userProfile[0])
<%namespace name="renderer" file="_content_renderer.html"/> <%namespace name="renderer" file="_content_renderer.html"/>
<section class="discussion" _id="${discussion_id | h}"> <article class="discussion-article" data-id="${discussion_id| h}">
<a class="discussion-title" href="javascript:void(0)">${thread['title'] | h}</a> <a href="#" class="dogear"></a>
<p class="posted-details"><span class="timeago" title="${thread['created_at'] | h}">sometime</span> by <a href="#">Foo</a></p> <div class="discussion-post">
<div class="threads"> <header>
${renderer.render_content_with_comments(thread)} <a href="#" class="vote-btn discussion-vote discussion-vote-up"><span class="plus-icon">+</span> <span class='votes-count-number'>${thread['votes']['up_count']}</span></a>
<h1>${thread['title']}</h1>
<p class="posted-details">
<span class="timeago" title="${thread['created_at'] | h}">sometime</span> by
<a href="${thread['user_id']}">${thread['username']}</a>
</p>
</header>
<div class="post-body">
${thread['body']}
</div>
</div> </div>
</section> <ol class="responses">
% for reply in thread.get("children", []):
<li>
<div class="response-body">${reply['body']}</div>
<ol class="comments">
% for comment in reply.get("children", []):
<li><p>${comment['body']}</p></li>
% endfor
</ol>
</li>
% endfor
</ol>
</article>
<%include file="_js_data.html" /> <%include file="_js_data.html" />
<script type="text/javascript">$(document).ready(function() { $("span.timeago").timeago() })</script> <script type="text/javascript">$(document).ready(function() { $("span.timeago").timeago() })</script>
<%! import django_comment_client.helpers as helpers %> <%! import django_comment_client.helpers as helpers %>
<%! from django.template.defaultfilters import escapejs %>
<%inherit file="../main.html" /> <%inherit file="../main.html" />
<%namespace name='static' file='../static_content.html'/> <%namespace name='static' file='../static_content.html'/>
<%block name="bodyclass">discussion</%block> <%block name="bodyclass">discussion</%block>
...@@ -16,48 +17,47 @@ ...@@ -16,48 +17,47 @@
<%include file="../courseware/course_navigation.html" args="active_page='discussion'" /> <%include file="../courseware/course_navigation.html" args="active_page='discussion'" />
<section class="container"> <div class="discussion container">
<div class="discussion-wrapper"> <div class="discussion-body">
<article class="discussion-sidebar"> <div class="sidebar">
<div class="board-selector"> <div class="board-selector">
<a href="#" class="board-drop-btn">Homework / Week 1 <span class="drop-arrow"></span></a> <a href="#" class="board-drop-btn">Homework / Week 1 <span class="drop-arrow"></span></a>
<div class="board-drop-menu"> <div class="board-drop-menu">
</div> </div>
</div> </div>
<div class="sort-bar"> <div class="sort-bar">
<a href="#"><span class="sort-label">Sort by:</span> most recent <span class="drop-arrow"></span></a> <a href="#"><span class="sort-label">Sort by:</span> most recent <span class="drop-arrow"></span></a>
</div> </div>
<div class="post-list-wrapper"> <div class="post-list-wrapper">
<ul class="post-list"> <ul class="post-list">
% for thread in threads: % for discussion_thread in threads:
<li><a href="${helpers.permalink(thread) | h}"><span class="title">${thread['title'] | h}</span> <span class="comments">${thread['comments_count'] | h}</span><span class="votes">+${thread['votes']['up_count'] | h}</span></a></li> <li><a href="${helpers.permalink(discussion_thread) | h}"><span class="title">${discussion_thread['title'] | h}</span> <span class="comments-count">${discussion_thread['comments_count'] | h}</span><span class="votes-count">+${discussion_thread['votes']['up_count'] | h}</span></a></li>
% endfor % endfor
</ul> </ul>
</div> </div>
</article> </div>
<section class="discussion-content">
${content.decode('utf-8')} <div class="discussion-column">
</section> <div class="global-discussion-actions">
</div> <a href="#" class="new-post-btn"><span class="new-post-icon"></span>New Post</a>
<form class="post-search">
</section> <input type="text" placeholder="Search discussions" class="post-search-field">
</form>
<section class="container"> </div>
<div class="course-wrapper">
<section aria-label="Course Navigation" class="course-index">
<nav>
<article class="sidebar-module discussion-sidebar">
</article>
<%include file="_recent_active_posts.html" />
<%include file="_trending_tags.html" />
</nav>
</section>
<section class="course-content">
${content.decode('utf-8')} ${content.decode('utf-8')}
</section>
</div>
</div> </div>
</section> </div>
<script>
$$contents = {}
$$discussions = {}
$(document).ready(function() {
var user = new DiscussionUser(JSON.parse("${annotated_content_info | escapejs}"));
var thread = new Thread(JSON.parse("${thread | escapejs}"));
view = new DiscussionThreadView({el: $(".discussion-article"), model: thread, user: user})
view.render()
});
</script>
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