Example #1
0
def admin_new():
    ''' Create a new election. '''
    form = nuancier.forms.AddElectionForm()
    if form.validate_on_submit():

        try:
            election = nuancierlib.add_election(
                SESSION,
                election_name=form.election_name.data,
                election_folder=form.election_folder.data,
                election_year=form.election_year.data,
                election_date_start=form.election_date_start.data,
                election_date_end=form.election_date_end.data,
                submission_date_start=form.submission_date_start.data,
                election_n_choice=form.election_n_choice.data,
                election_badge_link=form.election_badge_link.data,
                user=flask.g.fas_user.username,
            )

            SESSION.commit()
        except SQLAlchemyError as err:
            SESSION.rollback()
            LOG.debug("User: %s could not add an election",
                      flask.g.fas_user.username)
            LOG.exception(err)
            flask.flash('Could not add this election, is this name or '
                        'folder already used?', 'error')
            return flask.render_template('admin_new.html', form=form)

        flask.flash('Election created')
        if form.generate_cache.data:
            return admin_cache(election.id)

        return flask.redirect(flask.url_for('admin_index'))
    return flask.render_template('admin_new.html', form=form)
Example #2
0
def admin_cache(election_id):
    ''' Regenerate the cache for this election. '''
    election = nuancierlib.get_election(SESSION, election_id)

    next_url = None
    if 'next' in flask.request.args:
        next_url = flask.request.args['next']

    if not next_url or next_url == flask.url_for(
            '.admin_cache', election_id=election_id):
        next_url = flask.url_for('.admin_index')

    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    try:
        nuancierlib.generate_cache(
            session=SESSION,
            election=election,
            picture_folder=APP.config['PICTURE_FOLDER'],
            cache_folder=APP.config['CACHE_FOLDER'],
            size=APP.config['THUMB_SIZE'])
        flask.flash('Cache regenerated for election %s' %
                    election.election_name)
    except nuancierlib.NuancierException as err:
        SESSION.rollback()
        LOG.debug('User: "******" could not generate cache for "%s"',
                  flask.g.fas_user.username, election_id)
        LOG.exception(err)
        flask.flash(err.message, 'error')

    return flask.redirect(next_url)
Example #3
0
def admin_edit(election_id):
    ''' Edit an election. '''
    if not nuancier.is_nuancier_admin(flask.g.fas_user):
        flask.flash('You are not an administrator of nuancier',
                        'error')
        return flask.redirect(flask.url_for('msg'))

    election = nuancierlib.get_election(SESSION, election_id)

    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    form = nuancier.forms.AddElectionForm()
    if flask.request.method == 'GET':
        form = nuancier.forms.AddElectionForm(election=election)

    if form.validate_on_submit():
        try:
            election = nuancierlib.edit_election(
                SESSION,
                election=election,
                election_name=form.election_name.data,
                election_folder=form.election_folder.data,
                election_year=form.election_year.data,
                election_date_start=form.election_date_start.data,
                election_date_end=form.election_date_end.data,
                submission_date_start=form.submission_date_start.data,
                submission_date_end=form.submission_date_end.data,
                election_n_choice=form.election_n_choice.data,
                user_n_candidates=form.user_n_candidates.data,
                election_badge_link=form.election_badge_link.data,
                user=flask.g.fas_user.username,
            )
            SESSION.commit()
            flask.flash('Election updated')
        except SQLAlchemyError as err:
            SESSION.rollback()
            LOG.debug("User: %s could not edit election: %s ",
                      flask.g.fas_user.username, election_id)
            LOG.exception(err)
            flask.flash('Could not edit this election, is this name or '
                        'folder already used?', 'error')
            return flask.render_template(
                'admin_edit.html',
                election=election,
                form=form)

        return flask.redirect(flask.url_for('admin_index'))
    return flask.render_template(
        'admin_edit.html',
        election=election,
        form=form)
Example #4
0
def admin_edit(election_id):
    ''' Edit an election. '''
    if not nuancier.is_nuancier_admin(flask.g.fas_user):
        flask.flash('You are not an administrator of nuancier', 'error')
        return flask.redirect(flask.url_for('msg'))

    election = nuancierlib.get_election(SESSION, election_id)

    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    form = nuancier.forms.AddElectionForm()
    if flask.request.method == 'GET':
        form = nuancier.forms.AddElectionForm(election=election)

    if form.validate_on_submit():
        try:
            election = nuancierlib.edit_election(
                SESSION,
                election=election,
                election_name=form.election_name.data,
                election_folder=form.election_folder.data,
                election_year=form.election_year.data,
                election_date_start=form.election_date_start.data,
                election_date_end=form.election_date_end.data,
                submission_date_start=form.submission_date_start.data,
                submission_date_end=form.submission_date_end.data,
                election_n_choice=form.election_n_choice.data,
                user_n_candidates=form.user_n_candidates.data,
                election_badge_link=form.election_badge_link.data,
                user=flask.g.fas_user.username,
            )
            SESSION.commit()
            flask.flash('Election updated')
        except SQLAlchemyError as err:
            SESSION.rollback()
            LOG.debug("User: %s could not edit election: %s ",
                      flask.g.fas_user.username, election_id)
            LOG.exception(err)
            flask.flash(
                'Could not edit this election, is this name or '
                'folder already used?', 'error')
            return flask.render_template('admin_edit.html',
                                         election=election,
                                         form=form)

        return flask.redirect(flask.url_for('admin_index'))
    return flask.render_template('admin_edit.html',
                                 election=election,
                                 form=form)
Example #5
0
def admin_new():
    ''' Create a new election. '''
    if not nuancier.is_nuancier_admin(flask.g.fas_user):
        flask.flash('You are not an administrator of nuancier', 'error')
        return flask.redirect(flask.url_for('msg'))

    form = nuancier.forms.AddElectionForm()
    if form.validate_on_submit():

        try:
            election = nuancierlib.add_election(
                SESSION,
                election_name=form.election_name.data,
                election_folder=form.election_folder.data,
                election_year=form.election_year.data,
                election_date_start=form.election_date_start.data,
                election_date_end=form.election_date_end.data,
                submission_date_start=form.submission_date_start.data,
                submission_date_end=form.submission_date_end.data,
                election_n_choice=form.election_n_choice.data,
                user_n_candidates=form.user_n_candidates.data,
                election_badge_link=form.election_badge_link.data,
                user=flask.g.fas_user.username,
            )

            SESSION.commit()
        except SQLAlchemyError as err:
            SESSION.rollback()
            LOG.debug("User: %s could not add an election",
                      flask.g.fas_user.username)
            LOG.exception(err)
            flask.flash(
                'Could not add this election, is this name or '
                'folder already used?', 'error')
            return flask.render_template('admin_new.html', form=form)

        flask.flash('Election created')
        if form.generate_cache.data:
            return admin_cache(election.id)

        return flask.redirect(flask.url_for('admin_index'))
    return flask.render_template('admin_new.html', form=form)
Example #6
0
def admin_cache(election_id):
    ''' Regenerate the cache for this election. '''
    election = nuancierlib.get_election(SESSION, election_id)

    next_url = None
    if 'next' in flask.request.args:
        next_url = flask.request.args['next']

    if not next_url or next_url == flask.url_for('.admin_cache',
                                                 election_id=election_id):
        next_url = flask.url_for('.admin_index')

    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    try:
        nuancierlib.generate_cache(session=SESSION,
                                   election=election,
                                   picture_folder=APP.config['PICTURE_FOLDER'],
                                   cache_folder=APP.config['CACHE_FOLDER'],
                                   size=APP.config['THUMB_SIZE'])
        flask.flash('Cache regenerated for election %s' %
                    election.election_name)
    except nuancierlib.NuancierMultiExceptions as multierr:  # pragma: no cover
        SESSION.rollback()
        LOG.debug('User: "******" could not generate cache for "%s"',
                  flask.g.fas_user.username, election_id)
        LOG.exception(multierr.messages)
        for msg in multierr.messages:
            flask.flash(msg, 'error')
    except nuancierlib.NuancierException as err:
        SESSION.rollback()
        LOG.debug('User: "******" could not generate cache for "%s"',
                  flask.g.fas_user.username, election_id)
        LOG.exception(err)
        flask.flash(str(err), 'error')

    return flask.redirect(next_url)
Example #7
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)
Example #8
0
def update_candidate(cand_id):
    ''' Display the index page for interested contributor. '''
    candidate = nuancierlib.get_candidate(SESSION, cand_id)

    # First some security checks
    if not candidate:
        flask.flash('No candidate found', 'error')
        return flask.render_template('msg.html')
    elif not candidate.election.submission_open:
        flask.flash(
            'The election of this candidate is not open for submission',
            'error')
        return flask.redirect(flask.url_for('elections_list'))
    elif candidate.approved:
        flask.flash(
            'This candidate was already approved, you cannot update it',
            'error')
        return flask.redirect(flask.url_for('elections_list'))
    elif candidate.candidate_submitter != flask.g.fas_user.username:
        flask.flash(
            'You are not the person that submitted this candidate, you may '
            'not update it', 'error')
        return flask.redirect(flask.url_for('elections_list'))

    form = nuancier.forms.AddCandidateForm(obj=candidate)
    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,
                candidate.election.id)
            LOG.exception(err)
            flask.flash('%s' % err, 'error')
            return flask.render_template('update_contribution.html',
                                         candidate=candidate,
                                         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'],
                                     candidate.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('update_contribution.html',
                                             candidate=candidate,
                                             form=form)

        # Update the candidate
        form.populate_obj(obj=candidate)
        candidate.candidate_file = filename
        candidate.approved = False
        candidate.approved_motif = None
        SESSION.add(candidate)

        # 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
            LOG.debug(err)
            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,
                candidate.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('update_contribution.html',
                                         candidate=candidate,
                                         form=form)

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

    return flask.render_template('update_contribution.html',
                                 candidate=candidate,
                                 form=form)
Example #9
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'))
Example #10
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'))
Example #11
0
def update_candidate(cand_id):
    ''' Display the index page for interested contributor. '''
    candidate = nuancierlib.get_candidate(SESSION, cand_id)

    # First some security checks
    if not candidate:
        flask.flash('No candidate found', 'error')
        return flask.render_template('msg.html')
    elif not candidate.election.submission_open:
        flask.flash(
            'The election of this candidate is not open for submission',
            'error')
        return flask.redirect(flask.url_for('elections_list'))
    elif candidate.approved:
        flask.flash(
            'This candidate was already approved, you cannot update it',
            'error')
        return flask.redirect(flask.url_for('elections_list'))
    elif candidate.candidate_submitter != flask.g.fas_user.username:
        flask.flash(
            'You are not the person that submitted this candidate, you may '
            'not update it', 'error')
        return flask.redirect(flask.url_for('elections_list'))

    form = nuancier.forms.AddCandidateForm(obj=candidate)
    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,
                      candidate.election.id)
            LOG.exception(err)
            flask.flash(err.message, 'error')
            return flask.render_template(
                'update_contribution.html',
                candidate=candidate,
                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'],
            candidate.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(
                    'update_contribution.html',
                    candidate=candidate,
                    form=form)

        # Update the candidate
        form.populate_obj(obj=candidate)
        candidate.candidate_file = filename
        candidate.approved = False
        candidate.approved_motif = None
        SESSION.add(candidate)

        # 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
            LOG.debug(err)
            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,
                      candidate.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(
                'update_contribution.html',
                candidate=candidate,
                form=form)

        flask.flash('Thanks for updating your submission')
        return flask.redirect(flask.url_for('index'))
Example #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'))
Example #13
0
def admin_process_review(election_id):
    ''' Process the reviewing of a new election. '''
    if not nuancier.is_nuancier_admin(flask.g.fas_user):
        flask.flash('You are not an administrator of nuancier',
                        'error')
        return flask.redirect(flask.url_for('msg'))

    status = flask.request.args.get('status', None)
    endpoint = 'admin_review'
    if status:
        endpoint = 'admin_review_status'

    election = nuancierlib.get_election(SESSION, election_id)

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

    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    if election.election_open:
        flask.flash(
            'This election is already open to public votes and can no '
            'longer be changed', 'error')
        return flask.redirect(flask.url_for('results_list'))

    if election.election_public:
        flask.flash(
            'The results of this election are already public, this election'
            ' can no longer be changed', 'error')
        return flask.redirect(flask.url_for('results_list'))

    candidates = nuancierlib.get_candidates(SESSION, election_id)
    candidates_id = [str(candidate.id) for candidate in candidates]

    candidates_selected = flask.request.form.getlist('candidates_id')
    motifs = flask.request.form.getlist('motifs')
    action = flask.request.form.get('action')

    if action:
        action = action.strip()

    if action not in ['Approved', 'Denied']:
        flask.flash(
            'Only the actions "Approved" or "Denied" are accepted',
            'error')
        return flask.redirect(flask.url_for(
                endpoint, election_id=election_id, status=status))

    selections = []
    for cand in candidates_id:
        if cand not in candidates_selected:
            selections.append(None)
        else:
            selections.append(cand)

    if action == 'Denied':
        req_motif = False
        if not motifs:
            req_motif = True
        for cnt in range(len(motifs)):
            motif = motifs[cnt]
            if selections[cnt] and not motif.strip():
                req_motif = True
                break
        if req_motif:
            flask.flash(
                'You must provide a motif to deny a candidate',
                'error')
            return flask.redirect(flask.url_for(
                endpoint, election_id=election_id, status=status))

    cnt = 0
    for candidate in candidates_selected:
        if candidate not in candidates_id:
            flask.flash(
                'One of the candidate submitted was not candidate in this '
                'election', 'error')
            return flask.redirect(flask.url_for(
                endpoint, election_id=election_id, status=status))

    msgs = []

    for candidate in selections:
        if candidate:
            candidate = nuancierlib.get_candidate(SESSION, candidate)
            motif = None
            if len(motifs) > cnt:
                motif = motifs[cnt].strip()
            if action == 'Approved':
                candidate.approved = True
                candidate.approved_motif = motif
            else:
                candidate.approved = False
                candidate.approved_motif = motif
                if APP.config.get(
                        'NUANCIER_EMAIL_NOTIFICATIONS',
                        False):  # pragma: no cover
                    nuancierlib.notifications.email_publish(
                        to_email=candidate.submitter_email,
                        img_title=candidate.candidate_name,
                        motif=motif)
                else:
                    LOG.warning(
                        'Should have sent an email to "%s" about "%s" that has'
                        ' been rejected because of "%s"',
                        candidate.submitter_email,
                        candidate.candidate_name,
                        motif)

            SESSION.add(candidate)
            msgs.append({
                'topic': 'candidate.%s' % (action.lower()),
                'msg': dict(
                    agent=flask.g.fas_user.username,
                    election=election.api_repr(version=1),
                    candidate=candidate.api_repr(version=1),
                )
            })
        cnt += 1

    try:
        SESSION.commit()
    except SQLAlchemyError as err:  # pragma: no cover
        SESSION.rollback()
        LOG.debug('User: "******" could not approve/deny candidate(s) for '
                  'election "%s"', flask.g.fas_user.username,
                  election_id)
        LOG.exception(err)
        flask.flash('Could not approve/deny candidate', 'error')

    flask.flash('Candidate(s) updated')

    for msg in msgs:
        nuancierlib.notifications.publish(
            topic=msg['topic'],
            msg=msg['msg'],
        )

    return flask.redirect(flask.url_for(
        endpoint, election_id=election_id, status=status))
Example #14
0
def admin_process_review(election_id):
    ''' Process the reviewing of a new election. '''
    if not nuancier.is_nuancier_admin(flask.g.fas_user):
        flask.flash('You are not an administrator of nuancier', 'error')
        return flask.redirect(flask.url_for('msg'))

    status = flask.request.args.get('status', None)
    endpoint = 'admin_review'
    if status:
        endpoint = 'admin_review_status'

    election = nuancierlib.get_election(SESSION, election_id)

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

    if not election:
        flask.flash('No election found', 'error')
        return flask.render_template('msg.html')

    if election.election_open:
        flask.flash(
            'This election is already open to public votes and can no '
            'longer be changed', 'error')
        return flask.redirect(flask.url_for('results_list'))

    if election.election_public:
        flask.flash(
            'The results of this election are already public, this election'
            ' can no longer be changed', 'error')
        return flask.redirect(flask.url_for('results_list'))

    candidates = nuancierlib.get_candidates(SESSION, election_id)
    candidates_id = [str(candidate.id) for candidate in candidates]

    candidates_selected = flask.request.form.getlist('candidates_id')
    motifs = flask.request.form.getlist('motifs')
    action = flask.request.form.get('action')

    if action:
        action = action.strip()

    if action not in ['Approved', 'Denied']:
        flask.flash('Only the actions "Approved" or "Denied" are accepted',
                    'error')
        return flask.redirect(
            flask.url_for(endpoint, election_id=election_id, status=status))

    selections = []
    for cand in candidates_id:
        if cand not in candidates_selected:
            selections.append(None)
        else:
            selections.append(cand)

    if action == 'Denied':
        req_motif = False
        if not motifs:
            req_motif = True
        for cnt in range(len(motifs)):
            motif = motifs[cnt]
            if selections[cnt] and not motif.strip():
                req_motif = True
                break
        if req_motif:
            flask.flash('You must provide a reason to deny a candidate',
                        'error')
            return flask.redirect(
                flask.url_for(endpoint, election_id=election_id,
                              status=status))

    cnt = 0
    for candidate in candidates_selected:
        if candidate not in candidates_id:
            flask.flash(
                'One of the candidate submitted was not candidate in this '
                'election', 'error')
            return flask.redirect(
                flask.url_for(endpoint, election_id=election_id,
                              status=status))

    msgs = []

    for candidate in selections:
        if candidate:
            candidate = nuancierlib.get_candidate(SESSION, candidate)
            motif = None
            if len(motifs) > cnt:
                motif = motifs[cnt].strip()
            if action == 'Approved':
                candidate.approved = True
                candidate.approved_motif = motif
            else:
                candidate.approved = False
                candidate.approved_motif = motif
                if APP.config.get('NUANCIER_EMAIL_NOTIFICATIONS',
                                  False):  # pragma: no cover
                    nuancierlib.notifications.email_publish(
                        to_email=candidate.submitter_email,
                        img_title=candidate.candidate_name,
                        motif=motif)
                else:
                    LOG.warning(
                        'Should have sent an email to "%s" about "%s" that has'
                        ' been rejected because of "%s"',
                        candidate.submitter_email, candidate.candidate_name,
                        motif)

            SESSION.add(candidate)
            msgs.append({
                'topic':
                'candidate.%s' % (action.lower()),
                'msg':
                dict(
                    agent=flask.g.fas_user.username,
                    election=election.api_repr(version=1),
                    candidate=candidate.api_repr(version=1),
                )
            })
        cnt += 1

    try:
        SESSION.commit()
    except SQLAlchemyError as err:  # pragma: no cover
        SESSION.rollback()
        LOG.debug(
            'User: "******" could not approve/deny candidate(s) for '
            'election "%s"', flask.g.fas_user.username, election_id)
        LOG.exception(err)
        flask.flash('Could not approve/deny candidate', 'error')

    flask.flash('Candidate(s) updated')

    for msg in msgs:
        nuancierlib.notifications.publish(
            topic=msg['topic'],
            msg=msg['msg'],
        )

    return flask.redirect(
        flask.url_for(endpoint, election_id=election_id, status=status))