Skip to content

Commit

Permalink
implemented answer voting
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosmartin committed Oct 9, 2013
1 parent d69ac45 commit 29439dc
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 97 deletions.
5 changes: 3 additions & 2 deletions forum/serializers.py
Expand Up @@ -17,7 +17,7 @@ class AnswerSerializer(serializers.ModelSerializer):

class Meta:
model = Answer
fields = ('question', 'text', 'votes', 'timestamp', 'username')
fields = ('id', 'question', 'text', 'votes', 'timestamp', 'username')

def count_votes(self, obj):
if obj:
Expand All @@ -44,8 +44,9 @@ class Meta:

class AnswerVoteSerializer(serializers.ModelSerializer):

user = serializers.IntegerField(read_only=True)
timestamp = serializers.DateTimeField(read_only=True)

class Meta:
model = AnswerVote
fields = ('question', 'timestamp', 'user', 'value')
fields = ('id', 'answer', 'timestamp', 'user', 'value')
8 changes: 0 additions & 8 deletions forum/static/js/forum/controllers.js
Expand Up @@ -21,16 +21,8 @@ function QuestionCtrl ($scope, $sce, $window, Answer) {
};
}

function AnswersCtrl ($scope, $window, Answer) {
console.log('AnswersCtrl!!!!');
// var questionId = parseInt($window.question_id, 10);
// $scope.answers = Answer.query({question: questionId});

}

angular.module('forum.controllers', ['ngCookies']).
controller('QuestionCtrl', ['$scope', '$sce', '$window', 'Answer', QuestionCtrl]).
controller('AnswersCtrl', ['$scope', '$window', 'Answer', AnswersCtrl]).
// Uses the csrftoken from the cookie
run(function ($http, $cookies) {
$http.defaults.headers.common['X-CSRFToken'] = $cookies['csrftoken'];
Expand Down
62 changes: 0 additions & 62 deletions forum/static/js/question.js

This file was deleted.

138 changes: 117 additions & 21 deletions forum/static/js/vote.js
Expand Up @@ -16,33 +16,40 @@
// Verify if user has voted in up or down
$scope.question_vote = QuestionVote.get({question: $scope.questionId}, function (question_vote){
if ((question_vote.value == undefined) || (question_vote.value == 0)){
$scope.user_question_vote_up = '';
$scope.user_question_vote_down = '';
} else if (question_vote.value == 1) {
$scope.user_question_vote_up = 'active';
$scope.user_question_vote_down = '';
} else if (question_vote.value == -1) {
$scope.user_question_vote_up = '';
$scope.user_question_vote_down = 'active';
}
$scope.user_question_vote_up = '';
$scope.user_question_vote_down = '';
} else if (question_vote.value == 1) {
$scope.user_question_vote_up = 'active';
$scope.user_question_vote_down = '';
} else if (question_vote.value == -1) {
$scope.user_question_vote_up = '';
$scope.user_question_vote_down = 'active';
}
});
$scope.voteUp = function() {
$scope.question_vote = QuestionVote.get({question: $scope.questionId}, function (question_vote){
var question_vote = QuestionVote.get({question: $scope.questionId}, function (question_vote){
if ((question_vote.value == undefined) || (question_vote.value == 0)){
$scope.user_question_vote_up = 'active';
$scope.question_votes += 1;
question_vote.value = 1;
question_vote.$update({question: $scope.questionId});
} else if (question_vote.value == 1) {
$scope.user_question_vote_up = '';
$scope.question_votes -= 1;
question_vote.value = 0;
question_vote.$update({question: $scope.questionId});
} else if (question_vote.value == -1) {
$scope.user_question_vote_up = 'active';
$scope.user_question_vote_down = '';
$scope.question_votes += 2;
question_vote.value = 1;
}
question_vote.$update({question: $scope.questionId});
},
function (httpResponse){
if (httpResponse.status == 404) {
question_vote.question = $scope.questionId;
$scope.user_question_vote_up = 'active';
$scope.question_votes += 1;
question_vote.value = 1;
question_vote.$update({question: $scope.questionId});
}
});
Expand All @@ -53,32 +60,121 @@
$scope.user_question_vote_down = 'active';
$scope.question_votes -= 1;
question_vote.value = -1;
question_vote.$update({question: $scope.questionId});
} else if (question_vote.value == 1) {
$scope.user_question_vote_up = '';
$scope.user_question_vote_down = 'active';
$scope.question_votes -= 2;
question_vote.value = -1;
question_vote.$update({question: $scope.questionId});
} else if (question_vote.value == -1) {
$scope.user_question_vote_down = '';
$scope.question_votes += 1;
question_vote.value = 0;
}
question_vote.$update({question: $scope.questionId});
},
function (httpResponse){
if (httpResponse.status == 404) {
question_vote.question = $scope.questionId;
$scope.user_question_vote_down = 'active';
$scope.question_votes -= 1;
question_vote.value = -1;
question_vote.$update({question: $scope.questionId});
}
});
};
}]);

app.controller('AnswerVoteCtrl', ['$scope', '$window', 'AnswerVote',
function ($scope, $window, AnswerVote) {
// Verify if user has voted in up or down for this answer
$scope.answer_vote = AnswerVote.get({answer: $scope.answer.id},
function (answer_vote){
if ((answer_vote.value == undefined) || (answer_vote.value == 0)){
$scope.user_answer_vote_up = '';
$scope.user_answer_vote_down = '';
} else if (answer_vote.value == 1) {
$scope.user_answer_vote_up = 'active';
$scope.user_answer_vote_down = '';
} else if (answer_vote.value == -1) {
$scope.user_answer_vote_up = '';
$scope.user_answer_vote_down = 'active';
}
$scope.answer.votes = answer_vote.value;
},
function (httpResponse){
if (httpResponse.status == 404) {
$scope.answer.votes = 0;
}
});
$scope.voteUp = function(index) {
$scope.answer_vote = AnswerVote.get({answer: $scope.answer.id},
function(answer_vote) {
if ((answer_vote.value == undefined) || (answer_vote.value == 0)){
answer_vote.answer = $scope.answer.id;
answer_vote.value = 1;
$scope.user_answer_vote_up = 'active';
$scope.answer.votes += 1;
} else if (answer_vote.value == 1) {
$scope.user_answer_vote_up = '';
$scope.answer.votes -= 1;
answer_vote.value = 0;
} else if (answer_vote.value == -1) {
$scope.user_answer_vote_up = 'active';
$scope.user_answer_vote_down = '';
$scope.answer.votes += 2;
answer_vote.value = 1;
}
answer_vote.$update({answer: answer_vote.answer});
},
function (httpResponse){
if (httpResponse.status == 404) {
var new_answer_vote = new AnswerVote();
new_answer_vote.answer = $scope.answer.id;
new_answer_vote.value = 1;
new_answer_vote.$update({answer: new_answer_vote.answer});
$scope.user_answer_vote_up = 'active';
$scope.answer.votes += 1;
}
});
};
$scope.voteDown = function(index) {
$scope.answer_vote = AnswerVote.get({answer: $scope.answer.id}, function(answer_vote) {
if ((answer_vote.value == undefined) || (answer_vote.value == 0)){
$scope.user_answer_vote_down = 'active';
$scope.answer.votes -= 1;
answer_vote.value = -1;
} else if (answer_vote.value == 1) {
$scope.user_answer_vote_up = '';
$scope.user_answer_vote_down = 'active';
$scope.answer.votes -= 2;
answer_vote.value = -1;
} else if (answer_vote.value == -1) {
$scope.user_answer_vote_down = '';
$scope.answer.votes += 1;
answer_vote.value = 0;
}
answer_vote.$update({answer: answer_vote.answer});
},
function (httpResponse){
if (httpResponse.status == 404) {
var new_answer_vote = new AnswerVote();
new_answer_vote.answer = $scope.answer.id;
new_answer_vote.value = -1;
new_answer_vote.$update({answer: new_answer_vote.answer});
$scope.answer.votes -= 1;
$scope.user_answer_vote_down = 'active';
}
});
};
}]);
//
// app.factory('AnswerVote', function($resource){
// return $resource('/api/answer_vote/', {}, {
// create: {method: 'POST'}
// });
// });

app.factory('AnswerVote', function($resource){
return $resource('/api/answer_vote/:answer', {}, {
update: {method: 'PUT'},
});
});
app.factory('QuestionVote', function($resource){
return $resource('/api/question_vote/:question', {}, {
get: {method: 'GET'},
update: {method: 'PUT'},
});
});
Expand Down
8 changes: 4 additions & 4 deletions forum/templates/question.html
Expand Up @@ -90,16 +90,16 @@ <h1>{{ question.answers.count }} Respostas:</h1>

<!-- ANSWER -->
{% verbatim angularjs %}
<div ng-controller="AnswersCtrl">
<div>
<article class="answer" ng-repeat="answer in answers">
<div class="row">
<div class="col-sm-2 col-lg-2">
<div class="row">
<div class="row" ng-controller="AnswerVoteCtrl">
<div class="rating col-sm-12 col-lg-12" ng-app="vote" ng-controller="AnswerVoteCtrl">
<div class="arrow-up" ng-class="user_question_vote_up" ng-click=voteUp(answer.pk)></div>
<div class="arrow-up" ng-class="user_answer_vote_up" ng-click=voteUp($index)></div>
<div class="votes">{{ answer.votes }}</div>
<!-- <div class="votes" ng-init="answer_votes{{ answer.pk }}={{ answer.count_votes }}">{{ answer_votes }}</div> -->
<div class="arrow-down" ng-class="user_question_vote_down" ng-click=voteDown(answer.pk)></div>
<div class="arrow-down" ng-class="user_answer_vote_down" ng-click=voteDown($index)></div>
</div><!-- .rating -->
</div>
<div class="author clearfix">
Expand Down
10 changes: 10 additions & 0 deletions forum/views.py
Expand Up @@ -103,7 +103,17 @@ def get_queryset(self):
class AnswerVoteViewSet(viewsets.ModelViewSet):
model = AnswerVote
serializer_class = AnswerVoteSerializer
# Get answer vote usign kwarg as questionId
lookup_field = "answer"

def pre_save(self, obj):
obj.user = self.request.user
# Get Question vote usign kwarg as questionId
if 'answer' in self.kwargs:
obj.answer = Answer.objects.get(pk=int(self.kwargs['answer']))
self.kwargs['answer'] = obj.answer
return super(AnswerVoteViewSet, self).pre_save(obj)

def get_queryset(self):
user = self.request.user
return AnswerVote.objects.filter(user=user)

0 comments on commit 29439dc

Please sign in to comment.