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

[docs] Entity: TypeAlias = Election | ElectionCompound | Vote
[docs] _E = TypeVar('_E', bound='Entity')
@ElectionDayApp.screen_widget(tag='h1', category='generic')
[docs] class H1Widget:
[docs] tag = 'h1'
[docs] template = """ <xsl:template match="h1"> <h1 class="{@class}"> <xsl:apply-templates select="node()"/> </h1> </xsl:template> """
[docs] usage = '<h1 class=""></h1>'
@ElectionDayApp.screen_widget(tag='h2', category='generic')
[docs] class H2Widget:
[docs] tag = 'h2'
[docs] template = """ <xsl:template match="h2"> <h2 class="{@class}"> <xsl:apply-templates select="node()"/> </h2> </xsl:template> """
[docs] usage = '<h2 class=""></h2>'
@ElectionDayApp.screen_widget(tag='h3', category='generic')
[docs] class H3Widget:
[docs] tag = 'h3'
[docs] template = """ <xsl:template match="h3"> <h3 class="{@class}"> <xsl:apply-templates select="node()"/> </h3> </xsl:template> """
[docs] usage = '<h3 class=""></h3>'
@ElectionDayApp.screen_widget(tag='p', category='generic')
[docs] class PWidget:
[docs] tag = 'p'
[docs] template = """ <xsl:template match="p"> <p class="{@class}"> <xsl:apply-templates select="node()"/> </p> </xsl:template> """
[docs] usage = '<p class=""></p>'
@ElectionDayApp.screen_widget(tag='hr', category='generic')
[docs] class HRWidget:
[docs] tag = 'hr'
[docs] template = """ <xsl:template match="hr"> <hr class="{@class}" /> </xsl:template> """
[docs] usage = '<hr class=""/>'
@ElectionDayApp.screen_widget(tag='grid-row', category='generic')
[docs] class GridRowWidget:
[docs] tag = 'grid-row'
[docs] template = """ <xsl:template match="grid-row"> <div class="row {@class}" style="max-width: none"> <xsl:apply-templates select="node()"/> </div> </xsl:template> """
[docs] usage = '<grid-row class=""></grid-row>'
@ElectionDayApp.screen_widget(tag='grid-column', category='generic')
[docs] class GridColumnWidget:
[docs] tag = 'grid-column'
[docs] template = """ <xsl:template match="grid-column"> <div class="small-12 medium-{@span} columns {@class}"> <xsl:apply-templates select="node()"/> &#160; </div> </xsl:template> """
[docs] usage = '<grid-column span="" class=""></grid-column>'
@ElectionDayApp.screen_widget(tag='principal-logo', category='generic')
[docs] class PrincipalLogoWidget:
[docs] tag = 'principal-logo'
[docs] template = """ <xsl:template match="principal-logo"> <img tal:attributes="src logo" tal:condition="logo" class="{@class}" /> </xsl:template> """
[docs] usage = '<principal-logo class=""/>'
[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] tag = 'qr-code'
[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> """
[docs] usage = '<qr-code class="" url="https://"/>'
@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] self.model = model
[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] tag = 'model-title'
[docs] template = """ <xsl:template match="model-title"> <span tal:content="model.title" class="{@class}" /> </xsl:template> """
[docs] usage = '<model-title class=""/>'
@ElectionDayApp.screen_widget(tag='if-completed', category='generic')
[docs] class IfCompletedWidget(ModelBoundWidget['Entity']):
[docs] tag = 'if-completed'
[docs] template = """ <xsl:template match="if-completed"> <tal:block tal:condition="model.completed"> <xsl:apply-templates select="node()"/> </tal:block> </xsl:template> """
[docs] usage = '<if-completed></if-completed>'
@ElectionDayApp.screen_widget(tag='if-not-completed', category='generic')
[docs] class IfNotCompletedWidget(ModelBoundWidget['Entity']):
[docs] tag = 'if-not-completed'
[docs] template = """ <xsl:template match="if-not-completed"> <tal:block tal:condition="not model.completed"> <xsl:apply-templates select="node()"/> </tal:block> </xsl:template> """
[docs] usage = '<if-not-completed></if-not-completed>'
@ElectionDayApp.screen_widget(tag='model-progress', category='generic')
[docs] class ModelProgressWidget(ModelBoundWidget['Entity']):
[docs] tag = 'model-progress'
[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> """
[docs] usage = '<model-progress class=""/>'
@ElectionDayApp.screen_widget(tag='counted-entities', category='generic')
[docs] class CountedEntitiesWidget(ModelBoundWidget['Entity']):
[docs] tag = 'counted-entities'
[docs] template = """ <xsl:template match="counted-entities"> <span class="{@class}">${entities}</span> </xsl:template> """
[docs] usage = '<counted-entities class=""/>'
[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] tag = 'last-result-change'
[docs] template = """ <xsl:template match="last-result-change"> <span class="{@class}"> ${layout.format_date(layout.last_result_change, 'datetime_long')} </span> </xsl:template> """
[docs] usage = '<last-result-change class=""/>'
@ElectionDayApp.screen_widget( tag='number-of-counted-entities', category='generic' )
[docs] class NumberOfCountedEntitiesWidget(ModelBoundWidget['Entity']):
[docs] tag = 'number-of-counted-entities'
[docs] template = """ <xsl:template match="number-of-counted-entities"> <span class="{@class}"> ${layout.format_number(model.progress[0])} </span> </xsl:template> """
[docs] usage = '<number-of-counted-entities class=""/>'
@ElectionDayApp.screen_widget(tag='total-entities', category='generic')
[docs] class TotalEntitiesWidget(ModelBoundWidget['Entity']):
[docs] tag = 'total-entities'
[docs] template = """ <xsl:template match="total-entities"> <span class="{@class}"> ${layout.format_number(model.progress[1])} </span> </xsl:template> """
[docs] usage = '<total-entities class=""/>'