+ choices = query_db('select name, id from choices where id_vote=?', [idvote])
+ if request.method == 'POST':
+ if can_vote(idvote, session.get('user').get('id')):
+ for choice in choices:
+ if str(choice['id']) in request.form.keys():
+ g.db.execute('insert into user_choice (id_user, id_choice) values (?, ?)',
+ [session.get('user').get('id'), choice['id']])
+ g.db.commit()
+ if vote['is_multiplechoice'] == 0:
+ break
+ else:
+ abort(401)
+ tuples = query_db('select choiceid, choicename, users.id as userid, users.name as username from (select choices.id as choiceid, choices.name as choicename, id_user as userid from choices join user_choice on choices.id = user_choice.id_choice where id_vote = ?) join users on userid = users.id', [idvote])
+ users = dict()
+ for t in tuples:
+ if t['userid'] in users:
+ choice = dict()
+ choice['id'] = t['choiceid']
+ choice['name'] = t['choicename']
+ users[t['userid']]['choices'].append(choice)
+ else:
+ users[t['userid']] = dict()
+ users[t['userid']]['userid'] = t['userid']
+ users[t['userid']]['username'] = t['username']
+ choice = dict()
+ choice['id'] = t['choiceid']
+ choice['name'] = t['choicename']
+ users[t['userid']]['choices'] = [choice]