Source code for election_day.screen_widgets.election
from __future__ import annotations
from onegov.election_day import ElectionDayApp
from onegov.election_day.screen_widgets.generic import ChartWidget
from onegov.election_day.screen_widgets.generic import ModelBoundWidget
from onegov.election_day.utils.election import get_candidates_results
from onegov.election_day.utils.election import get_candidates_results_by_entity
from onegov.election_day.utils.election import get_list_results
from onegov.election_day.utils.parties import get_party_results
from onegov.election_day.utils.parties import get_party_results_deltas
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from onegov.election_day.layouts import DefaultLayout
    from onegov.election_day.models import Election  # noqa: F401
    from onegov.election_day.models import ProporzElection  # noqa: F401
@ElectionDayApp.screen_widget(
    tag='election-candidates-table',
    category='election'
)
[docs]
class ElectionCandidatesTableWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="election-candidates-table">
            <div class="{@class}" tal:define="lists '{@lists}'">
                <tal:block
                    metal:use-macro="layout.macros['election-candidates-table']"
                    tal:define="show_percentage (election.type != 'proporz')"
                    />
            </div>
        </xsl:template>
    """
[docs]
    def get_variables(self, layout: DefaultLayout) -> dict[str, Any]:
        model = self.model or layout.model
        session = layout.request.session
        candidates = get_candidates_results(model, session).all()
        return {
            'election': model,
            'candidates': candidates
        }
@ElectionDayApp.screen_widget(
    tag='election-candidates-by-entity-table',
    category='majorz_election'
)
[docs]
class ElectionCandidatesByEntityTableWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="election-candidates-by-entity-table">
            <div class="{@class}">
                <tal:block
                    metal:use-macro="layout.macros['election-candidates-by-entity-table']"
                    />
            </div>
        </xsl:template>
    """
[docs]
    def get_variables(self, layout: DefaultLayout) -> dict[str, Any]:
        model = self.model or layout.model
        candidates_by_entites = get_candidates_results_by_entity(
            model, sort_by_votes=True
        )
        return {
            'election': model,
            'candidates_by_entites': candidates_by_entites,
        }
@ElectionDayApp.screen_widget(
    tag='election-lists-table',
    category='proporz_election'
)
[docs]
class ElectionListsTableWidget(ModelBoundWidget['ProporzElection']):
[docs]
    template = """
        <xsl:template match="election-lists-table">
            <div class="{@class}" tal:define="names '{@names}'">
                <tal:block
                    metal:use-macro="layout.macros['election-lists-table']"
                    />
            </div>
        </xsl:template>
    """
[docs]
    def get_variables(self, layout: DefaultLayout) -> dict[str, Any]:
        model = self.model or layout.model
        lists = get_list_results(model).all()
        return {
            'election': model,
            'lists': lists
        }
@ElectionDayApp.screen_widget(
    tag='election-party-strengths-table',
    category='proporz_election'
)
[docs]
class ElectionPartyStrengthsTableWidget(ModelBoundWidget['ProporzElection']):
[docs]
    template = """
        <xsl:template match="election-party-strengths-table">
            <div class="{@class}" tal:define="year '{@year}'">
                <tal:block
                    metal:use-macro="layout.macros['party-strengths-table']"
                    />
            </div>
        </xsl:template>
    """
[docs]
    def get_variables(self, layout: DefaultLayout) -> dict[str, Any]:
        model = self.model or layout.model
        party_years, parties = get_party_results(model)
        party_deltas, party_results = get_party_results_deltas(
            model, party_years, parties
        )
        return {
            'election': model,
            'party_years': party_years,
            'party_deltas': party_deltas,
            'party_results': party_results
        }
@ElectionDayApp.screen_widget(
    tag='election-candidates-chart',
    category='election'
)
[docs]
class ElectionCandidatesChartWidget(ChartWidget['Election']):
[docs]
    template = """
        <xsl:template match="election-candidates-chart">
            <div class="{@class}"
                 tal:define="limit '0{@limit}';
                             lists '{@lists}';
                             elected '{@elected}';
                             sort_by_lists '{@sort-by-lists}';
                             ">
                <tal:block
                    metal:use-macro="layout.macros['candidates-chart']"
                    />
            </div>
        </xsl:template>
    """
[docs]
    usage = (
        '<election-candidates-chart limit="" lists="," sort-by-lists=""'
        ' elected="" class=""/>'
    )
@ElectionDayApp.screen_widget(
    tag='election-lists-chart',
    category='proporz_election'
)
[docs]
class ElectionListsChartWidget(ChartWidget['ProporzElection']):
[docs]
    template = """
        <xsl:template match="election-lists-chart">
            <div class="{@class}"
                 tal:define="limit '0{@limit}';
                             names '{@names}';
                             sort_by_names '{@sort-by-names}'
                             ">
                <tal:block
                    metal:use-macro="layout.macros['lists-chart']"
                    />
            </div>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='election-party-strengths-chart',
    category='proporz_election'
)
[docs]
class ElectionPartyStrengthsChartWidget(ChartWidget['ProporzElection']):
[docs]
    template = """
        <xsl:template match="election-party-strengths-chart">
            <div class="{@class}"
                 tal:define="horizontal '{@horizontal}'=='true'">
                <tal:block
                    metal:use-macro="layout.macros['party-strengths-chart']"
                    />
            </div>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='allocated-mandates',
    category='election'
)
[docs]
class AllocatedMandatesWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="allocated-mandates">
            <span class="{@class}">
                ${layout.format_number(model.allocated_mandates)}
            </span>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='number-of-mandates',
    category='election'
)
[docs]
class NumberOfMandatesWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="number-of-mandates">
            <span class="{@class}">
                ${layout.format_number(model.number_of_mandates)}
            </span>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='mandates',
    category='election'
)
[docs]
class MandatesWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="mandates">
            <span class="{@class}">
                <tal:block
                    metal:use-macro="layout.macros['progress']"
                    tal:define="progress (model.allocated_mandates,\
                                            model.number_of_mandates)"
                    />
            </span>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='election-turnout',
    category='election'
)
[docs]
class ElectionTurnoutWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="election-turnout">
            <span class="{@class}">
                ${'{0:.2f}'.format(model.turnout)} %
            </span>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='absolute-majority',
    category='majorz_election'
)
[docs]
class AbsoluteMajorityWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="absolute-majority">
            <span class="{@class}">
                ${layout.format_number(model.absolute_majority or 0)}
            </span>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='if-absolute-majority',
    category='majorz_election'
)
[docs]
class IfAbsoluteMajorityWidget(ModelBoundWidget['Election']):
[docs]
    template = """
        <xsl:template match="if-absolute-majority">
            <tal:block tal:condition="model.majority_type == 'absolute'">
                <xsl:apply-templates select="node()"/>
            </tal:block>
        </xsl:template>
    """
@ElectionDayApp.screen_widget(
    tag='if-relative-majority',
    category='majorz_election'
)