def get_valid_election(election_alias, ended=False): """ Return the election if it is valid (not pending, not ended). """ election = models.Election.get(SESSION, alias=election_alias) if not election: flask.flash('The election, %s, does not exist.' % election_alias) return safe_redirect_back() if election.status == 'Pending': flask.flash('Voting has not yet started, sorry.') return safe_redirect_back() elif not ended and election.status in ('Ended', 'Embargoed'): flask.flash( 'This election is closed. You have been redirected to the ' 'election results.') return flask.redirect(flask.url_for( 'election_results', election_alias=election.alias)) elif ended and election.status == 'In progress': flask.flash( 'Sorry but this election is in progress, and you may not see its ' 'results yet.') return safe_redirect_back() return election
def vote(election_alias): election = get_valid_election(election_alias) if not isinstance(election, models.Election): return election if election.legal_voters_list: user_groups = OIDC.user_getfield('groups') if len(set(user_groups).intersection(set( election.legal_voters_list))) == 0: flask.flash( 'You are not among the groups that are allowed to vote ' 'for this election', 'error') return safe_redirect_back() votes = models.Vote.of_user_on_election(SESSION, flask.g.fas_user.username, election.id, count=True) revote = True if votes > 0 else False if election.voting_type.startswith('range'): return vote_range(election, revote) elif election.voting_type == 'simple': return vote_simple(election, revote) elif election.voting_type == 'select': return vote_select(election, revote) elif election.voting_type == 'irc': return vote_irc(election, revote) else: # pragma: no cover flask.flash('Unknown election voting type: %s' % election.voting_type) return safe_redirect_back()
def vote(election_alias): election = get_valid_election(election_alias) if not isinstance(election, models.Election): return election if election.legal_voters_list: if len(set(flask.g.fas_user.groups).intersection( set(election.legal_voters_list))) == 0: flask.flash( 'You are not among the groups that are allowed to vote ' 'for this election', 'error') return safe_redirect_back() votes = models.Vote.of_user_on_election( SESSION, flask.g.fas_user.username, election.id, count=True) revote = True if votes > 0 else False if election.voting_type.startswith('range'): return vote_range(election, revote) elif election.voting_type == 'simple': return vote_simple(election, revote) elif election.voting_type == 'select': return vote_select(election, revote) elif election.voting_type == 'irc': return vote_irc(election, revote) else: # pragma: no cover flask.flash( 'Unknown election voting type: %s' % election.voting_type) return safe_redirect_back()
def decorated_function(*args, **kwargs): if not is_authenticated(): return flask.redirect( flask.url_for('auth_login', next=flask.request.url)) elif not flask.g.fas_user.cla_done: flask.flash('You must sign the CLA to vote', 'error') return safe_redirect_back() elif len(flask.g.fas_user.groups) == 0: flask.flash('You need to be in one another group than CLA to vote', 'error') return safe_redirect_back() return f(*args, **kwargs)
def decorated_function(*args, **kwargs): if not is_authenticated(): return flask.redirect(flask.url_for( 'auth_login', next=flask.request.url)) elif not flask.g.fas_user.cla_done: flask.flash( 'You must sign the CLA to vote', 'error') return safe_redirect_back() elif len(flask.g.fas_user.groups) == 0: flask.flash( 'You need to be in one another group than CLA to vote', 'error') return safe_redirect_back() return f(*args, **kwargs)
def vote_irc(election, revote): votes = models.Vote.of_user_on_election(SESSION, flask.g.fas_user.username, election.id) cand_name = {} for candidate in election.candidates: cand_name[candidate.name] = candidate.id num_candidates = election.candidates.count() next_action = 'confirm' form = forms.get_irc_voting_form(candidates=election.candidates, fasusers=election.candidates_are_fasusers) if form.validate_on_submit(): if form.action.data == 'submit': candidates = [ candidate for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] process_vote(candidates, election, votes, revote, cand_name) say_thank_you(election) return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' return flask.render_template('vote_simple.html', election=election, form=form, num_candidates=num_candidates, nextaction=next_action)
def vote_irc(election, revote): votes = models.Vote.of_user_on_election( SESSION, flask.g.fas_user.username, election.id) cand_name = {} for candidate in election.candidates: cand_name[candidate.name] = candidate.id num_candidates = election.candidates.count() next_action = 'confirm' form = forms.get_irc_voting_form( candidates=election.candidates, fasusers=election.candidates_are_fasusers) if form.validate_on_submit(): if form.action.data == 'submit': candidates = [ candidate for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] process_vote(candidates, election, votes, revote, cand_name) flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' return flask.render_template( 'vote_simple.html', election=election, form=form, num_candidates=num_candidates, nextaction=next_action)
def vote_select(election, revote): votes = models.Vote.of_user_on_election( SESSION, flask.g.fas_user.username, election.id) num_candidates = election.candidates.count() cand_name = {} for candidate in election.candidates: cand_name[candidate.name] = candidate.id next_action = 'confirm' max_selection = num_candidates if election.max_votes: max_selection = election.max_votes form = forms.get_select_voting_form( candidates=election.candidates, max_selection=max_selection) if form.validate_on_submit(): cnt = [ candidate for candidate in form if candidate.data and candidate.short_name not in ['csrf_token', 'action'] ] if len(cnt) > max_selection: flask.flash('Too many candidates submitted', 'error') else: if form.action.data == 'submit': candidates = [ candidate for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] process_vote(candidates, election, votes, revote, cand_name) flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' usernamemap = build_name_map(election) return flask.render_template( 'vote_simple.html', election=election, form=form, num_candidates=num_candidates, max_selection=max_selection, usernamemap=usernamemap, nextaction=next_action)
def election_results(election_alias): election = get_valid_election(election_alias, ended=True) if not isinstance(election, models.Election): # pragma: no cover return election elif election.embargoed: if is_authenticated() and ( is_admin(flask.g.fas_user) or is_election_admin(flask.g.fas_user, election.id)): flask.flash("You are only seeing this page because you are " "an admin.", "warning") flask.flash("The results for this election are currently " "embargoed pending formal announcement.", "warning") else: flask.flash("We are sorry. The results for this election " "cannot be viewed because they are currently " "embargoed pending formal announcement.") return safe_redirect_back() if is_authenticated() and ( is_admin(flask.g.fas_user) or is_election_admin(flask.g.fas_user, election.id)): flask.flash( "Check out the <a href='%s'>Text version</a> " "to send the annoucement" % flask.url_for( 'election_results_text', election_alias=election.alias) ) usernamemap = build_name_map(election) stats = models.Vote.get_election_stats(SESSION, election.id) cnt = 1 evolution_label = [] evolution_data = [] for delta in range((election.end_date - election.start_date).days + 1): day = ( election.start_date + timedelta(days=delta) ).strftime('%d-%m-%Y') evolution_label.append([cnt, day]) evolution_data.append([cnt, stats['vote_timestamps'].count(day)]) cnt += 1 return flask.render_template( 'results.html', election=election, usernamemap=usernamemap, stats=stats, evolution_label=evolution_label, evolution_data=evolution_data, )
def vote_range(election): votes = models.Vote.of_user_on_election( SESSION, flask.g.fas_user.username, election.id, count=True) num_candidates = election.candidates.count() cand_ids = [str(cand.id) for cand in election.candidates] next_action = 'confirm' max_selection = num_candidates if election.max_votes: max_selection = election.max_votes form = forms.get_range_voting_form( candidates=election.candidates, max_range=max_selection) if form.validate_on_submit(): if form.action.data == 'submit': for candidate in form: if candidate.short_name in ['csrf_token', 'action']: continue new_vote = models.Vote( election_id=election.id, voter=flask.g.fas_user.username, timestamp=datetime.now(), candidate_id=candidate.short_name, value=candidate.data, ) SESSION.add(new_vote) SESSION.commit() flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' usernamemap = build_name_map(election) return flask.render_template( 'vote_range.html', election=election, form=form, num_candidates=num_candidates, max_range=max_selection, usernamemap=usernamemap, nextaction=next_action)
def get_valid_election(election_alias, ended=False): """ Return the election if it is valid (not pending, not ended). """ election = models.Election.get(SESSION, alias=election_alias) if not election: flask.flash('The election, %s, does not exist.' % election_alias) return safe_redirect_back() if election.status == 'Pending': flask.flash('Voting has not yet started, sorry.') return safe_redirect_back() elif not ended and election.status in ('Ended', 'Embargoed'): return flask.redirect( flask.url_for('about_election', election_alias=election.alias)) elif ended and election.status == 'In progress': flask.flash( 'Sorry but this election is in progress, and you may not see its ' 'results yet.') return safe_redirect_back() return election
def vote_select(election, revote): votes = models.Vote.of_user_on_election(SESSION, flask.g.fas_user.username, election.id) num_candidates = election.candidates.count() cand_name = {} for candidate in election.candidates: cand_name[candidate.name] = candidate.id next_action = 'confirm' max_selection = num_candidates if election.max_votes: max_selection = election.max_votes form = forms.get_select_voting_form(candidates=election.candidates, max_selection=max_selection) if form.validate_on_submit(): cnt = [ candidate for candidate in form if candidate.data and candidate.short_name not in ['csrf_token', 'action'] ] if len(cnt) > max_selection: flask.flash('Too many candidates submitted', 'error') else: if form.action.data == 'submit': candidates = [ candidate for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] process_vote(candidates, election, votes, revote, cand_name) say_thank_you(election) return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' usernamemap = build_name_map(election) return flask.render_template('vote_simple.html', election=election, form=form, num_candidates=num_candidates, max_selection=max_selection, usernamemap=usernamemap, nextaction=next_action)
def vote_range(election, revote): votes = models.Vote.of_user_on_election( SESSION, flask.g.fas_user.username, election.id) num_candidates = election.candidates.count() cand_ids = [str(cand.id) for cand in election.candidates] next_action = 'confirm' max_selection = num_candidates if election.max_votes: max_selection = election.max_votes form = forms.get_range_voting_form( candidates=election.candidates, max_range=max_selection) if form.validate_on_submit(): if form.action.data == 'submit': candidates = [ candidate for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] process_vote(candidates, election, votes, revote) flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' usernamemap = build_name_map(election) return flask.render_template( 'vote_range.html', election=election, form=form, num_candidates=num_candidates, max_range=max_selection, usernamemap=usernamemap, nextaction=next_action)
def vote_irc(election): votes = models.Vote.of_user_on_election( SESSION, flask.g.fas_user.username, election.id, count=True) cand_name = {} for candidate in election.candidates: cand_name[candidate.name] = candidate.id num_candidates = election.candidates.count() next_action = 'confirm' form = forms.get_irc_voting_form( candidates=election.candidates, fasusers=election.candidates_are_fasusers) if form.validate_on_submit(): if form.action.data == 'submit': for candidate in form: if candidate.short_name in ['csrf_token', 'action']: continue new_vote = models.Vote( election_id=election.id, voter=flask.g.fas_user.username, timestamp=datetime.now(), candidate_id=cand_name[candidate.short_name], value=candidate.data, ) SESSION.add(new_vote) SESSION.commit() flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' return flask.render_template( 'vote_simple.html', election=election, form=form, num_candidates=num_candidates, nextaction=next_action)
def election_results_text(election_alias): election = get_valid_election(election_alias, ended=True) if not isinstance(election, models.Election): # pragma: no cover return election if not (is_authenticated() and (is_admin(flask.g.fas_user) or is_election_admin(flask.g.fas_user, election.id))): flask.flash( "The text results are only available to the admins", "error") return safe_redirect_back() usernamemap = build_name_map(election) stats = models.Vote.get_election_stats(SESSION, election.id) return flask.render_template( 'results_text.html', election=election, usernamemap=usernamemap, stats=stats, )
def election_results_text(election_alias): election = get_valid_election(election_alias, ended=True) if not isinstance(election, models.Election): # pragma: no cover return election if not (is_authenticated() and (is_admin(flask.g.fas_user) or is_election_admin(flask.g.fas_user, election.id))): flask.flash("The text results are only available to the admins", "error") return safe_redirect_back() usernamemap = build_name_map(election) stats = models.Vote.get_election_stats(SESSION, election.id) return flask.render_template( 'results_text.html', election=election, usernamemap=usernamemap, stats=stats, )
def vote_range(election, revote): votes = models.Vote.of_user_on_election(SESSION, flask.g.fas_user.username, election.id) num_candidates = election.candidates.count() next_action = 'confirm' max_selection = num_candidates if election.max_votes: max_selection = election.max_votes form = forms.get_range_voting_form(candidates=election.candidates, max_range=max_selection) if form.validate_on_submit(): if form.action.data == 'submit': candidates = [ candidate for candidate in form if candidate and candidate.short_name not in ['csrf_token', 'action'] ] process_vote(candidates, election, votes, revote) say_thank_you(election) return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' usernamemap = build_name_map(election) return flask.render_template('vote_range.html', election=election, form=form, num_candidates=num_candidates, max_range=max_selection, usernamemap=usernamemap, nextaction=next_action)
def vote_select(election): votes = models.Vote.of_user_on_election( SESSION, flask.g.fas_user.username, election.id, count=True) num_candidates = election.candidates.count() cand_name = {} for candidate in election.candidates: cand_name[candidate.name] = candidate.id next_action = 'confirm' max_selection = num_candidates if election.max_votes: max_selection = election.max_votes form = forms.get_select_voting_form( candidates=election.candidates, max_selection=max_selection) if form.validate_on_submit(): cnt = 0 for candidate in form: if candidate.short_name in ['csrf_token', 'action']: continue if candidate.data: cnt += 1 if cnt > max_selection: flask.flash('Too many candidates submitted', 'error') else: if form.action.data == 'submit': for candidate in form: if candidate.short_name in ['csrf_token', 'action']: continue new_vote = models.Vote( election_id=election.id, voter=flask.g.fas_user.username, timestamp=datetime.now(), candidate_id=cand_name[candidate.short_name], value=int(candidate.data), ) SESSION.add(new_vote) SESSION.commit() flask.flash("Your vote has been recorded. Thank you!") return safe_redirect_back() if form.action.data == 'preview': flask.flash("Please confirm your vote!") next_action = 'vote' usernamemap = build_name_map(election) return flask.render_template( 'vote_simple.html', election=election, form=form, num_candidates=num_candidates, max_selection=max_selection, usernamemap=usernamemap, nextaction=next_action)