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("!") if election.candidates_are_fasusers: # pragma: no cover try: 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]) 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))
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')
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(): if election.candidates_are_fasusers: # pragma: no cover try: FAS2.person_by_username(form.name.data)['human_name'] except (KeyError, AuthError), err: 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 ) 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))
def get_simple_voting_form(candidates, fasusers): class SimpleVoting(wtf.Form): action = wtforms.HiddenField() titles = [] for candidate in candidates: title = candidate.name if fasusers: try: title = \ FAS2.person_by_username(candidate.name)['human_name'] except (KeyError, AuthError), err: APP.logger.debug(err) if candidate.url: title = '%s <a href="%s">[Info]</a>' % (title, candidate.url) titles.append((str(candidate.id), title))
def get_simple_voting_form(candidates, fasusers): class SimpleVoting(FlaskForm): action = wtforms.HiddenField() titles = [] for candidate in candidates: title = candidate.fas_name or candidate.name if fasusers: # pragma: no cover # We can't cover FAS integration try: title = \ FAS2.person_by_username(candidate.name)['human_name'] except (KeyError, AuthError), err: APP.logger.debug(err) if candidate.url: title = '%s <a href="%s">[Info]</a>' % (title, candidate.url) titles.append((str(candidate.id), title))
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')
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')
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')