def downvote_article(user, group_id, list_id, article_id):
try:
# Resources check
article = Article.objects.get(id=ObjectId(article_id))
group = Group.objects.get(id=ObjectId(group_id), lists=ObjectId(list_id), members=user)
list = List.objects.get(id=ObjectId(list_id), articles=article)
# Create new vote
vote = check_vote_exist(list, article)
if check_user_has_downvoted(user, vote):
raise UserHasVoted('User cannot vote twice.')
# User is just trying to take vote back
if check_user_has_upvoted(user, vote):
Vote.objects(id=vote.id).update_one(pull__upvoter_list=user, vote_count=vote.vote_count-1)
else:
# Downvote
Vote.objects(id=vote.id).update_one(push__downvoter_list=user, vote_count=vote.vote_count-1)
except Exception as e:
return type(e).__name__
vote.reload()
return vote
评论列表
文章目录