From 4c5a279249a44bc921cfab5535ab69abe0311030 Mon Sep 17 00:00:00 2001 From: ashwinishinde Date: Mon, 6 Jul 2015 15:12:23 +0530 Subject: Subject: Resolve comment issue occured by Vote functionality Description: --- forums/forms.py | 24 ++++++------- forums/views.py | 4 +-- static/website/css/main.css | 4 +++ static/website/js/thread-user.js | 1 - static/website/templates/get-question.html | 19 +++++++---- static/website/templates/questions.html | 17 ++++----- website/models.py | 4 +-- website/urls.py | 2 ++ website/views.py | 55 +++++++++++++++++++++++++++--- 9 files changed, 90 insertions(+), 40 deletions(-) diff --git a/forums/forms.py b/forums/forms.py index 90a147d..05f0d56 100644 --- a/forums/forms.py +++ b/forums/forms.py @@ -15,33 +15,24 @@ from website.models import Profile class UserLoginForm(forms.Form): - username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput()) - db = forms.ChoiceField(choices=[('forum', 'forum'), ('scilab', 'scilab'), ('client2', 'client2')]) def clean(self): cleaned_data = self.cleaned_data username = cleaned_data.get('username') + print username password = cleaned_data.get('password') - db = cleaned_data.get('db') - + print password if username is None or password is None: raise forms.ValidationError("Invalid username or password") - - - if db == 'forum': - user = authenticate(username=username, password=password) - print "default" , "*******************" - else: - user = authenticate(username=username, password=password, db=db) - print db , "*******************" + user = authenticate(username=username, password=password) + if not user: raise forms.ValidationError("Invalid username or password") if not user.is_active: raise forms.ValidationError("User is blocked") cleaned_data['user'] = user - return cleaned_data class ProfileForm(forms.ModelForm): @@ -112,4 +103,9 @@ class RegisterForm(forms.Form): raise forms.ValidationError(_("This username has already existed.")) except User.DoesNotExist: pass - return username + + + + + + diff --git a/forums/views.py b/forums/views.py index 80410b4..185cc84 100644 --- a/forums/views.py +++ b/forums/views.py @@ -19,8 +19,7 @@ def account_register(request): print request.method if request.method == 'POST': form = RegisterForm(request.POST) - print form - print form.is_valid + if form.is_valid(): username = request.POST['username'] @@ -49,6 +48,7 @@ def account_register(request): } context.update(csrf(request)) return render_to_response('forums/templates/user-register.html', context) + def confirm(request, confirmation_code, username): try: user = User.objects.get(username=username) diff --git a/static/website/css/main.css b/static/website/css/main.css index 774871e..8d011fb 100644 --- a/static/website/css/main.css +++ b/static/website/css/main.css @@ -456,6 +456,10 @@ table .question a{ text-align: center; } +.ans-vote { + text-align: center; +} + .vote-up-off, .vote-up-on, .vote-down-off, .vote-down-on, .star-on, .star-off, .comment-up-off, .comment-up-on, .comment-flag, .flag-off, .vote-accepted-off, .vote-accepted-on { text-indent: -9999em; diff --git a/static/website/js/thread-user.js b/static/website/js/thread-user.js index 0dd0e89..1a089d2 100644 --- a/static/website/js/thread-user.js +++ b/static/website/js/thread-user.js @@ -270,7 +270,6 @@ $(document).ready(function() { $add_comment = $(".add-comment"); $cancel_commment = $(".cancel-comment"); $post_comment = $(".post-comment"); - $add_comment.click(function(e) { $(this).hide(); $(this).siblings(".cancel-comment").show(); diff --git a/static/website/templates/get-question.html b/static/website/templates/get-question.html index 67ac33a..ea6bfa3 100644 --- a/static/website/templates/get-question.html +++ b/static/website/templates/get-question.html @@ -109,11 +109,13 @@ + {% for answer in answers %} +
- +
{{ answer.body|safe }}
@@ -268,15 +270,16 @@ }).panelInstance('id_body'); }); - + + + + }); + + + + {% endblock %} diff --git a/static/website/templates/questions.html b/static/website/templates/questions.html index 81c0d56..292437f 100644 --- a/static/website/templates/questions.html +++ b/static/website/templates/questions.html @@ -13,17 +13,7 @@ Question Date Views -
- {% if num_votes == 0 %} - vote - {% elif num_votes == 1 %} - vote - {% elif num_votes == -1 %} - vote - {% else %} - votes - {% endif %} -
+ Votes Answers User {% for question in questions %} @@ -56,6 +46,11 @@ {{ question.views}} + + + {{ question.num_votes}} + + {{ question.answer_set.count }} diff --git a/website/models.py b/website/models.py index b0a9fc3..b15f1cd 100644 --- a/website/models.py +++ b/website/models.py @@ -54,8 +54,8 @@ class Answer(models.Model): body = models.TextField() date_created = models.DateTimeField(auto_now_add=True) date_modified = models.DateTimeField(auto_now=True) - #userUpVotes = models.ManyToManyField(User, blank=True, related_name='postUpVotes') - #userDownVotes = models.ManyToManyField(User, blank=True, related_name='postDownVotes') + userUpVotes = models.ManyToManyField(User, blank=True, related_name='postAnswerUpVotes') + userDownVotes = models.ManyToManyField(User, blank=True, related_name='postAnswerDownVotes') upvotes = models.IntegerField(default=0) num_votes = models.IntegerField(default=0) diff --git a/website/urls.py b/website/urls.py index 71588af..72b4751 100644 --- a/website/urls.py +++ b/website/urls.py @@ -21,6 +21,8 @@ urlpatterns = patterns('', url(r'^search/$', 'website.views.search', name='search'), url(r'^unanswered-notification/$', 'website.views.unanswered_notification', name='unanswered_notification'), url(r'^vote_post/$', 'website.views.vote_post', name='vote_post'), + url(r'^ans_vote_post/$', 'website.views.ans_vote_post', name='ans_vote_post'), + # Ajax helpers url(r'^ajax-tutorials/$', 'website.views.ajax_tutorials', name='ajax_tutorials'), url(r'^ajax-duration/$', 'website.views.ajax_duration', name='ajax_duration'), diff --git a/website/views.py b/website/views.py index 5974433..6e84cfa 100644 --- a/website/views.py +++ b/website/views.py @@ -60,9 +60,8 @@ def get_question(request, question_id=None, pretty_url=None): form = AnswerQuestionForm() thisuserupvote = question.userUpVotes.filter(id=request.user.id).count() thisuserdownvote = question.userDownVotes.filter(id=request.user.id).count() - net_count = question.userUpVotes.count() - question.userDownVotes.count() - + context = { 'question': question, 'answers': answers, @@ -307,12 +306,12 @@ def new_question(request): def vote_post(request): - print "post" + post_id = int(request.POST.get('id')) vote_type = request.POST.get('type') vote_action = request.POST.get('action') - print post_id + cur_post = get_object_or_404(Question, id=post_id) thisuserupvote = cur_post.userUpVotes.filter(id=request.user.id).count() @@ -353,6 +352,54 @@ def vote_post(request): return HttpResponse(num_votes) +def ans_vote_post(request): + + + post_id = int(request.POST.get('id')) + + vote_type = request.POST.get('type') + vote_action = request.POST.get('action') + + cur_post = get_object_or_404(Answer, id=post_id) + + userupvote = cur_post.userUpVotes.filter(id=request.user.id).count() + userdownvote = cur_post.userDownVotes.filter(id=request.user.id).count() + + initial_votes = cur_post.userUpVotes.count() - cur_post.userDownVotes.count() + + # print "User Initial Upvote and Downvote: %d %d %s " % (thisuserupvote, thisuserdownvote, vote_action) + + #This loop is for voting + if vote_action == 'vote': + if (userupvote == 0) and (userdownvote == 0): + if vote_type == 'up': + cur_post.userUpVotes.add(request.user) + elif vote_type == 'down': + cur_post.userDownVotes.add(request.user) + else: + return HttpResponse("Error: Unknown vote-type passed.") + else: + return HttpResponse(initial_votes) + #This loop is for canceling vote + elif vote_action == 'recall-vote': + if (vote_type == 'up') and (userupvote == 1): + cur_post.userUpVotes.remove(request.user) + elif (vote_type == 'down') and (userdownvote == 1): + cur_post.userDownVotes.remove(request.user) + else: + # "Error - Unknown vote type or no vote to recall" + return HttpResponse(initial_votes) + else: + return HttpResponse("Error: Bad Action.") + + num_votes = cur_post.userUpVotes.count() - cur_post.userDownVotes.count() + cur_post.num_votes = num_votes + cur_post.save() + + print "Num Votes: %s" % num_votes + + return HttpResponse(num_votes) + # Notification Section @login_required def user_questions(request, user_id): -- cgit