def put(self, issue_id, username):
if not current_user.is_authenticated:
return abort(401, message='permission denied')
issue = issues_service.get(issue_id)
if issue is None:
return abort(400, message='issue not found')
user = users_service.get_by_username(username)
if user is None:
return abort(400, message='user not found')
args = self.parser.parse_args()
action = args.get('action')
action = 'upvote' if action is None else action
value = -1 if action == 'downvote' else 1
voter = voters_service.get(user, issue)
if voter is None:
if current_user.points - points.VOTE < 0:
return abort(403, message='points are not enough')
current_user.points -= points.VOTE
log = PointLog(action, points.VOTE, user, issue)
voter = Voter(user, issue)
db.session.add(voter)
db.session.add(log)
issue.points += value
elif voter.value != value:
issue.points += value
voter.value = value
else:
return voter
try:
db.session.commit()
except:
db.session.rollback()
return abort(500, message='operation failed')
return voter
评论列表
文章目录