Hey guys my code is a bit jumbled but maybe you can help me sort out why it's not doing what I want it to do.
There's something wrong with my logic since I have tried the update statements on their own and they do work.
I can't figure out why it won't go to the elif statement.
The Vote table is just postID, username, upvote and downvote. I want it to work if a user has a record in the table for that postID and they have upvote marked as 1, then you should be able to only downvote.
Currently, it doesn't let you vote twice, but it won't let you vote at all if you've already cast a vote.
Here is my relevant code.
Thanks!
def upvote(request):
postID = request.POST.get("postID")
currentUser = request.POST.get("currentUser")
username = request.POST.get("username")
query = NewPost.objects.get(id = postID)
check = Vote.objects.filter(postID = postID) & Vote.objects.filter(username = currentUser)
if(len(check) < 1):
query.rating = query.rating +1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking +1
query2.save()
new = Vote.objects.create(postID = postID, username = currentUser, downvote = 0, upvote = 1)
new.save()
pyautogui.hotkey('f5')
return render(request)
elif(len(check) > 1):
if(check[0].upvote == 0):
check.update(downvote = 0)
check.update(upvote = 1)
check[0].save()
check.save()
query.rating = query.rating +1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking +1
query2.save()
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)
def downvote(request):
postID = request.POST.get("postID")
currentUser = request.POST.get("currentUser")
username = request.POST.get("username")
query = NewPost.objects.get(id = postID)
check = Vote.objects.filter(postID = postID) & Vote.objects.filter(username = currentUser)
if(len(check) < 1):
query.rating = query.rating -1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking -1
query2.save()
new = Vote.objects.create(postID = postID, username = currentUser, downvote = 1, upvote = 0)
new.save()
pyautogui.hotkey('f5')
return render(request)
elif(len(check) > 1):
if(check[0].downvote == 0):
check.update(downvote = 1)
check.update(upvote = 0)
check[0].save()
query.rating = query.rating -1
query.save()
query2 = User.objects.get(username = username)
query2.userRanking = query2.userRanking -1
query2.save()
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)
else:
pyautogui.hotkey('f5')
return render(request)
I changed the elif statement to :
elif(len(check) > 0):