예제 #1
0
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)
예제 #2
0
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,
    )
예제 #3
0
def about_election(election_alias):
    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()

    usernamemap = build_name_map(election)

    return flask.render_template(
        'about.html',
        election=election,
        usernamemap=usernamemap)
예제 #4
0
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)
예제 #5
0
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)
예제 #6
0
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)
예제 #7
0
def about_election(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    stats = []
    evolution_label = []
    evolution_data = []
    if not election:
        flask.flash('The election, %s,  does not exist.' % election_alias)
        return safe_redirect_back()
    elif election.status in ['Embargoed', 'Ended']:

        stats = models.Vote.get_election_stats(SESSION, election.id)
        cnt = 1
        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

    usernamemap = build_name_map(election)

    voted = []
    if is_authenticated():
        votes = models.Vote.of_user_on_election(SESSION,
                                                flask.g.fas_user.username,
                                                election.id,
                                                count=True)
        if votes > 0:
            voted.append(election)

    return flask.render_template(
        'about.html',
        election=election,
        usernamemap=usernamemap,
        stats=stats,
        voted=voted,
        evolution_label=evolution_label,
        evolution_data=evolution_data,
        candidates=sorted(election.candidates,
                          key=lambda x: x.vote_count,
                          reverse=True),
    )
예제 #8
0
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,
    )
예제 #9
0
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,
    )
예제 #10
0
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)
예제 #11
0
def about_election(election_alias):
    election = models.Election.get(SESSION, alias=election_alias)
    stats = []
    evolution_label = []
    evolution_data = []
    if not election:
        flask.flash('The election, %s,  does not exist.' % election_alias)
        return safe_redirect_back()
    elif election.status in ['Embargoed', 'Ended']:

        stats = models.Vote.get_election_stats(SESSION, election.id)
        cnt = 1
        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

    usernamemap = build_name_map(election)

    voted = []
    if is_authenticated():
        votes = models.Vote.of_user_on_election(
            SESSION, flask.g.fas_user.username, election.id, count=True)
        if votes > 0:
            voted.append(election)

    return flask.render_template(
        'about.html',
        election=election,
        usernamemap=usernamemap,
        stats=stats,
        voted=voted,
        evolution_label=evolution_label,
        evolution_data=evolution_data)
예제 #12
0
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)