diff --git a/forum/serializers.py b/forum/serializers.py index 2e8907291..0c84c5db0 100644 --- a/forum/serializers.py +++ b/forum/serializers.py @@ -11,6 +11,22 @@ class Meta: class AnswerSerializer(serializers.ModelSerializer): + votes = serializers.SerializerMethodField('count_votes') + username = serializers.SerializerMethodField('get_username') + timestamp = serializers.DateTimeField(read_only=True) + class Meta: model = Answer - fields = ('question', 'text',) + fields = ('question', 'text', 'votes', 'timestamp', 'username') + + def count_votes(self, obj): + if obj: + return obj.votes.count() + else: + return 0 + + def get_username(self, obj): + if obj: + return obj.user.username + else: + return u'' diff --git a/forum/static/js/question.js b/forum/static/js/question.js new file mode 100644 index 000000000..0b44954c6 --- /dev/null +++ b/forum/static/js/question.js @@ -0,0 +1,52 @@ +(function (angular) { + "use strict"; + + var app = angular.module('answer', ['ngRoute', 'ngResource', "ngCookies"]); + + // Uses the csrftoken from the cookie + app.run(function ($http, $cookies) { + $http.defaults.headers.common['X-CSRFToken'] = $cookies['csrftoken']; + }); + // Avoid jumping to top after click in anchor '#' + app.value('$anchorScroll', angular.noop); + + app.config(['$routeProvider', function ($routeProvider) { + console.log('ngRoute!!!'); + $routeProvider + .when('/forum/question/answer/create', { + templateUrl: 'answer_create.html', + controller: 'AnswerCtrl'}); + }]); + + app.controller('AnswerCtrl', ['$scope', 'Answer', + function ($scope, Answer) { + var questionId = 1; + var answer_text = 'Angular Angular Angular AngularAngular AngularAngularAngularAngularAngularAngular Angular Angular Angular Angular Angular'; + $scope.new_answer = Answer.create({question: questionId, text: answer_text}); + }]); + + app.factory('Answer', function($resource){ + return $resource('/api/answer/', {}, { + create: {method: 'POST'} + }); + }); + +})(angular); + +$(document).ready(function() { + $('.comment-form').hide(); + $('.add-comment-link').click(function(e) { + e.preventDefault(); + $(this).parent().parent().height(170); + $(this).parent().parent().find('form').slideDown(); + $(this).hide(); + }); + + $('.cancel-comment').click(function(e) { + e.preventDefault(); + $(this).parent().parent().slideUp(); + $(this).parent().parent().parent().find('.add-comment-link').slideDown().show(); + $(this).parent().parent().parent().height(30); + }); + $('nav.course-sections > a > span').tooltip(); +}); \ No newline at end of file diff --git a/forum/templates/question.html b/forum/templates/question.html index 19ec7d163..fba78fc52 100644 --- a/forum/templates/question.html +++ b/forum/templates/question.html @@ -2,6 +2,10 @@ {% load i18n %} {% load staticfiles %} {% block content %} + + +
@@ -33,28 +37,13 @@

{{ question.course.name
-
+
@@ -148,7 +137,7 @@

{{ question.answers.count }} Respostas:

-

{{ answer.text }} +

{{ answer.text }}

@@ -166,14 +155,50 @@

{{ question.answers.count }} Respostas:

{% endfor %} + + {% verbatim angularjs %} + + {% endverbatim angularjs %} +
+

Sua Resposta

-
@@ -187,9 +212,8 @@

Sua Resposta

- + Enviar
-

diff --git a/forum/views.py b/forum/views.py index e296ea983..57b2fd756 100644 --- a/forum/views.py +++ b/forum/views.py @@ -64,7 +64,15 @@ class QuestionViewSet(viewsets.ModelViewSet): model = Question serializer_class = QuestionSerializer + def pre_save(self, obj): + obj.user = self.request.user + return super(QuestionViewSet, self).pre_save(obj) + class AnswerViewSet(viewsets.ModelViewSet): model = Answer serializer_class = AnswerSerializer + + def pre_save(self, obj): + obj.user = self.request.user + return super(AnswerViewSet, self).pre_save(obj)