Source code for election_day.screen_widgets.generic
from onegov.election_day import _
from onegov.election_day import ElectionDayApp
from onegov.qrcode import QrCode
from typing import Any
from typing import Generic
from typing import TypeVar
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.election_day.layouts import DefaultLayout
from onegov.election_day.models import Election
from onegov.election_day.models import ElectionCompound
from onegov.election_day.models import Vote
from typing import TypeAlias
@ElectionDayApp.screen_widget(tag='h1', category='generic')
[docs]
class H1Widget:
[docs]
template = """
<xsl:template match="h1">
<h1 class="{@class}">
<xsl:apply-templates select="node()"/>
</h1>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='h2', category='generic')
[docs]
class H2Widget:
[docs]
template = """
<xsl:template match="h2">
<h2 class="{@class}">
<xsl:apply-templates select="node()"/>
</h2>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='h3', category='generic')
[docs]
class H3Widget:
[docs]
template = """
<xsl:template match="h3">
<h3 class="{@class}">
<xsl:apply-templates select="node()"/>
</h3>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='p', category='generic')
[docs]
class PWidget:
[docs]
template = """
<xsl:template match="p">
<p class="{@class}">
<xsl:apply-templates select="node()"/>
</p>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='hr', category='generic')
@ElectionDayApp.screen_widget(tag='grid-row', category='generic')
[docs]
class GridRowWidget:
[docs]
template = """
<xsl:template match="grid-row">
<div class="row {@class}" style="max-width: none">
<xsl:apply-templates select="node()"/>
</div>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='grid-column', category='generic')
[docs]
class GridColumnWidget:
[docs]
template = """
<xsl:template match="grid-column">
<div class="small-12 medium-{@span} columns {@class}">
<xsl:apply-templates select="node()"/>
 
</div>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='principal-logo', category='generic')
[docs]
class PrincipalLogoWidget:
[docs]
template = """
<xsl:template match="principal-logo">
<img
tal:attributes="src logo"
tal:condition="logo"
class="{@class}"
/>
</xsl:template>
"""
[docs]
def get_variables(self, layout: 'DefaultLayout') -> dict[str, Any]:
logo = layout.app.logo
return {'logo': layout.request.link(logo) if logo else ''}
@ElectionDayApp.screen_widget(tag='qr-code', category='generic')
[docs]
class QrCodeWidget:
[docs]
template = """
<xsl:template match="qr-code">
<tal:block tal:define="url '{@url}'; src qr_code(url)">
<img tal:attributes="src src"
class="{@class}"
/>
</tal:block>
</xsl:template>
"""
@staticmethod
[docs]
def qr_code(url: str) -> str:
return 'data:image/png;base64,{}'.format(
QrCode(payload=url, encoding='base64').encoded_image.decode()
)
[docs]
def get_variables(self, layout: 'DefaultLayout') -> dict[str, Any]:
return {'qr_code': self.qr_code}
[docs]
class ModelBoundWidget(Generic[_E]):
def __init__(self, model: _E | None = None) -> None:
[docs]
def get_variables(self, layout: 'DefaultLayout') -> dict[str, Any]:
return {
'model': self.model or layout.model
}
@ElectionDayApp.screen_widget(tag='model-title', category='generic')
[docs]
class ModelTitleWidget(ModelBoundWidget['Entity']):
[docs]
template = """
<xsl:template match="model-title">
<span tal:content="model.title" class="{@class}" />
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='if-completed', category='generic')
[docs]
class IfCompletedWidget(ModelBoundWidget['Entity']):
[docs]
template = """
<xsl:template match="if-completed">
<tal:block tal:condition="model.completed">
<xsl:apply-templates select="node()"/>
</tal:block>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='if-not-completed', category='generic')
[docs]
class IfNotCompletedWidget(ModelBoundWidget['Entity']):
[docs]
template = """
<xsl:template match="if-not-completed">
<tal:block tal:condition="not model.completed">
<xsl:apply-templates select="node()"/>
</tal:block>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='model-progress', category='generic')
[docs]
class ModelProgressWidget(ModelBoundWidget['Entity']):
[docs]
template = """
<xsl:template match="model-progress">
<span class="{@class}">
<tal:block
metal:use-macro="layout.macros['progress']"
tal:define="progress model.progress"
/>
</span>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='counted-entities', category='generic')
[docs]
class CountedEntitiesWidget(ModelBoundWidget['Entity']):
[docs]
template = """
<xsl:template match="counted-entities">
<span class="{@class}">${entities}</span>
</xsl:template>
"""
[docs]
def get_variables(self, layout: 'DefaultLayout') -> dict[str, Any]:
model = self.model or layout.model
entities = ', '.join([
entity or layout.request.translate(_('Expats'))
for entity in model.counted_entities
])
return {
'model': model,
'entities': entities
}
[docs]
class ChartWidget(ModelBoundWidget[_E]):
[docs]
def get_variables(self, layout: 'DefaultLayout') -> dict[str, Any]:
return {
'embed': False,
'model': self.model or layout.model
}
@ElectionDayApp.screen_widget(tag='last-result-change', category='generic')
[docs]
class LastResultChangeWidget(ModelBoundWidget['Entity']):
[docs]
template = """
<xsl:template match="last-result-change">
<span class="{@class}">
${layout.format_date(layout.last_result_change, 'datetime_long')}
</span>
</xsl:template>
"""
@ElectionDayApp.screen_widget(
tag='number-of-counted-entities',
category='generic'
)
[docs]
class NumberOfCountedEntitiesWidget(ModelBoundWidget['Entity']):
[docs]
template = """
<xsl:template match="number-of-counted-entities">
<span class="{@class}">
${layout.format_number(model.progress[0])}
</span>
</xsl:template>
"""
@ElectionDayApp.screen_widget(tag='total-entities', category='generic')