コード例 #1
0
ファイル: admin.py プロジェクト: cclauss/elections
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')
コード例 #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():

        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')
コード例 #3
0
def admin_add_candidate(election_alias):
    election = models.Election.query.filter_by(alias=election_alias).first()
    if not election:
        flask.abort(404)
    form = forms.CandidateForm()
    if form.validate_on_submit():
        candidate = models.Candidate(election=election,
                                     **remove_csrf(form.data))
        db.session.add(candidate)
        db.session.commit()
        flask.flash('Candidate "%s" saved' % candidate.name)
        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')
コード例 #4
0
    def setUp(self):
        super(TestSmoke, self).setUp()
        fas.login = Mock(return_value=True)
        # Add an election
        election = models.Election(summary='Test Election 1',
                                   alias='test1',
                                   description='lorem ipsum of course',
                                   url='http://fedorahosted.org/elections',
                                   start_date=datetime.datetime.utcnow(),
                                   end_date=datetime.datetime.utcnow() +
                                   datetime.timedelta(hours=1))
        db.session.add(election)
        # Add candidate to election
        candidate = models.Candidate(election=election,
                                     name='Mr Testy',
                                     url='http://fedoraproject.org')
        db.session.add(candidate)
        db.session.commit()

        self.url_subs = copy(url_subs)
        self.url_subs['election_alias'] = 'test1'
        self.url_subs['candidate_id'] = candidate.id
コード例 #5
0
    def test_init_candidate(self):
        """ Test the Candidate init function. """
        elections = Electiontests('test_init_election')
        elections.session = self.session
        elections.test_init_election()

        #
        # Election #1
        #

        obj = models.Candidate(  # id:1
            election_id=1,
            name='Toshio',
            url='https://fedoraproject.org/wiki/User:Toshio',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:2
            election_id=1,
            name='Kevin',
            url='https://fedoraproject.org/wiki/User:Kevin',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:3
            election_id=1,
            name='Ralph',
            url='https://fedoraproject.org/wiki/User:Ralph',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        #
        # Election #3
        #

        obj = models.Candidate(  # id:4
            election_id=3,
            name='Toshio',
            url='https://fedoraproject.org/wiki/User:Toshio',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:5
            election_id=3,
            name='Kevin',
            url='https://fedoraproject.org/wiki/User:Kevin',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:6
            election_id=3,
            name='Ralph',
            url='https://fedoraproject.org/wiki/User:Ralph',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        #
        # Election #5
        #

        obj = models.Candidate(  # id:7
            election_id=5,
            name='Toshio',
            url='https://fedoraproject.org/wiki/User:Toshio',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:8
            election_id=5,
            name='Kevin',
            url='https://fedoraproject.org/wiki/User:Kevin',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:9
            election_id=5,
            name='Ralph',
            url='https://fedoraproject.org/wiki/User:Ralph',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        #
        # Election #4
        #

        obj = models.Candidate(  # id:10
            election_id=4,
            name='Toshio',
            url='https://fedoraproject.org/wiki/User:Toshio',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:11
            election_id=4,
            name='Kevin',
            url='https://fedoraproject.org/wiki/User:Kevin',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        #
        # Election #6
        #

        obj = models.Candidate(  # id:12
            election_id=6,
            name='Toshio',
            url='https://fedoraproject.org/wiki/User:Toshio',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:13
            election_id=6,
            name='Kevin',
            url='https://fedoraproject.org/wiki/User:Kevin',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        #
        # Election #7
        #

        obj = models.Candidate(  # id:14
            election_id=7,
            name='Toshio',
            url='https://fedoraproject.org/wiki/User:Toshio',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)

        obj = models.Candidate(  # id:15
            election_id=7,
            name='Kevin',
            url='https://fedoraproject.org/wiki/User:Kevin',
        )
        self.session.add(obj)
        self.session.commit()
        self.assertNotEqual(obj, None)