def test_get_votes_user(self):
        """ Test the get_votes_user function. """
        create_elections(self.session)
        create_candidates(self.session)
        create_votes(self.session)

        votes = nuancierlib.get_votes_user(self.session, 1, 'pingou')
        self.assertEqual(2, len(votes))
        self.assertEqual(1, votes[0].candidate_id)
        self.assertEqual(2, votes[1].candidate_id)
Beispiel #2
0
    def test_get_votes_user(self):
        """ Test the get_votes_user function. """
        create_elections(self.session)
        create_candidates(self.session)
        create_votes(self.session)

        votes = nuancierlib.get_votes_user(self.session, 1, 'pingou')
        self.assertEqual(2, len(votes))
        self.assertEqual(1, votes[0].candidate_id)
        self.assertEqual(2, votes[1].candidate_id)
Beispiel #3
0
    def test_votes_repr(self):
        """ Test the __repr__ function of Votes. """
        create_elections(self.session)
        create_candidates(self.session)
        create_votes(self.session)

        votes = nuancierlib.get_votes_user(self.session, 1, 'pingou')
        self.assertTrue(
            votes[0].__repr__().startswith(
                "Votes(name:u'pingou', candidate_id:1, created:"
            )
        )
Beispiel #4
0
    def test_votes_repr(self):
        """ Test the __repr__ function of Votes. """
        create_elections(self.session)
        create_candidates(self.session)
        create_votes(self.session)

        votes = nuancierlib.get_votes_user(self.session, 1, 'pingou')
        if six.PY2:
            self.assertTrue(votes[0].__repr__().startswith(
                "Votes(name:u'pingou', candidate_id:1, created:"))
        else:
            self.assertTrue(votes[0].__repr__().startswith(
                "Votes(name:'pingou', candidate_id:1, created:"))
Beispiel #5
0
    def test_add_vote(self):
        """ Test the add_vote function. """
        create_elections(self.session)
        create_candidates(self.session)

        nuancierlib.add_vote(session=self.session,
                             candidate_id=2,
                             username='******')

        self.session.commit()

        votes = nuancierlib.get_votes_user(self.session, 1, 'pingou')
        self.assertEqual(1, len(votes))
        self.assertEqual(2, votes[0].candidate_id)
Beispiel #6
0
def vote(election_id):
    ''' Give the possibility to the user to vote for an election. '''
    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')
    candidates = nuancierlib.get_candidates(SESSION,
                                            election_id,
                                            approved=True)

    if not election.election_open:
        flask.flash('This election is not open', 'error')
        return flask.redirect(flask.url_for('index'))

    if flask.g.fas_user:
        username = flask.g.fas_user.username
        if not isinstance(username, six.binary_type):
            username = username.encode('utf-8')
        random.seed(int(hashlib.sha1(username).hexdigest(), 16) % 100000)
    random.shuffle(candidates)

    # How many votes the user made:
    votes = nuancierlib.get_votes_user(SESSION, election_id,
                                       flask.g.fas_user.username)

    if len(votes) >= election.election_n_choice:
        flask.flash(
            'You have cast the maximal number of votes '
            'allowed for this election.', 'error')
        return flask.redirect(
            flask.url_for('election', election_id=election_id))

    if len(votes) > 0:
        candidate_done = [cdt.candidate_id for cdt in votes]
        candidates = [
            candidate for candidate in candidates
            if candidate.id not in candidate_done
        ]

    return flask.render_template(
        'vote.html',
        election=election,
        form=nuancier.forms.ConfirmationForm(),
        candidates=candidates,
        n_votes_done=len(votes),
        picture_folder=os.path.join(APP.config['PICTURE_FOLDER'],
                                    election.election_folder),
        cache_folder=os.path.join(APP.config['CACHE_FOLDER'],
                                  election.election_folder))
    def test_add_vote(self):
        """ Test the add_vote function. """
        create_elections(self.session)
        create_candidates(self.session)

        nuancierlib.add_vote(
            session=self.session,
            candidate_id=2,
            username='******'
        )

        self.session.commit()

        votes = nuancierlib.get_votes_user(self.session, 1, 'pingou')
        self.assertEqual(1, len(votes))
        self.assertEqual(2, votes[0].candidate_id)
Beispiel #8
0
def vote(election_id):
    ''' Give the possibility to the user to vote for an election. '''
    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')
    candidates = nuancierlib.get_candidates(
        SESSION, election_id, approved=True)

    if not election.election_open:
        flask.flash('This election is not open', 'error')
        return flask.redirect(flask.url_for('index'))

    if flask.g.fas_user:
        random.seed(
            int(
                hashlib.sha1(flask.g.fas_user.username).hexdigest(), 16
            ) % 100000)
    random.shuffle(candidates)

    # How many votes the user made:
    votes = nuancierlib.get_votes_user(SESSION, election_id,
                                       flask.g.fas_user.username)

    if len(votes) >= election.election_n_choice:
        flask.flash('You have cast the maximal number of votes '
                    'allowed for this election.', 'error')
        return flask.redirect(
            flask.url_for('election', election_id=election_id))

    if len(votes) > 0:
        candidate_done = [cdt.candidate_id for cdt in votes]
        candidates = [candidate
                      for candidate in candidates
                      if candidate.id not in candidate_done]

    return flask.render_template(
        'vote.html',
        election=election,
        form=nuancier.forms.ConfirmationForm(),
        candidates=candidates,
        n_votes_done=len(votes),
        picture_folder=os.path.join(
            APP.config['PICTURE_FOLDER'], election.election_folder),
        cache_folder=os.path.join(
            APP.config['CACHE_FOLDER'], election.election_folder)
    )
Beispiel #9
0
def election(election_id):
    ''' Display the index page of the election will all the candidates
    submitted. '''
    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    # How many votes the user made:
    votes = []
    can_vote = True
    if hasattr(flask.g, 'fas_user') and flask.g.fas_user:
        votes = nuancierlib.get_votes_user(SESSION, election_id,
                                           flask.g.fas_user.username)

    if election.election_open and len(votes) < election.election_n_choice:
        if len(votes) > 0:
            flask.flash('You have already voted, but you can still vote '
                        'on more candidates.')
        return flask.redirect(flask.url_for('vote', election_id=election_id))
    elif election.election_open and len(votes) >= election.election_n_choice:
        can_vote = False
    elif not election.election_public:
        flask.flash('This election is not open', 'error')
        return flask.redirect(flask.url_for('elections_list'))

    candidates = nuancierlib.get_candidates(SESSION,
                                            election_id,
                                            approved=True)

    if hasattr(flask.g, 'fas_user') and flask.g.fas_user:
        username = flask.g.fas_user.username
        if not isinstance(username, six.binary_type):
            username = username.encode('utf-8')
        random.seed(int(hashlib.sha1(username).hexdigest(), 16) % 100000)
    random.shuffle(candidates)

    return flask.render_template(
        'election.html',
        candidates=candidates,
        election=election,
        can_vote=can_vote,
        picture_folder=os.path.join(APP.config['PICTURE_FOLDER'],
                                    election.election_folder),
        cache_folder=os.path.join(APP.config['CACHE_FOLDER'],
                                  election.election_folder))
Beispiel #10
0
def election(election_id):
    ''' Display the index page of the election will all the candidates
    submitted. '''
    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    # How many votes the user made:
    votes = []
    can_vote = True
    if hasattr(flask.g, 'fas_user') and flask.g.fas_user:
        votes = nuancierlib.get_votes_user(SESSION, election_id,
                                           flask.g.fas_user.username)

    if election.election_open and len(votes) < election.election_n_choice:
        if len(votes) > 0:
            flask.flash('You have already voted, but you can still vote '
                        'on more candidates.')
        return flask.redirect(flask.url_for('vote', election_id=election_id))
    elif election.election_open and len(votes) >= election.election_n_choice:
        can_vote = False
    elif not election.election_public:
        flask.flash('This election is not open', 'error')
        return flask.redirect(flask.url_for('elections_list'))

    candidates = nuancierlib.get_candidates(
        SESSION, election_id, approved=True)

    if hasattr(flask.g, 'fas_user') and flask.g.fas_user:
        random.seed(
            int(
                hashlib.sha1(flask.g.fas_user.username).hexdigest(), 16
            ) % 100000)
    random.shuffle(candidates)

    return flask.render_template(
        'election.html',
        candidates=candidates,
        election=election,
        can_vote=can_vote,
        picture_folder=os.path.join(
            APP.config['PICTURE_FOLDER'], election.election_folder),
        cache_folder=os.path.join(
            APP.config['CACHE_FOLDER'], election.election_folder)
    )
Beispiel #11
0
def process_vote(election_id):
    ''' Actually register the vote, after checking if the user is actually
    allowed to vote.
    '''

    form = nuancier.forms.ConfirmationForm()
    if not form.validate_on_submit():
        flask.flash('Wrong input submitted', 'error')
        return flask.render_template('msg.html')

    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    if not election.election_open:
        flask.flash('This election is not open', 'error')
        return flask.render_template('msg.html')

    candidates = nuancierlib.get_candidates(SESSION,
                                            election_id,
                                            approved=True)
    candidate_ids = set([candidate.id for candidate in candidates])

    entries = set(
        [int(entry) for entry in flask.request.form.getlist('selection')])

    # If not enough candidates selected
    if not entries:
        flask.flash('You did not select any candidate to vote for.', 'error')
        return flask.redirect(flask.url_for('vote', election_id=election_id))

    # If vote on candidates from other elections
    if not set(entries).issubset(candidate_ids):
        flask.flash(
            'The selection you have made contains element which are '
            'not part of this election, please be careful.', 'error')
        return flask.redirect(flask.url_for('vote', election_id=election_id))

    # How many votes the user made:
    votes = nuancierlib.get_votes_user(SESSION, election_id,
                                       flask.g.fas_user.username)

    # Too many votes -> redirect
    if len(votes) >= election.election_n_choice:
        flask.flash(
            'You have cast the maximal number of votes '
            'allowed for this election.', 'error')
        return flask.redirect(
            flask.url_for('election', election_id=election_id))

    # Selected more candidates than allowed -> redirect
    if len(votes) + len(entries) > election.election_n_choice:
        flask.flash(
            'You selected %s wallpapers while you are only allowed '
            'to select %s' % (len(entries),
                              (election.election_n_choice - len(votes))),
            'error')
        return flask.render_template(
            'vote.html',
            form=nuancier.forms.ConfirmationForm(),
            election=election,
            candidates=[
                nuancierlib.get_candidate(SESSION, candidate_id)
                for candidate_id in entries
            ],
            n_votes_done=len(votes),
            picture_folder=os.path.join(APP.config['PICTURE_FOLDER'],
                                        election.election_folder),
            cache_folder=os.path.join(APP.config['CACHE_FOLDER'],
                                      election.election_folder))

    # Allowed to vote, selection sufficient, choice confirmed: process
    for selection in entries:
        value = 1
        if nuancier.has_weigthed_vote(flask.g.fas_user):
            value = 2
        nuancierlib.add_vote(SESSION,
                             selection,
                             flask.g.fas_user.username,
                             value=value)

    try:
        SESSION.commit()
    except SQLAlchemyError as err:  # pragma: no cover
        SESSION.rollback()
        LOG.debug(
            'ERROR: could not process the vote - user: "******" '
            'election: "%s"', flask.g.fas_user.username, election_id)
        LOG.exception(err)
        flask.flash(
            'An error occured while processing your votes, please '
            'report this to your lovely admin or see logs for '
            'more details', 'error')

    flask.flash('Your vote has been recorded, thank you for voting on '
                '%s %s' % (election.election_name, election.election_year))

    if election.election_badge_link:
        flask.flash('Do not forget to <a href="%s" target="_blank">claim your '
                    'badge!</a>' % election.election_badge_link)
    return flask.redirect(flask.url_for('elections_list'))
Beispiel #12
0
def process_vote(election_id):
    ''' Actually register the vote, after checking if the user is actually
    allowed to vote.
    '''

    form = nuancier.forms.ConfirmationForm()
    if not form.validate_on_submit():
        flask.flash('Wrong input submitted', 'error')
        return flask.render_template('msg.html')

    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    if not election.election_open:
        flask.flash('This election is not open', 'error')
        return flask.render_template('msg.html')

    candidates = nuancierlib.get_candidates(
        SESSION, election_id, approved=True)
    candidate_ids = set([candidate.id for candidate in candidates])

    entries = set([int(entry)
                   for entry in flask.request.form.getlist('selection')])

    # If not enough candidates selected
    if not entries:
        flask.flash('You did not select any candidate to vote for.', 'error')
        return flask.redirect(flask.url_for('vote', election_id=election_id))

    # If vote on candidates from other elections
    if not set(entries).issubset(candidate_ids):
        flask.flash('The selection you have made contains element which are '
                    'not part of this election, please be careful.', 'error')
        return flask.redirect(flask.url_for('vote', election_id=election_id))

    # How many votes the user made:
    votes = nuancierlib.get_votes_user(SESSION, election_id,
                                       flask.g.fas_user.username)

    # Too many votes -> redirect
    if len(votes) >= election.election_n_choice:
        flask.flash('You have cast the maximal number of votes '
                    'allowed for this election.', 'error')
        return flask.redirect(
            flask.url_for('election', election_id=election_id))

    # Selected more candidates than allowed -> redirect
    if len(votes) + len(entries) > election.election_n_choice:
        flask.flash('You selected %s wallpapers while you are only allowed '
                    'to select %s' % (
                        len(entries),
                        (election.election_n_choice - len(votes))),
                    'error')
        return flask.render_template(
            'vote.html',
            form=nuancier.forms.ConfirmationForm(),
            election=election,
            candidates=[nuancierlib.get_candidate(SESSION, candidate_id)
                        for candidate_id in entries],
            n_votes_done=len(votes),
            picture_folder=os.path.join(
                APP.config['PICTURE_FOLDER'], election.election_folder),
            cache_folder=os.path.join(
                APP.config['CACHE_FOLDER'], election.election_folder)
        )

    # Allowed to vote, selection sufficient, choice confirmed: process
    for selection in entries:
        value = 1
        if nuancier.has_weigthed_vote(flask.g.fas_user):
            value = 2
        nuancierlib.add_vote(
            SESSION, selection, flask.g.fas_user.username, value=value)

    try:
        SESSION.commit()
    except SQLAlchemyError as err:  # pragma: no cover
        SESSION.rollback()
        LOG.debug('ERROR: could not process the vote - user: "******" '
                  'election: "%s"', flask.g.fas_user.username,
                  election_id)
        LOG.exception(err)
        flask.flash('An error occured while processing your votes, please '
                    'report this to your lovely admin or see logs for '
                    'more details', 'error')

    flask.flash('Your vote has been recorded, thank you for voting on '
                '%s %s' % (election.election_name, election.election_year))

    if election.election_badge_link:
        flask.flash('Do not forget to <a href="%s" target="_blank">claim your '
                    'badge!</a>' % election.election_badge_link)
    return flask.redirect(flask.url_for('elections_list'))