def resp_downvote(request): # Get the data being passed by get resp_id = None if request.method == "GET": resp_id = request.GET['resposta_id'] votes = 0 # If was passed any value on get if resp_id: # Get the question with the id received resp = Resposta.objects.get(id=int(resp_id)) votes = resp.votes # Get the user profile profile = request.user.userprofile # downvote the question if resp and resp not in profile.resp_downvotes.all(): votes = resp.votes - 1 resp.votes = votes resp.save() # first removes from upvotes if resp in profile.resp_upvotes.all(): profile.resp_upvotes.remove(resp) profile.save() else: # add question to downvotes profile.resp_downvotes.add(resp) profile.save() new_Vote(request.user, resp.autor, 0, None, resp) return HttpResponse(votes)
def downvote(request): # Get the data being passed by get perg_id = None if request.method == "GET": perg_id = request.GET['pergunta_id'] votes = 0 # If was passed any value on get if perg_id: # Get the question with the id received perg = Pergunta.objects.get(id=int(perg_id)) votes = perg.votes # Get the user profile profile = request.user.userprofile # downvote the question if perg and perg not in profile.perg_downvotes.all(): votes = perg.votes - 1 perg.votes = votes perg.save() # removes from upvotes if perg in profile.perg_upvotes.all(): profile.perg_upvotes.remove(perg) profile.save() else: # add question to downvotes profile.perg_downvotes.add(perg) profile.save() # Send a notification to the autor of the question new_Vote(request.user, perg.autor, 0, perg, None) return HttpResponse(votes)