Onegov Election Day API

Application

class onegov.election_day.app.ElectionDayApp[source]

The election day application. Include this in your onegov.yml to serve it with onegov-server.

classmethod manage_html(*args, **kw)

HTML directive for manage views which makes sure the permission is set to private.

classmethod manage_form(*args, **kw)

HTML directive for manage forms which makes sure the permission is set to private. Sets a valid default for the template and form class.

property principal

Returns the principal of the election day app. See onegov.election_day.models.principal.Principal.

load_principal()[source]

The principal is defined in the principal.yml file stored on the applications filestorage root.

If the file does not exist, the site root does not exist and therefore a 404 is returned.

The structure of the yaml file is defined in class:onegov.election_app.model.Principal.

send_sms(receiver=None, content=None)[source]

Sends an SMS by writing a file to the sms_directory of the principal.

SMS sent through this method are bound to the current transaction. If that transaction is aborted or not commited, the SMS is not sent.

Usually you’ll use this method inside a request, where transactions are automatically commited at the end.

Returns the logo as onegov.core.filestorage.FilestorageFile.

property theme_options

Returns the application-bound theme options.

property pages_cache

A five minute cache for pages.

Layouts

class onegov.election_day.layouts.DefaultLayout(model, request)[source]
class onegov.election_day.layouts.ElectionLayout(model, request, tab=None)[source]
pdf_path

Returns the path to the PDF file or None, if it is not available.

svg_path

Returns the path to the SVG or None, if it is not available.

Returns a link to the SVG download view.

svg_name

Returns a nice to read SVG filename.

class onegov.election_day.layouts.ElectionCompoundLayout(model, request, tab=None)[source]
pdf_path

Returns the path to the PDF file or None, if it is not available.

svg_path

Returns the path to the SVG or None, if it is not available.

Returns a link to the SVG download view.

svg_name

Returns a nice to read SVG filename.

class onegov.election_day.layouts.MailLayout(model, request)[source]

A special layout for creating HTML E-Mails.

model_title(model)[source]

Returns the translated title of the given election or vote. Falls back to the title of the default fallback, if no translated title is available.

model_url(model)[source]

Returns the localized link to the given election of vote.

subject(model)[source]

Returns a nice subject for the given model.

Returns the opt-out link of the principal.

class onegov.election_day.layouts.ManageDataSourceItemsLayout(model, request)[source]
class onegov.election_day.layouts.ManageDataSourcesLayout(model, request)[source]
class onegov.election_day.layouts.ManageElectionCompoundsLayout(model, request)[source]
class onegov.election_day.layouts.ManageElectionsLayout(model, request)[source]
class onegov.election_day.layouts.ManageLayout(model, request)[source]
class onegov.election_day.layouts.ManageSubscribersLayout(model, request)[source]
class onegov.election_day.layouts.ManageUploadTokensLayout(model, request)[source]
class onegov.election_day.layouts.ManageVotesLayout(model, request)[source]
class onegov.election_day.layouts.VoteLayout(model, request, tab='entities')[source]
pdf_path

Returns the path to the PDF file or None, if it is not available.

svg_path

Returns the path to the SVG file or None, if it is not available.

Returns a link to the SVG download view.

svg_name

Returns a nice to read SVG filename.

Models

class onegov.election_day.models.archived_result.ArchivedResult(**kwargs)[source]

Stores the result of an election or vote.

id

Identifies the result

date

The date of the election/vote

last_modified

The last change of the results election/vote

last_result_change

The last change of election/vote

type

Type of the result

schema

Origin of the result

name

The name of the principal

total_entities

Total number of political entities

counted_entities

Number of already counted political entitites

url

The link to the detailed results

title_translations

Title of the election

shortcode

Shortcode for cantons that use it

external_id = None

The id of the election/vote.

elected_candidates = None

The names of the elected candidates.

elections = None

The URLs of the elections (if it is a compound)

answer = None

The answer of a vote (accepted, rejected, counter-proposal).

nays_percentage = None

The nays rate of a vote.

yeas_percentage = None

The yeas rate of a vote.

counted = None

True, if the vote or election has been counted.

completed = None

True, if the vote or election has been completed.

local = None

The local results (municipal results if fetched from cantonal instance)

local_answer = None

The answer if this a fetched cantonal/federal result on a communal instance.

local_nays_percentage = None

The nays rate if this a fetched cantonal/federal result on a communal instance.

local_yeas_percentage = None

The yeas rate if this a fetched cantonal/federal result on a communal instance.

is_fetched(request)[source]

Returns True, if this results has been fetched from another instance.

is_fetched_by_municipality(request)[source]

Returns True, if this results has been fetched from another instance by a communal instance.

display_answer(request)[source]

Returns the answer (depending on the current instance).

display_nays_percentage(request)[source]

Returns the nays rate (depending on the current instance).

display_yeas_percentage(request)[source]

Returns the yeas rate (depending on the current instance).

class onegov.election_day.models.notification.Notification(**kwargs)[source]

Stores triggered notifications.

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 notification

last_modified

The last update of the corresponding election/vote

election_id

The corresponding election id

election

The corresponding election

vote_id

The corresponding vote id

vote

The corresponding vote

update_from_model(model)[source]

Copy

trigger(request, model)[source]

Trigger the custom actions.

class onegov.election_day.models.notification.WebhookNotification(**kwargs)[source]
trigger(request, model)[source]

Posts the summary of the given vote or election to the webhook URL defined for this principal.

This only works for external URLs. If posting to the server itself is needed, use a process instead of the thread:

process = Process(target=send_post_request, args=(urls, data)) process.start()

class onegov.election_day.models.notification.EmailNotification(**kwargs)[source]
set_locale(request, locale=None)[source]

Changes the locale of the request.

(Re)stores the intial locale if no locale is given.

send_emails(request, elections, votes, subject=None)[source]

Sends the results of the vote or election to all subscribers.

Adds unsubscribe headers (RFC 2369, RFC 8058).

trigger(request, model)[source]

Sends the results of the vote or election to all subscribers.

Adds unsubscribe headers (RFC 2369, RFC 8058).

class onegov.election_day.models.notification.SmsNotification(**kwargs)[source]
send_sms(request, content)[source]

Sends the given text to all subscribers.

trigger(request, model)[source]

Posts a link to the vote or election to all subscribers.

This is done by writing files to a directory similary to maildir, sending the SMS is done using an external command, probably called by a cronjob.

class onegov.election_day.models.principal.Principal(id_=None, domain=None, domains_election=None, domains_vote=None, entities=None, name=None, logo=None, color='#000', base=None, analytics=None, has_districts=True, use_maps=False, fetch=None, webhooks=None, sms_notification=None, email_notification=None, wabsti_import=False, pdf_signing=None, open_data=None, hidden_elements=None, **kwargs)[source]

The principal is the political entity running the election day app.

There are currently two different types of principals supported: Cantons and Municipalitites.

A principal is identitifed by its ID (municipalitites: BFS number, cantons: canton code).

A principal may consist of different entitites (municipalitites: quarters, cantons: municipalities) grouped by districts. The label of the entity and districts may vary and can be queried with label.

A principal is part of a domain (municipalitites: municipality, canton: canton) and supports different types of domains (typically all higher domains).

class onegov.election_day.models.principal.Canton(canton=None, **kwargs)[source]

A cantonal instance.

class onegov.election_day.models.principal.Municipality(municipality=None, **kwargs)[source]

A communal instance.

Collections

class onegov.election_day.collections.archived_results.SearchableArchivedResultCollection(session, date_=None, from_date=None, to_date=None, types=None, item_type=None, domains=None, term=None, answers=None, locale='de_CH', page=0)[source]
subset()[source]

Returns an SQLAlchemy query containing all records that should be considered for pagination.

property page_index

Returns the current page index (starting at 0).

page_by_index(index)[source]

Returns the page at the given index. A page here means an instance of the class inheriting from the Pagination base class.

group_items(items, request)[source]

Groups a list of archived results.

Groups election compounds and elections to the same group. Removes elections already covered by an election compound.

static term_to_tsquery_string(term)[source]

Returns the current search term transformed to use within Postgres to_tsquery function. Removes all unwanted characters, replaces prefix matching, joins word together using FOLLOWED BY.

static match_term(column, language, term)[source]

Usage: model.filter(match_term(model.col, ‘german’, ‘my search term’))

static filter_text_by_locale(column, term, locale=None)[source]

Returns an SqlAlchemy filter statement based on the search term. If no locale is provided, it will use english as language. to_tsquery creates a tsquery value from term, which must consist of

single tokens separated by the Boolean operators

& (AND), | (OR) and ! (NOT). to_tsvector parses a textual document into tokens, reduces the tokens to lexemes, and returns a tsvector which lists the lexemes together with their positions in the document. The document is processed according to the specified or default text search configuration.

class onegov.election_day.collections.subscribers.SubscriberCollectionPagination(session, page=0, term=None)[source]
subset()[source]

Returns an SQLAlchemy query containing all records that should be considered for pagination.

property page_index

Returns the current page index (starting at 0).

page_by_index(index)[source]

Returns the page at the given index. A page here means an instance of the class inheriting from the Pagination base class.

class onegov.election_day.collections.subscribers.SubscriberCollection(session, page=0, term=None)[source]
by_id(id)[source]

Returns the subscriber by its id.

subscribe(address, request, confirm=True)[source]

Subscribe with the given address and locale.

Existing subscriptions with the given address will be updated according to the new locale.

confirm_subscription(subscriber, request)[source]

Give the (new) subscriber a confirmation that he successfully subscribed.

unsubscribe(address)[source]

Unsubscribe with the given address.

class onegov.election_day.collections.subscribers.EmailSubscriberCollection(session, page=0, term=None)[source]
confirm_subscription(subscriber, request)[source]

Give the (new) subscriber a confirmation that he successfully subscribed.

class onegov.election_day.collections.subscribers.SmsSubscriberCollection(session, page=0, term=None)[source]
confirm_subscription(subscriber, request)[source]

Give the (new) subscriber a confirmation that he successfully subscribed.

Paths

Formats

onegov.election_day.formats.import_election_internal_majorz(election, principal, file, mimetype)[source]

Tries to import the given file (internal format).

This is the format used by onegov.ballot.Election.export().

This function is typically called automatically every few minutes during an election day - we use bulk inserts to speed up the import.

Returns

A list containing errors.

onegov.election_day.formats.import_election_internal_proporz(election, principal, file, mimetype)[source]

Tries to import the given file (internal format).

This is the format used by onegov.ballot.Election.export().

This function is typically called automatically every few minutes during an election day - we use bulk inserts to speed up the import.

Returns

A list containing errors.

onegov.election_day.formats.import_election_wabsti_majorz(election, principal, file, mimetype, elected_file=None, elected_mimetype=None)[source]

Tries to import the given csv, xls or xlsx file.

This is the format used by Wabsti for majorz elections. Since there is no format description, importing these files is somewhat experimental.

Returns

A list containing errors.

onegov.election_day.formats.import_election_wabsti_proporz(election, principal, file, mimetype, connections_file=None, connections_mimetype=None, elected_file=None, elected_mimetype=None, statistics_file=None, statistics_mimetype=None)[source]

Tries to import the given csv, xls or xlsx file.

This is the format used by Wabsti for proporz elections. Since there is no format description, importing these files is somewhat experimental.

Returns

A list containing errors.

onegov.election_day.formats.import_election_wabstic_majorz(election, principal, number, district, file_wm_wahl, mimetype_wm_wahl, file_wmstatic_gemeinden, mimetype_wmstatic_gemeinden, file_wm_gemeinden, mimetype_wm_gemeinden, file_wm_kandidaten, mimetype_wm_kandidaten, file_wm_kandidatengde, mimetype_wm_kandidatengde)[source]

Tries to import the given CSV files from a WabstiCExport.

This function is typically called automatically every few minutes during an election day - we use bulk inserts to speed up the import.

Returns

A list containing errors.

onegov.election_day.formats.import_election_wabstic_proporz(election=None, principal=None, number=None, district=None, file_wp_wahl=None, mimetype_wp_wahl=None, file_wpstatic_gemeinden=None, mimetype_wpstatic_gemeinden=None, file_wp_gemeinden=None, mimetype_wp_gemeinden=None, file_wp_listen=None, mimetype_wp_listen=None, file_wp_listengde=None, mimetype_wp_listengde=None, file_wpstatic_kandidaten=None, mimetype_wpstatic_kandidaten=None, file_wp_kandidaten=None, mimetype_wp_kandidaten=None, file_wp_kandidatengde=None, mimetype_wp_kandidatengde=None)[source]

Tries to import the given CSV files from a WabstiCExport.

This function is typically called automatically every few minutes during an election day - we use bulk inserts to speed up the import.

Returns

A list containing errors.

onegov.election_day.formats.import_party_results(election, file, mimetype)[source]

Tries to import the given file.

This is our own format used for party results. Supports per party panachage data. Stores the panachage results from the blank list with a blank name.

Returns

A list containing errors.

onegov.election_day.formats.import_vote_default(vote, principal, ballot_type, file, mimetype)[source]

Tries to import the given csv, xls or xlsx file to the given ballot result type.

This is a custom format defined by us to easily create vote results by hand.

Returns

A list containing errors.

onegov.election_day.formats.import_vote_internal(vote, principal, file, mimetype)[source]

Tries to import the given csv, xls or xlsx file.

This is the format used by onegov.ballot.Vote.export().

This function is typically called automatically every few minutes during an election day - we use bulk inserts to speed up the import.

Returns

A list containing errors.

onegov.election_day.formats.import_vote_wabsti(vote, principal, vote_number, file, mimetype)[source]

Tries to import the given csv, xls or xlsx file.

This is the format used by Wabsti. Since there is no format description, importing these files is somewhat experimental.

Returns

A list containing errors.

onegov.election_day.formats.import_vote_wabstic(vote, principal, number, district, file_sg_geschaefte, mimetype_sg_geschaefte, file_sg_gemeinden, mimetype_sg_gemeinden)[source]

Tries to import the given CSV files from a WabstiCExport.

This function is typically called automatically every few minutes during an election day - we use bulk inserts to speed up the import.

Returns

A list containing errors.

onegov.election_day.formats.import_vote_wabstim(vote, principal, file, mimetype)[source]

Tries to import the given csv, xls or xlsx file.

This is the format used by Wabsti for municipalities. Since there is no format description, importing these files is somewhat experimental.

Returns

A list containing errors.

Views

Forms

class onegov.election_day.forms.election.ElectionForm(*args, **kwargs)[source]
class onegov.election_day.forms.upload.UploadMajorzElectionForm(*args, **kwargs)[source]
adjust(principal, election)[source]

Adjusts the form to the given principal and election.

class onegov.election_day.forms.upload.UploadProporzElectionForm(*args, **kwargs)[source]
class onegov.election_day.forms.upload.UploadPartyResultsForm(*args, **kwargs)[source]
class onegov.election_day.forms.upload.UploadRestForm(*args, **kwargs)[source]
class onegov.election_day.forms.upload.UploadVoteForm(*args, **kwargs)[source]
adjust(principal, vote)[source]

Adjusts the form to the given principal and vote.

class onegov.election_day.forms.upload.UploadWabstiMajorzElectionForm(*args, **kwargs)[source]
class onegov.election_day.forms.upload.UploadWabstiProporzElectionForm(*args, **kwargs)[source]
class onegov.election_day.forms.upload.UploadWabstiVoteForm(*args, **kwargs)[source]
class onegov.election_day.forms.vote.VoteForm(*args, **kwargs)[source]

Utils

onegov.election_day.utils.add_cors_header(response)[source]

Adds a header allowing the response being used in scripts.

onegov.election_day.utils.add_last_modified_header(response, last_modified)[source]

Adds the give date to the response as Last-Modified header.

onegov.election_day.utils.add_local_results(source, target, principal, session)[source]

Adds the result of the principal.

Municipalities are interested in their own result rather than the cantonal end result of votes. We query the result of the municipality within the given vote (source) and add it to the target.

onegov.election_day.utils.pdf_filename(item, locale, last_modified=None)[source]

Generates a filename from an election or vote:

[‘election’ or ‘vote’]-[hash of id].[timestamp].[locale].pdf

onegov.election_day.utils.svg_filename(item, type_, locale=None, last_modified=None)[source]

Generates a filename from an election, ballot or vote:

[‘election’ or ‘vote’]-[hash of id].[type_].[timestamp].[locale].svg

onegov.election_day.utils.get_election_summary(election, request, url=None)[source]

Returns some basic informations about the given election as a JSON seriazable dict.

onegov.election_day.utils.get_summaries(items, request)[source]

Converts the given list of election/votes to a JSON seriazable list of summaries.

onegov.election_day.utils.get_summary(item, request)[source]

Returns some basic informations about the given election/vote as a JSON seriazable dict.

onegov.election_day.utils.get_vote_summary(vote, request, url=None)[source]

Returns some basic informations about the given vote as a JSON seriazable dict.