Beispiel #1
0
def admin_delete_candidate(election_alias, candidate_id):
    election = models.Election.get(SESSION, alias=election_alias)
    candidate = models.Candidate.get(SESSION, candidate_id=candidate_id)

    if not election or not candidate:
        flask.abort(404)

    if candidate.election != election:
        flask.abort(404)

    form = forms.ConfirmationForm()
    if form.validate_on_submit():
        candidate_name = candidate.name
        try:
            SESSION.delete(candidate)
            SESSION.commit()
            flask.flash('Candidate "%s" deleted' % candidate_name)
            fedmsgshim.publish(
                topic="candidate.delete",
                msg=dict(
                    agent=flask.g.fas_user.username,
                    election=candidate.election.to_json(),
                    candidate=candidate.to_json(),
                )
            )
        except SQLAlchemyError, err:
            SESSION.rollback()
            APP.logger.debug('Could not delete candidate')
            APP.logger.exception(err)
            flask.flash(
                'Could not delete this candidate. Is it already part of an '
                'election?', 'error'
            )
        return flask.redirect(flask.url_for(
            'admin_view_election', election_alias=election.alias))
Beispiel #2
0
def admin_delete_candidate(election_alias, candidate_id):
    election = models.Election.get(SESSION, alias=election_alias)
    candidate = models.Candidate.get(SESSION, candidate_id=candidate_id)

    if not election or not candidate:
        flask.abort(404)

    if candidate.election != election:
        flask.abort(404)

    form = forms.ConfirmationForm()
    if form.validate_on_submit():
        candidate_name = candidate.name
        try:
            SESSION.delete(candidate)
            SESSION.commit()
            flask.flash('Candidate "%s" deleted' % candidate_name)
            fedmsgshim.publish(topic="candidate.delete",
                               msg=dict(
                                   agent=flask.g.fas_user.username,
                                   election=candidate.election.to_json(),
                                   candidate=candidate.to_json(),
                               ))
        except SQLAlchemyError, err:
            SESSION.rollback()
            APP.logger.debug('Could not delete candidate')
            APP.logger.exception(err)
            flask.flash(
                'Could not delete this candidate. Is it already part of an '
                'election?', 'error')
        return flask.redirect(
            flask.url_for('admin_view_election',
                          election_alias=election.alias))
Beispiel #3
0
def admin_add_multi_candidate(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    if not election:
        flask.abort(404)

    form = forms.MultiCandidateForm()
    if form.validate_on_submit():

        candidates_name = []
        for entry in form.candidate.data.strip().split("|"):
            candidate = entry.split("!")

            fas_name = None
            if election.candidates_are_fasusers:  # pragma: no cover
                try:
                    fas_name = FAS2.person_by_username(
                        candidate[0])['human_name']
                except (KeyError, AuthError):
                    SESSION.rollback()
                    flask.flash(
                        'User `%s` does not have a FAS account.' %
                        candidate[0], 'error')
                    return flask.redirect(
                        flask.url_for('admin_add_candidate',
                                      election_alias=election_alias))

            # No url
            if len(candidate) == 1:
                cand = models.Candidate(election=election,
                                        name=candidate[0],
                                        fas_name=fas_name)
                SESSION.add(cand)
                candidates_name.append(cand.name)
            # With url
            elif len(candidate) == 2:
                cand = models.Candidate(election=election,
                                        name=candidate[0],
                                        url=candidate[1],
                                        fas_name=fas_name)
                SESSION.add(cand)
                candidates_name.append(cand.name)
            else:
                flask.flash("There was an issue!")
            fedmsgshim.publish(topic="candidate.new",
                               msg=dict(
                                   agent=flask.g.fas_user.username,
                                   election=cand.election.to_json(),
                                   candidate=cand.to_json(),
                               ))

        SESSION.commit()
        flask.flash('Added %s candidates' % len(candidates_name))
        return flask.redirect(
            flask.url_for('admin_view_election',
                          election_alias=election.alias))

    return flask.render_template('admin/candidate_multi.html',
                                 form=form,
                                 submit_text='Add candidates')
Beispiel #4
0
def admin_add_multi_candidate(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    if not election:
        flask.abort(404)

    form = forms.MultiCandidateForm()
    if form.validate_on_submit():

        candidates_name = []
        for entry in form.candidate.data.strip().split("|"):
            candidate = entry.split("!")

            fas_name = None
            if election.candidates_are_fasusers:  # pragma: no cover
                try:
                    fas_name = FAS2.person_by_username(
                        candidate[0])['human_name']
                except (KeyError, AuthError), err:
                    SESSION.rollback()
                    flask.flash(
                        'User `%s` does not have a FAS account.'
                        % candidate[0], 'error')
                    return flask.redirect(
                        flask.url_for(
                            'admin_add_candidate',
                            election_alias=election_alias))

            # No url
            if len(candidate) == 1:
                cand = models.Candidate(
                    election=election,
                    name=candidate[0],
                    fas_name=fas_name)
                SESSION.add(cand)
                candidates_name.append(cand.name)
            # With url
            elif len(candidate) == 2:
                cand = models.Candidate(
                    election=election,
                    name=candidate[0],
                    url=candidate[1],
                    fas_name=fas_name)
                SESSION.add(cand)
                candidates_name.append(cand.name)
            else:
                flask.flash("There was an issue!")
            fedmsgshim.publish(
                topic="candidate.new",
                msg=dict(
                    agent=flask.g.fas_user.username,
                    election=cand.election.to_json(),
                    candidate=cand.to_json(),
                )
            )

        SESSION.commit()
        flask.flash('Added %s candidates' % len(candidates_name))
        return flask.redirect(flask.url_for(
            'admin_view_election', election_alias=election.alias))
Beispiel #5
0
def admin_edit_candidate(election_alias, candidate_id):
    election = models.Election.get(SESSION, alias=election_alias)
    candidate = models.Candidate.get(SESSION, candidate_id=candidate_id)

    if not election or not candidate:
        flask.abort(404)

    if candidate.election != election:
        flask.abort(404)

    form = forms.CandidateForm(obj=candidate)
    if form.validate_on_submit():
        form.populate_obj(candidate)

        if election.candidates_are_fasusers:  # pragma: no cover
            try:
                if APP.config.get('FASJSON'):
                    user = ACCOUNTS.get_user(
                        username=candidate.name).result
                    candidate.fas_name = f'{user['givenname']} {user['surname']}'
                else:
                    candidate.fas_name = ACCOUNTS.person_by_username(
                        candidate.name)['human_name']
            except (KeyError, AuthError, APIError):
                SESSION.rollback()
                flask.flash(
                    'User `%s` does not have a FAS account.'
                    % candidate.name, 'error')
                return flask.redirect(flask.url_for(
                    'admin_edit_candidate',
                    election_alias=election_alias,
                    candidate_id=candidate_id))

        SESSION.commit()
        flask.flash('Candidate "%s" saved' % candidate.name)
        fedmsgshim.publish(
            topic="candidate.edit",
            msg=dict(
                agent=flask.g.fas_user.username,
                election=candidate.election.to_json(),
                candidate=candidate.to_json(),
            )
        )
        return flask.redirect(flask.url_for(
            'admin_view_election', election_alias=election.alias))

    return flask.render_template(
        'admin/candidate.html',
        form=form,
        submit_text='Edit candidate')
Beispiel #6
0
def admin_edit_candidate(election_alias, candidate_id):
    election = models.Election.get(SESSION, alias=election_alias)
    candidate = models.Candidate.get(SESSION, candidate_id=candidate_id)

    if not election or not candidate:
        flask.abort(404)

    if candidate.election != election:
        flask.abort(404)

    form = forms.CandidateForm(obj=candidate)
    if form.validate_on_submit():
        form.populate_obj(candidate)

        if election.candidates_are_fasusers:  # pragma: no cover
            try:
                candidate.fas_name = FAS2.person_by_username(
                    candidate.name)['human_name']
            except (KeyError, AuthError):
                SESSION.rollback()
                flask.flash(
                    'User `%s` does not have a FAS account.'
                    % candidate.name, 'error')
                return flask.redirect(flask.url_for(
                    'admin_edit_candidate',
                    election_alias=election_alias,
                    candidate_id=candidate_id))

        SESSION.commit()
        flask.flash('Candidate "%s" saved' % candidate.name)
        fedmsgshim.publish(
            topic="candidate.edit",
            msg=dict(
                agent=flask.g.fas_user.username,
                election=candidate.election.to_json(),
                candidate=candidate.to_json(),
            )
        )
        return flask.redirect(flask.url_for(
            'admin_view_election', election_alias=election.alias))

    return flask.render_template(
        'admin/candidate.html',
        form=form,
        submit_text='Edit candidate')