예제 #1
0
    def test_send(self, messenger, email):
        sent = user_last_to_vote_notification(self.user,
                                              self.submission_period)

        self.assertTrue(sent)
        email.assert_called_once_with(self.user, self.submission_period)
예제 #2
0
 def test_no_submission_period(self):
     sent = user_last_to_vote_notification(self.user, None)
     self.assertFalse(sent)
예제 #3
0
 def test_no_user(self):
     sent = user_last_to_vote_notification(None, self.submission_period)
     self.assertFalse(sent)
예제 #4
0
def vote(league_id, submission_period_id):
    try:
        league = select_league(league_id)
        submission_period = next((sp for sp in league.submission_periods
                                  if sp.id == submission_period_id), None)

        if not league or not submission_period:
            return "No submission period or league", httplib.INTERNAL_SERVER_ERROR

        if not league.has_user(g.user):
            return "Not a member of this league", httplib.UNAUTHORIZED

        # If this user didn't submit for this round, don't allow them to vote
        if not get_my_submission(g.user, submission_period):
            return redirect(url_for('view_league', league_id=league_id))

        # If this user already voted for this round, don't allow them to vote
        if get_my_vote(g.user, submission_period):
            return redirect(url_for('view_league', league_id=league_id))

        # If this round is no longer accepting votes, redirect
        if not submission_period.accepting_votes:
            return redirect(request.referrer)

        try:
            votes = json.loads(request.form.get('votes'))
            comments = json.loads(request.form.get('comments'))
        except Exception:
            app.logger.exception(
                "Failed to load JSON from form with votes: %s", request.form)
            return 'There was an error processing votes', 500

        # Remove all unnecessary zero-values
        votes = {k: v for k, v in votes.iteritems() if v}
        comments = {k: v for k, v in comments.iteritems() if v}

        # Process votes
        vote = create_or_update_vote(votes, comments, submission_period,
                                     league, g.user)

        # If someone besides owner is voting, notify the owner
        if not league.has_owner(g.user):
            owner_user_voted_notification(vote)

        remaining = submission_period.have_not_voted
        if not remaining:
            complete_submission_period.delay(submission_period.id)

        elif vote.count < 2 and len(remaining) == 1:
            last_user = remaining[0]
            user_last_to_vote_notification(last_user, submission_period)

        track_user_voted(g.user.id, submission_period)
        if comments:
            track_user_voted_with_comments(g.user.id, submission_period,
                                           len(comments))

        return redirect(
            url_for('view_submission_period',
                    league_id=league_id,
                    submission_period_id=submission_period_id))

    except Exception:
        app.logger.exception('Failed to process votes',
                             extra={
                                 'user': g.user.id,
                                 'league': league.id,
                                 'round': submission_period_id
                             })