election_day.models.election.election

Classes

VotesByDistrictRow

Election

Base class used for declarative class definitions.

Module Contents

class election_day.models.election.election.VotesByDistrictRow[source]

Bases: NamedTuple

election_id: str[source]
district: str[source]
entities: list[int][source]
counted: bool[source]
votes: int[source]
class election_day.models.election.election.Election[source]

Bases: onegov.core.orm.Base, onegov.core.orm.mixins.ContentMixin, onegov.election_day.models.mixins.LastModifiedMixin, onegov.election_day.models.mixins.DomainOfInfluenceMixin, onegov.election_day.models.mixins.StatusMixin, onegov.election_day.models.mixins.TitleTranslationsMixin, onegov.election_day.models.mixins.IdFromTitlesMixin, onegov.election_day.models.election.mixins.DerivedAttributesMixin, onegov.election_day.models.mixins.ExplanationsPdfMixin, onegov.election_day.models.party_result.mixins.PartyResultsOptionsMixin

Base class used for declarative class definitions.

The _orm.DeclarativeBase allows for the creation of new declarative bases in such a way that is compatible with type checkers:

from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass

The above Base class is now usable as the base for new declarative mappings. The superclass makes use of the __init_subclass__() method to set up new classes and metaclasses aren’t used.

When first used, the _orm.DeclarativeBase class instantiates a new _orm.registry to be used with the base, assuming one was not provided explicitly. The _orm.DeclarativeBase class supports class-level attributes which act as parameters for the construction of this registry; such as to indicate a specific _schema.MetaData collection as well as a specific value for :paramref:`_orm.registry.type_annotation_map`:

from typing_extensions import Annotated

from sqlalchemy import BigInteger
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy.orm import DeclarativeBase

bigint = Annotated[int, "bigint"]
my_metadata = MetaData()


class Base(DeclarativeBase):
    metadata = my_metadata
    type_annotation_map = {
        str: String().with_variant(String(255), "mysql", "mariadb"),
        bigint: BigInteger(),
    }

Class-level attributes which may be specified include:

Parameters:
  • metadata – optional _schema.MetaData collection. If a _orm.registry is constructed automatically, this _schema.MetaData collection will be used to construct it. Otherwise, the local _schema.MetaData collection will supersede that used by an existing _orm.registry passed using the :paramref:`_orm.DeclarativeBase.registry` parameter.

  • type_annotation_map – optional type annotation map that will be passed to the _orm.registry as :paramref:`_orm.registry.type_annotation_map`.

  • registry – supply a pre-existing _orm.registry directly.

Added in version 2.0: Added DeclarativeBase, so that declarative base classes may be constructed in such a way that is also recognized by PEP 484 type checkers. As a result, DeclarativeBase and other subclassing-oriented APIs should be seen as superseding previous “class returned by a function” APIs, namely _orm.declarative_base() and _orm.registry.generate_base(), where the base class returned cannot be recognized by type checkers without using plugins.

__init__ behavior

In a plain Python class, the base-most __init__() method in the class hierarchy is object.__init__(), which accepts no arguments. However, when the _orm.DeclarativeBase subclass is first declared, the class is given an __init__() method that links to the :paramref:`_orm.registry.constructor` constructor function, if no __init__() method is already present; this is the usual declarative constructor that will assign keyword arguments as attributes on the instance, assuming those attributes are established at the class level (i.e. are mapped, or are linked to a descriptor). This constructor is never accessed by a mapped class without being called explicitly via super(), as mapped classes are themselves given an __init__() method directly which calls :paramref:`_orm.registry.constructor`, so in the default case works independently of what the base-most __init__() method does.

Changed in version 2.0.1: _orm.DeclarativeBase has a default constructor that links to :paramref:`_orm.registry.constructor` by default, so that calls to super().__init__() can access this constructor. Previously, due to an implementation mistake, this default constructor was missing, and calling super().__init__() would invoke object.__init__().

The _orm.DeclarativeBase subclass may also declare an explicit __init__() method which will replace the use of the :paramref:`_orm.registry.constructor` function at this level:

class Base(DeclarativeBase):
    def __init__(self, id=None):
        self.id = id

Mapped classes still will not invoke this constructor implicitly; it remains only accessible by calling super().__init__():

class MyClass(Base):
    def __init__(self, id=None, name=None):
        self.name = name
        super().__init__(id=id)

Note that this is a different behavior from what functions like the legacy _orm.declarative_base() would do; the base created by those functions would always install :paramref:`_orm.registry.constructor` for __init__().

__tablename__ = 'elections'[source]
property polymorphic_base: type[Election][source]
type: sqlalchemy.orm.Mapped[str][source]
__mapper_args__[source]
id: sqlalchemy.orm.Mapped[str][source]
external_id: sqlalchemy.orm.Mapped[str | None][source]
title_translations: sqlalchemy.orm.Mapped[collections.abc.Mapping[str, str]][source]
title[source]
short_title_translations: sqlalchemy.orm.Mapped[collections.abc.Mapping[str, str] | None][source]
short_title[source]
title_observer(title_translations: collections.abc.Mapping[str, str], short_title_translations: collections.abc.Mapping[str, str]) None[source]
shortcode: sqlalchemy.orm.Mapped[str | None][source]
date: sqlalchemy.orm.Mapped[datetime.date][source]
number_of_mandates: sqlalchemy.orm.Mapped[int][source]
property allocated_mandates: int[source]

Number of already allocated mandates/elected candidates.

majority_type: onegov.core.orm.mixins.dict_property[str | None][source]
absolute_majority: sqlalchemy.orm.Mapped[int | None][source]
counted() bool[source]

True if all results have been counted.

classmethod _counted_expression() sqlalchemy.sql.ColumnElement[bool][source]
property progress: tuple[int, int][source]

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 counted_entities: list[str][source]

Returns the names of the already counted entities.

Might contain an empty string in case of expats.

property has_results: bool[source]

Returns True, if the election has any results.

candidates: sqlalchemy.orm.Mapped[list[onegov.election_day.models.election.candidate.Candidate]][source]
results: sqlalchemy.orm.Mapped[list[onegov.election_day.models.election.election_result.ElectionResult]][source]
property results_query: sqlalchemy.orm.Query[onegov.election_day.models.election.election_result.ElectionResult][source]
related_elections: sqlalchemy.orm.DynamicMapped[onegov.election_day.models.ElectionRelationship][source]
referencing_elections: sqlalchemy.orm.DynamicMapped[onegov.election_day.models.ElectionRelationship][source]
election_compound_id: sqlalchemy.orm.Mapped[str | None][source]
election_compound: sqlalchemy.orm.Mapped[onegov.election_day.models.ElectionCompound | None][source]
property completed: bool[source]

Overwrites StatusMixin’s ‘completed’ for compounds with manual completion.

eligible_voters[source]
expats[source]
received_ballots[source]
accounted_ballots[source]

The number of accounted ballots.

blank_ballots[source]
invalid_ballots[source]
accounted_votes[source]
aggregate_results(attribute: str) int[source]

Gets the sum of the given attribute from the results.

classmethod aggregate_results_expression(attribute: str) sqlalchemy.sql.ColumnElement[int][source]

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

property elected_candidates: list[tuple[str, str]][source]

Returns the first and last names of the elected candidates.

tacit: onegov.core.orm.mixins.dict_property[bool][source]
has_expats: onegov.core.orm.mixins.dict_property[bool][source]
domain_segment: onegov.core.orm.mixins.dict_property[str][source]
domain_supersegment: onegov.core.orm.mixins.dict_property[str][source]
property votes_by_district: sqlalchemy.orm.Query[VotesByDistrictRow][source]
colors: onegov.core.orm.mixins.dict_property[dict[str, str]][source]
clear_results(clear_all: bool = False) None[source]

Clears all the results.

data_sources: sqlalchemy.orm.Mapped[list[onegov.election_day.models.DataSourceItem]][source]
notifications: sqlalchemy.orm.DynamicMapped[onegov.election_day.models.Notification][source]
screens: sqlalchemy.orm.DynamicMapped[onegov.election_day.models.Screen][source]