Exemple #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))
Exemple #2
0
def admin_add_candidate(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    if not election:
        flask.abort(404)

    form = forms.CandidateForm()
    if form.validate_on_submit():
        candidate = models.Candidate(
            election=election,
            name=form.name.data,
            url=form.url.data
        )

        SESSION.add(candidate)
        SESSION.commit()
        flask.flash('Candidate "%s" saved' % candidate.name)
        fedmsgshim.publish(
            topic="candidate.new",
            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='Add candidate')
Exemple #3
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)
        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')
Exemple #4
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))
Exemple #5
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')
Exemple #6
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))
Exemple #7
0
def admin_add_candidate(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    if not election:
        flask.abort(404)

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

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

        candidate = models.Candidate(
            election=election,
            name=form.name.data,
            url=form.url.data,
            fas_name=fas_name,
        )

        SESSION.add(candidate)
        SESSION.commit()
        flask.flash('Candidate "%s" saved' % candidate.name)
        fedmsgshim.publish(
            topic="candidate.new",
            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='Add candidate')
Exemple #8
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')
Exemple #9
0
def admin_add_candidate(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    if not election:
        flask.abort(404)

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

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

        candidate = models.Candidate(
            election=election,
            name=form.name.data,
            url=form.url.data,
            fas_name=fas_name,
        )

        SESSION.add(candidate)
        SESSION.commit()
        flask.flash('Candidate "%s" saved' % candidate.name)
        fedmsgshim.publish(
            topic="candidate.new",
            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='Add candidate')
Exemple #10
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("!")
            # No url
            if len(candidate) == 1:
                cand = models.Candidate(
                    election=election,
                    name=candidate[0])
                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])
                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')
Exemple #11
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')
Exemple #12
0
def admin_new_election():
    form = forms.ElectionForm()
    if form.validate_on_submit():

        if form.max_votes.data:
            try:
                form.max_votes.data = int(form.max_votes.data)
            except ValueError:
                form.max_votes.data = None
        else:
            form.max_votes.data = None

        election = models.Election(
            shortdesc=form.shortdesc.data,
            alias=form.alias.data,
            description=form.description.data,
            url=form.url.data,
            start_date=form.start_date.data,
            end_date=form.end_date.data,
            seats_elected=form.seats_elected.data,
            embargoed=int(form.embargoed.data),
            voting_type=form.voting_type.data,
            max_votes=form.max_votes.data,
            candidates_are_fasusers=int(form.candidates_are_fasusers.data),
            fas_user=flask.g.fas_user.username,
        )

        # Fix start_date and end_date to use datetime
        election.start_date = datetime.combine(election.start_date, time())
        election.end_date = datetime.combine(election.end_date,
                                             time(23, 59, 59))
        SESSION.add(election)

        # Add admin groups if there are any
        for admin_grp in form.admin_grp.data.split(','):
            if admin_grp.strip():
                admin = models.ElectionAdminGroup(
                    election=election,
                    group_name=admin_grp.strip(),
                )
                SESSION.add(admin)

        # Add legal voters groups if there are any
        for voter_grp in form.lgl_voters.data.split(','):
            if voter_grp.strip():
                lglvoters = models.LegalVoter(
                    election=election,
                    group_name=voter_grp.strip(),
                )
                SESSION.add(lglvoters)

        SESSION.commit()

        fedmsgshim.publish(
            topic="election.new",
            msg=dict(
                agent=flask.g.fas_user.username,
                election=election.to_json(),
                )
        )

        flask.flash('Election "%s" added' % election.alias)
        fedmsgshim.publish(topic="election.new", msg=election)
        return flask.redirect(flask.url_for(
            'admin_view_election', election_alias=election.alias))
    return flask.render_template(
        'admin/election_form.html',
        form=form,
        submit_text='Create election')
Exemple #13
0
def admin_edit_election(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    if not election:
        flask.abort(404)

    form = forms.ElectionForm(election.id, obj=election)
    if form.validate_on_submit():
        form.embargoed.data = int(form.embargoed.data)
        if form.max_votes.data:
            try:
                form.max_votes.data = int(form.max_votes.data)
            except ValueError:
                form.max_votes.data = None
        else:
            form.max_votes.data = None

        form.candidates_are_fasusers.data = int(
            form.candidates_are_fasusers.data)
        form.populate_obj(election)

        admin_groups = set(election.admin_groups_list)

        new_groups = set(
            [grp.strip() for grp in form.admin_grp.data.split(',')])

        # Add the new admin groups
        for admin_grp in new_groups.difference(admin_groups):
            admin = models.ElectionAdminGroup(
                election=election,
                group_name=admin_grp,
            )
            SESSION.add(admin)

        # Remove the admin groups that were removed with this edition
        for admin_grp in admin_groups.difference(new_groups):
            admingrp = models.ElectionAdminGroup.by_election_id_and_name(
                SESSION, election.id, admin_grp)
            SESSION.delete(admingrp)

        legal_voters = set(election.legal_voters_list)

        new_lgl_voters_groups = set(
            [grp.strip() for grp in form.lgl_voters.data.split(',')
             if grp.strip()])

        # Add the new legal voter groups
        for lgl_grp in new_lgl_voters_groups.difference(legal_voters):
            admin = models.LegalVoter(
                election=election,
                group_name=lgl_grp,
            )
            SESSION.add(admin)

        # Remove the legal voter groups that were removed with this edition
        for lgl_grp in legal_voters.difference(new_lgl_voters_groups):
            admingrp = models.LegalVoter.by_election_id_and_name(
                SESSION, election.id, lgl_grp)
            SESSION.delete(admingrp)

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

    form.admin_grp.data = ', '.join(election.admin_groups_list)
    form.lgl_voters.data = ', '.join(election.legal_voters_list)
    return flask.render_template(
        'admin/election_form.html',
        form=form,
        submit_text='Edit election')
Exemple #14
0
def admin_new_election():
    form = forms.ElectionForm()
    if form.validate_on_submit():

        if form.max_votes.data:
            try:
                form.max_votes.data = int(form.max_votes.data)
            except ValueError:
                form.max_votes.data = None
        else:
            form.max_votes.data = None

        election = models.Election(
            shortdesc=form.shortdesc.data,
            alias=form.alias.data,
            description=form.description.data,
            url=form.url.data,
            start_date=form.start_date.data,
            end_date=form.end_date.data,
            seats_elected=form.seats_elected.data,
            embargoed=int(form.embargoed.data),
            voting_type=form.voting_type.data,
            max_votes=form.max_votes.data,
            candidates_are_fasusers=int(form.candidates_are_fasusers.data),
            fas_user=flask.g.fas_user.username,
        )

        # Fix start_date and end_date to use datetime
        election.start_date = datetime.combine(election.start_date, time())
        election.end_date = datetime.combine(election.end_date,
                                             time(23, 59, 59))
        SESSION.add(election)

        # Add admin groups if there are any
        for admin_grp in form.admin_grp.data.split(','):
            if admin_grp.strip():
                admin = models.ElectionAdminGroup(
                    election=election,
                    group_name=admin_grp.strip(),
                )
                SESSION.add(admin)

        # Add legal voters groups if there are any
        for voter_grp in form.lgl_voters.data.split(','):
            if voter_grp.strip():
                lglvoters = models.LegalVoter(
                    election=election,
                    group_name=voter_grp.strip(),
                )
                SESSION.add(lglvoters)

        SESSION.commit()

        fedmsgshim.publish(topic="election.new",
                           msg=dict(
                               agent=flask.g.fas_user.username,
                               election=election.to_json(),
                           ))

        flask.flash('Election "%s" added' % election.alias)
        fedmsgshim.publish(topic="election.new", msg=election)
        return flask.redirect(
            flask.url_for('admin_view_election',
                          election_alias=election.alias))
    return flask.render_template('admin/election_form.html',
                                 form=form,
                                 submit_text='Create election')
Exemple #15
0
def admin_view_election(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    if not election:
        flask.abort(404)
    form = forms.ElectionForm(election.id, obj=election)
    if form.validate_on_submit():
        form.embargoed.data = int(form.embargoed.data)
        if form.max_votes.data:
            try:
                form.max_votes.data = int(form.max_votes.data)
            except ValueError:
                form.max_votes.data = None
        else:
            form.max_votes.data = None

        form.candidates_are_fasusers.data = int(
            form.candidates_are_fasusers.data)
        form.populate_obj(election)

        # Fix start_date and end_date to use datetime
        election.start_date = datetime.combine(election.start_date, time())
        election.end_date = datetime.combine(election.end_date,
                                             time(23, 59, 59))
        SESSION.add(election)

        admin_groups = set(election.admin_groups_list)

        new_groups = set(
            [grp.strip() for grp in form.admin_grp.data.split(',')])

        # Add the new admin groups
        for admin_grp in new_groups.difference(admin_groups):
            admin = models.ElectionAdminGroup(
                election=election,
                group_name=admin_grp,
            )
            SESSION.add(admin)

        # Remove the admin groups that were removed with this edition
        for admin_grp in admin_groups.difference(new_groups):
            admingrp = models.ElectionAdminGroup.by_election_id_and_name(
                SESSION, election.id, admin_grp)
            SESSION.delete(admingrp)

        legal_voters = set(election.legal_voters_list)

        new_lgl_voters_groups = set([
            grp.strip() for grp in form.lgl_voters.data.split(',')
            if grp.strip()
        ])

        # Add the new legal voter groups
        for lgl_grp in new_lgl_voters_groups.difference(legal_voters):
            admin = models.LegalVoter(
                election=election,
                group_name=lgl_grp,
            )
            SESSION.add(admin)

        # Remove the legal voter groups that were removed with this edition
        for lgl_grp in legal_voters.difference(new_lgl_voters_groups):
            admingrp = models.LegalVoter.by_election_id_and_name(
                SESSION, election.id, lgl_grp)
            SESSION.delete(admingrp)

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

    form.admin_grp.data = ', '.join(election.admin_groups_list)
    form.lgl_voters.data = ', '.join(election.legal_voters_list)

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