Onegov Ballot API

Election Models

class onegov.ballot.models.election.ElectionAssociation(**kwargs)[source]

An association between elections.

This might be used for example to connect different rounds of elections together. Using a separate table for the associations allows to add further attributes to association in a later stage, for example translated names, such as ‘Erster Wahlgang’.

id

Identifies the assiciation.

source_id

The source election ID.

target_id

The target election ID.

source_election

The source election.

target_election

The target election.

class onegov.ballot.models.election.Candidate(**kwargs)[source]

A candidate.

id

the internal id of the candidate

candidate_id

the external id of the candidate

family_name

the family name

first_name

the first name

elected

True if the candidate is elected

election_id

the election this candidate belongs to

list_id

the list this candidate belongs to

party

the party name

results

a candidate contains n results

votes

the total votes

aggregate_results(attribute)[source]

Gets the sum of the given attribute from the results.

static aggregate_results_expression(cls, attribute)[source]

Gets the sum of the given attribute from the results, as SQL expression.

property percentage_by_entity

Returns the percentage of votes by the entity. Includes uncounted entities and entities with no results available.

property percentage_by_district

Returns the percentage of votes aggregated by the distict. Includes uncounted districts and districts with no results available.

class onegov.ballot.models.election.CandidateResult(**kwargs)[source]

The election result of a candidate in a single political entity.

id

identifies the candidate result

election_result_id

the election result this result belongs to

candidate_id

the candidate this result belongs to

class onegov.ballot.models.election.Election(**kwargs)[source]
type

the type of the item, this can be used to create custom polymorphic subclasses of this class. See http://docs.sqlalchemy.org/en/improve_toc/ orm/extensions/declarative/inheritance.html.

id

Identifies the election, may be used in the url

title_translations

all translations of the title

title

the translated title (uses the locale of the request, falls back to the default locale of the app)

shortcode

Shortcode for cantons that use it

date

The date of the election

number_of_mandates

Number of mandates

property allocated_mandates

Number of already allocated mandates/elected candidates.

majority_type = None

Defines the type of majority (e.g. ‘absolute’, ‘relative’)

absolute_majority

Absolute majority

counted

True if all results have been counted.

property progress

Returns a tuple with the first value being the number of counted election results and the second value being the number of total results.

property has_results

Returns True, if the election has any results.

candidates

An election contains n candidates

results

An election contains n results, one for each political entity

eligible_voters

The total eligible voters

received_ballots

The total recceived ballots

accounted_ballots

The total accounted ballots

blank_ballots

The total blank ballots

invalid_ballots

The total invalid ballots

accounted_votes

The total accounted votes

aggregate_results(attribute)[source]

Gets the sum of the given attribute from the results.

static aggregate_results_expression(cls, attribute)[source]

Gets the sum of the given attribute from the results, as SQL expression.

property last_modified

Returns last change of the election, its candidates and any of its results.

property last_result_change

Returns the last change of the results of the election and the candidates.

property elected_candidates

Returns the first and last names of the elected candidates.

may be used to store a link related to this election

tacit = None

may be used to mark an election as a tacit election

expats = None

may be used to indicate that the vote contains expats as seperate resultas (typically with entity_id = 0)

distinct = None

may be used to mark an election as distinct, e.g. a regional election containing only the municipalities of one district of a canton.

property district

Returns the district name, if this is a distinct election. Requires results to be present.

clear_results()[source]

Clears all the results.

export()[source]

Returns all data connected to this election as list with dicts.

This is meant as a base for json/csv/excel exports. The result is therefore a flat list of dictionaries with repeating values to avoid the nesting of values. Each record in the resulting list is a single candidate result for each political entity. Party results are not included in the export (since they are not really connected with the lists).

class onegov.ballot.models.election.ElectionCompound(**kwargs)[source]
id

Identifies the election compound, may be used in the url

title_translations

all translations of the title

title

the translated title (uses the locale of the request, falls back to the default locale of the app)

shortcode

Shortcode for cantons that use it

date

The date of the elections

party_results

An election compound may contains n party results

panachage_results

An election compound may contains n panachage results

property number_of_mandates

The (total) number of mandates.

property allocated_mandates

Number of already allocated mandates/elected candidates.

property counted

True if all elections have been counted.

property progress

Returns a tuple with the first value being the number of counted elections and the second value being the number of total elections.

property has_results

Returns True, if the election compound has any results.

property completed

Returns True, if the all elections are completed.

property last_modified

Returns last change of the elections.

property last_result_change

Returns the last change of the results of the elections.

property elected_candidates

Returns the first and last names of the elected candidates.

may be used to store a link related to this election

show_party_strengths = None

may be used to enable/disable the visibility of party strengths

show_mandate_allocation = None

may be used to enable/disable the visibility of mandate allocation

clear_results()[source]

Clears all own results.

export()[source]

Returns all data connected to this election compound as list with dicts.

This is meant as a base for json/csv/excel exports. The result is therefore a flat list of dictionaries with repeating values to avoid the nesting of values. Each record in the resulting list is a single candidate result for each political entity. Party results are not included in the export (since they are not really connected with the lists).

class onegov.ballot.models.election.ElectionResult(**kwargs)[source]

The election result in a single political entity.

id

identifies the result

election_id

the election this result belongs to

entity_id

entity id (e.g. a BFS number).

name

the name of the entity

district

the district this entity belongs to

counted

True if the result has been counted and no changes will be made anymore. If the result is definite, all the values below must be specified.

eligible_voters

number of eligible voters

received_ballots

number of received ballots

blank_ballots

number of blank ballots

invalid_ballots

number of invalid ballots

blank_votes

number of blank votes

invalid_votes

number of invalid votes

accounted_votes

The number of accounted votes.

list_results

an election result may contain n list results

candidate_results

an election result contains n candidate results

class onegov.ballot.models.election.List(**kwargs)[source]

A list.

id

internal id of the list

list_id

external id of the list

name

name of the list

election_id

the election this result belongs to

connection_id

the list connection id

candidates

a list contains n candidates

results

a list contains n results

panachage_results

a (proporz) list contains votes from other other lists

votes

the total votes

aggregate_results(attribute)[source]

Gets the sum of the given attribute from the results.

static aggregate_results_expression(cls, attribute)[source]

Gets the sum of the given attribute from the results, as SQL expression.

property percentage_by_entity

Returns the percentage of votes by the entity. Includes uncounted entities and entities with no results available.

property percentage_by_district

Returns the percentage of votes aggregated by the distict. Includes uncounted districts and districts with no results available.

class onegov.ballot.models.election.ListConnection(**kwargs)[source]

A list connection.

id

internal id of the list

connection_id

external id of the list

election_id

the election this result belongs to

parent_id

ID of the parent list connection

lists

a list connection contains n lists

children

a list connection contains n sub-connection

property total_votes

Returns the total number of votes.

property total_number_of_mandates

Returns the total number of mandates.

votes

the total votes

number_of_mandates

the total number of mandates

aggregate_results(attribute)[source]

Gets the sum of the given attribute from the results.

static aggregate_results_expression(cls, attribute)[source]

Gets the sum of the given attribute from the results, as SQL expression.

class onegov.ballot.models.election.ListResult(**kwargs)[source]

The election result of a list in a single political entity.

id

identifies the list

election_result_id

the election result this result belongs to

list_id

the list this result belongs to

class onegov.ballot.models.election.PanachageResult(**kwargs)[source]

The votes transferred from one list to another.

id

identifies the result

owner

the owner of this result

target

the target this result belongs to

source

the source this result belongs to

class onegov.ballot.models.election.PartyResult(**kwargs)[source]

The election result of a party in an election for a given year.

id

identifies the party result

owner

the election or election compound this result belongs to

name

name of the party

year

year

color

color code

class onegov.ballot.models.election.ProporzElection(**kwargs)[source]
list_connections

An election contains n list connections

lists

An election contains n lists

party_results

An election may contains n party results

panachage_results

An election may contains n (party) panachage results

property last_modified

Returns last change of the election, its candidates, lists, list connections and any of its results.

property last_result_change

Returns the last change of the results of the election and the candidates.

property has_lists_panachage_data

Checks if there are lists panachage data available.

clear_results()[source]

Clears all the results.

export()[source]

Returns all data connected to this election as list with dicts.

This is meant as a base for json/csv/excel exports. The result is therefore a flat list of dictionaries with repeating values to avoid the nesting of values. Each record in the resulting list is a single candidate result for each political entity. Party results are not included in the export (since they are not really connected with the lists).

Vote Models

class onegov.ballot.models.vote.Ballot(**kwargs)[source]

A ballot contains a single question asked for a vote.

Usually each vote has exactly one ballot, but it’s possible for votes to have multiple ballots.

In the latter case there are usually two options that are mutually exclusive and a third option that acts as a tie breaker between the frist two options.

The type of the ballot indicates this. Standard ballots only contain one question, variant ballots contain multiple questions.

id

identifies the ballot, maybe used in the url

type

the type of the ballot, ‘standard’ for normal votes, ‘counter-proposal’ if there’s an alternative to the standard ballot. And ‘tie-breaker’, which must exist if there’s a counter proposal. The tie breaker is only relevant if both standard and counter proposal are accepted. If that’s the case, the accepted tie breaker selects the standard, the rejected tie breaker selects the counter proposal.

vote_id

identifies the vote this ballot result belongs to

title_translations

all translations of the title

title

the translated title (uses the locale of the request, falls back to the default locale of the app)

results

a ballot contains n results

property results_by_district

Returns the results aggregated by the distict.

counted

True if all results have been counted.

property progress

Returns a tuple with the first value being the number of counted ballot results and the second value being the number of total ballot results.

yeas

the total yeas

nays

the total nays

empty

the total empty votes

invalid

the total invalid votes

eligible_voters

the total eligible voters

aggregate_results(attribute)[source]

Gets the sum of the given attribute from the results.

static aggregate_results_expression(cls, attribute)[source]

Gets the sum of the given attribute from the results, as SQL expression.

clear_results()[source]

Clear all the results.

class onegov.ballot.models.vote.BallotResult(**kwargs)[source]

The result of a specific ballot. Each ballot may have multiple results. Those results may be aggregated or not.

id

identifies the result, may be used in the url

entity_id

The entity id (e.g. BFS number).

name

the name of the entity

district

the district this entity belongs to

counted

True if the result has been counted and no changes will be made anymore. If the result is definite, all the values below must be specified.

yeas

number of yeas, in case of variants, the number of yeas for the first option of the tie breaker

nays

number of nays, in case of variants, the number of nays for the first option of the tie breaker (so a yay for the second option)

empty

number of empty votes

invalid

number of invalid votes

eligible_voters

number of eligible voters

ballot_id

the ballot this result belongs to

class onegov.ballot.models.vote.ComplexVote(**kwargs)[source]

A complex vote with proposal, counter-proposal and tie-breaker.

property yeas_percentage

The percentage of yeas (discounts empty/invalid ballots).

class onegov.ballot.models.vote.Vote(**kwargs)[source]

A vote describes the issue being voted on. For example, “Vote for Net Neutrality” or “Vote for Basic Income”.

type

the type of the item, this can be used to create custom polymorphic subclasses of this class. See http://docs.sqlalchemy.org/en/improve_toc/ orm/extensions/declarative/inheritance.html.

id

identifies the vote, may be used in the url, generated from the title

shortcode

shortcode for cantons that use it

title_translations

all translations of the title

title

the translated title (uses the locale of the request, falls back to the default locale of the app)

date

identifies the date of the vote

ballots

a vote contains n ballots

ballot(ballot_type, create=False)[source]

Returns the given ballot if it exists. Optionally creates the ballot.

property counted

Checks if there are results for all entitites.

property has_results

Returns True, if there are any results.

property yeas_percentage

The percentage of yeas (discounts empty/invalid ballots).

property nays_percentage

The percentage of nays (discounts empty/invalid ballots).

property progress

Returns a tuple with the first value being the number of counted entities and the second value being the number of total entities.

yeas

the total yeas

nays

the total nays

empty

the total empty votes

invalid

the total invalid votes

eligible_voters

the total eligible voters

aggregate_results(attribute)[source]

Gets the sum of the given attribute from the results.

static aggregate_results_expression(cls, attribute)[source]

Gets the sum of the given attribute from the results, as SQL expression.

property last_modified

Returns last change of the vote, its ballots and any of its results.

property last_result_change

Returns the last change of the results of the vote.

may be used to store a link related to this vote

Additional, translatable label for the link

expats = None

may be used to indicate that the vote contains expats as seperate resultas (typically with entity_id = 0)

clear_results()[source]

Clear all the results.

export()[source]

Returns all data connected to this vote as list with dicts.

This is meant as a base for json/csv/excel exports. The result is therefore a flat list of dictionaries with repeating values to avoid the nesting of values. Each record in the resulting list is a single ballot result.

Collections

class onegov.ballot.collections.ElectionCollection(session, page=0, year=None)[source]
get_latest()[source]

Returns the elections with the latest date.

get_years()[source]

Returns a list of years for which there are elections.

by_date(date)[source]

Returns the elections on the given date.

by_year(year=None)[source]

Returns the elections for the current/given year.

by_id(id)[source]

Returns the election by id.

class onegov.ballot.collections.ElectionCompoundCollection(session, page=0, year=None)[source]
get_latest()[source]

Returns the election compounds with the latest date.

get_years()[source]

Returns a list of years for which there are election compounds.

by_date(date)[source]

Returns the election compounds on the given date.

by_year(year=None)[source]

Returns the election compounds for the current/given year.

by_id(id)[source]

Returns the election compound by id.

class onegov.ballot.collections.VoteCollection(session, page=0, year=None)[source]
get_latest()[source]

Returns the votes with the latest date.

get_years()[source]

Returns a list of years for which there are votes.

by_date(date)[source]

Returns the votes on the given date.

by_year(year=None)[source]

Returns the votes for the current/given year.

by_id(id)[source]

Returns the vote by id.