Exemplo n.º 1
0
    def test_add_candidate(self):
        """ Test the add_candidate function. """
        create_elections(self.session)

        self.assertRaises(
            nuancierlib.NuancierException,
            nuancierlib.add_candidate,
            session=self.session,
            candidate_file='test.png',
            candidate_name='test image',
            candidate_author='pingou',
            candidate_license='CC-BY-SA',
            candidate_submitter='pingou',
            submitter_email='[email protected]',
            candidate_original_url=None,
            election_id=2,
        )

        nuancierlib.add_candidate(
            session=self.session,
            candidate_file='test.png',
            candidate_name='test image',
            candidate_author='pingou',
            candidate_license='CC-BY-SA',
            candidate_submitter='pingou',
            submitter_email='[email protected]',
            candidate_original_url=None,
            election_id=2,
            user='******',
        )
        self.session.commit()

        candidates = nuancierlib.get_candidates(self.session, 2, False)
        self.assertEqual(1, len(candidates))
        self.assertEqual('test image', candidates[0].candidate_name)
        self.assertEqual('test.png', candidates[0].candidate_file)

        candidates = nuancierlib.get_candidates(self.session, 2, True)
        self.assertEqual(0, len(candidates))

        self.assertRaises(
            nuancierlib.NuancierException,
            nuancierlib.add_candidate,
            session=self.session,
            candidate_file='test.png',
            candidate_name='test image',
            candidate_author='pingou',
            candidate_license='CC-BY-SA',
            candidate_submitter='pingou',
            submitter_email='[email protected]',
            candidate_original_url='http://example.org',
            election_id=2,
            user='******',
        )
Exemplo n.º 2
0
    def test_add_candidate(self):
        """ Test the add_candidate function. """
        create_elections(self.session)

        self.assertRaises(
            nuancierlib.NuancierException,
            nuancierlib.add_candidate,
            session=self.session,
            candidate_file='test.png',
            candidate_name='test image',
            candidate_author='pingou',
            candidate_license='CC-BY-SA',
            candidate_submitter='pingou',
            submitter_email='[email protected]',
            candidate_original_url=None,
            election_id=2,
        )

        nuancierlib.add_candidate(
            session=self.session,
            candidate_file='test.png',
            candidate_name='test image',
            candidate_author='pingou',
            candidate_license='CC-BY-SA',
            candidate_submitter='pingou',
            submitter_email='[email protected]',
            candidate_original_url=None,
            election_id=2,
            user='******',
        )
        self.session.commit()

        candidates = nuancierlib.get_candidates(self.session, 2, False)
        self.assertEqual(1, len(candidates))
        self.assertEqual('test image', candidates[0].candidate_name)
        self.assertEqual('test.png', candidates[0].candidate_file)

        candidates = nuancierlib.get_candidates(self.session, 2, True)
        self.assertEqual(0, len(candidates))

        self.assertRaises(
            nuancierlib.NuancierException,
            nuancierlib.add_candidate,
            session=self.session,
            candidate_file='test.png',
            candidate_name='test image',
            candidate_author='pingou',
            candidate_license='CC-BY-SA',
            candidate_submitter='pingou',
            submitter_email='[email protected]',
            candidate_original_url='http://example.org',
            election_id=2,
            user='******',
        )
Exemplo n.º 3
0
    def test_add_candidate(self):
        """ Test the add_candidate function. """
        create_elections(self.session)

        nuancierlib.add_candidate(
            session=self.session,
            candidate_file='test.png',
            candidate_name='test image',
            candidate_author='pingou',
            election_id=2,
        )
        self.session.commit()

        candidates = nuancierlib.get_candidates(self.session, 2)
        self.assertEqual(1, len(candidates))
        self.assertEqual('test image', candidates[0].candidate_name)
        self.assertEqual('test.png', candidates[0].candidate_file)
Exemplo n.º 4
0
def contribute(election_id):
    ''' Display the index page for interested contributor. '''
    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')
    elif not election.submission_open:
        flask.flash('This election is not open for submission', 'error')
        return flask.redirect(flask.url_for('elections_list'))

    candidates = nuancier.lib.model.Candidates.get_by_submitter(
        SESSION, flask.g.fas_user.username, election_id)
    if election.user_n_candidates and \
        len(candidates) >= election.user_n_candidates:
        flask.flash(
            'You have uploaded the maximum number of candidates (%s) you '
            'can upload for this election' % election.user_n_candidates,
            'error')
        return flask.redirect(flask.url_for('elections_list'))

    form = nuancier.forms.AddCandidateForm()
    if form.validate_on_submit():
        candidate_file = flask.request.files['candidate_file']

        try:
            validate_input_file(candidate_file)
        except nuancierlib.NuancierException as err:
            LOG.debug(
                'ERROR: Uploaded file is invalid - user: "******" '
                'election: "%s"', flask.g.fas_user.username, election_id)
            LOG.exception(err)
            flask.flash('%s' % err, 'error')
            return flask.render_template('contribute.html',
                                         election=election,
                                         form=form)

        filename = secure_filename(
            '%s-%s' % (flask.g.fas_user.username, candidate_file.filename))

        # Only save the file once everything has been safely saved in the DB
        upload_folder = os.path.join(APP.config['PICTURE_FOLDER'],
                                     election.election_folder)
        if not os.path.exists(upload_folder):  # pragma: no cover
            try:
                os.mkdir(upload_folder)
            except OSError as err:
                LOG.debug('ERROR: cannot add candidate file')
                LOG.exception(err)
                flask.flash(
                    'An error occured while writing the file, please '
                    'contact an administrator', 'error')
                return flask.render_template('contribute.html',
                                             election=election,
                                             form=form)

        # Save candidate to the database
        try:
            nuancierlib.add_candidate(
                SESSION,
                candidate_file=filename,
                candidate_name=form.candidate_name.data,
                candidate_author=form.candidate_author.data,
                candidate_original_url=form.candidate_original_url.data,
                candidate_license=form.candidate_license.data,
                candidate_submitter=flask.g.fas_user.username,
                submitter_email=flask.g.fas_user.email,
                election_id=election.id,
                user=flask.g.fas_user.username,
            )
        except nuancierlib.NuancierException as err:
            flask.flash('%s' % err, 'error')
            return flask.render_template('contribute.html',
                                         election=election,
                                         form=form)

        # The PIL module has already read the stream so we need to back up
        candidate_file.seek(0)

        candidate_file.save(os.path.join(upload_folder, filename))

        try:
            SESSION.commit()
        except SQLAlchemyError as err:  # pragma: no cover
            SESSION.rollback()
            # Remove file from the system if the db commit failed
            os.unlink(os.path.join(upload_folder, filename))
            LOG.debug(
                'ERROR: cannot add candidate - user: "******" '
                'election: "%s"', flask.g.fas_user.username, election_id)
            LOG.exception(err)
            flask.flash(
                'Someone has already upload a file with the same file name'
                ' for this election', 'error')
            return flask.render_template('contribute.html',
                                         election=election,
                                         form=form)

        flask.flash('Thanks for your submission')
        return flask.redirect(flask.url_for('index'))
    elif flask.request.method == 'GET':
        form.candidate_author.data = flask.g.fas_user.username

    return flask.render_template('contribute.html',
                                 election=election,
                                 form=form)
Exemplo n.º 5
0
def contribute(election_id):
    ''' Display the index page for interested contributor. '''
    election = nuancierlib.get_election(SESSION, election_id)
    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')
    elif not election.submission_open:
        flask.flash('This election is not open for submission', 'error')
        return flask.redirect(flask.url_for('elections_list'))

    candidates = nuancier.lib.model.Candidates.get_by_submitter(
        SESSION, flask.g.fas_user.username, election_id)
    if election.user_n_candidates and \
        len(candidates) >= election.user_n_candidates:
        flask.flash(
            'You have uploaded the maximum number of candidates (%s) you '
            'can upload for this election' % election.user_n_candidates,
            'error')
        return flask.redirect(flask.url_for('elections_list'))

    form = nuancier.forms.AddCandidateForm()
    if form.validate_on_submit():
        candidate_file = flask.request.files['candidate_file']

        try:
            validate_input_file(candidate_file)
        except nuancierlib.NuancierException as err:
            LOG.debug('ERROR: Uploaded file is invalid - user: "******" '
                      'election: "%s"', flask.g.fas_user.username,
                      election_id)
            LOG.exception(err)
            flask.flash(err.message, 'error')
            return flask.render_template(
                'contribute.html',
                election=election,
                form=form)

        filename = secure_filename('%s-%s' % (flask.g.fas_user.username,
                                   candidate_file.filename))

        # Only save the file once everything has been safely saved in the DB
        upload_folder = os.path.join(
            APP.config['PICTURE_FOLDER'], election.election_folder)
        if not os.path.exists(upload_folder):  # pragma: no cover
            try:
                os.mkdir(upload_folder)
            except OSError, err:
                LOG.debug('ERROR: cannot add candidate file')
                LOG.exception(err)
                flask.flash(
                    'An error occured while writing the file, please '
                    'contact an administrator', 'error')
                return flask.render_template(
                    'contribute.html',
                    election=election,
                    form=form)

        # Save candidate to the database
        try:
            nuancierlib.add_candidate(
                SESSION,
                candidate_file=filename,
                candidate_name=form.candidate_name.data,
                candidate_author=form.candidate_author.data,
                candidate_original_url=form.candidate_original_url.data,
                candidate_license=form.candidate_license.data,
                candidate_submitter=flask.g.fas_user.username,
                submitter_email=flask.g.fas_user.email,
                election_id=election.id,
                user=flask.g.fas_user.username,
            )
        except nuancierlib.NuancierException as err:
            flask.flash(err.message, 'error')
            return flask.render_template(
                'contribute.html',
                election=election,
                form=form)

        # The PIL module has already read the stream so we need to back up
        candidate_file.seek(0)

        candidate_file.save(
            os.path.join(upload_folder, filename))

        try:
            SESSION.commit()
        except SQLAlchemyError as err:  # pragma: no cover
            SESSION.rollback()
            # Remove file from the system if the db commit failed
            os.unlink(os.path.join(upload_folder, filename))
            LOG.debug('ERROR: cannot add candidate - user: "******" '
                      'election: "%s"', flask.g.fas_user.username,
                      election_id)
            LOG.exception(err)
            flask.flash(
                'Someone has already upload a file with the same file name'
                ' for this election', 'error')
            return flask.render_template(
                'contribute.html',
                election=election,
                form=form)

        flask.flash('Thanks for your submission')
        return flask.redirect(flask.url_for('index'))