def test_vote_to_string_single(): poll = Poll.create(creator_id='user0', message='Message', vote_options=['Sure', 'Maybe', 'No']) poll.vote('user0', 0) poll.vote('user1', 2) assert frmts.format_user_vote(poll, 'user0') == ('Sure ✓, Maybe ✗, No ✗') assert frmts.format_user_vote(poll, 'user1') == ('Sure ✗, Maybe ✗, No ✓') assert frmts.format_user_vote(poll, 'user2') == ('Sure ✗, Maybe ✗, No ✗')
def vote(): """Places a vote for a user. Called through the URL in the corresponding action (see formatters.format_actions). The JSON `context` is expected to contain a valid poll_id and the vote_id to vote for. """ json = request.get_json() user_id = json['user_id'] poll_id = json['context']['poll_id'] vote_id = json['context']['vote'] request.user_id = user_id try: poll = Poll.load(poll_id) except InvalidPollError: return jsonify({ 'ephemeral_text': tr("This poll is not valid anymore.\n" "Sorry for the inconvenience.") }) app.logger.info('Voting in poll "%s" for user "%s": %i', poll_id, user_id, vote_id) try: poll.vote(user_id, vote_id) except NoMoreVotesError: return jsonify({ 'ephemeral_text': tr("You already used all your votes.\n" "Click on a vote to unselect it again.") }) return jsonify({ 'update': { 'props': format_poll(poll) }, 'ephemeral_text': tr("Your vote has been updated:\n{}").format( format_user_vote(poll, user_id)) })