from onegov.core.collection import Pagination
from onegov.election_day.models import Screen
from typing import Any
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.orm import Query
from sqlalchemy.orm import Session
from typing import Self
[docs]
class ScreenCollection(Pagination[Screen]):
def __init__(self, session: 'Session', page: int = 0):
super().__init__(page)
[docs]
def __eq__(self, other: object) -> bool:
return isinstance(other, self.__class__) and self.page == other.page
[docs]
def subset(self) -> 'Query[Screen]':
return self.query()
@property
[docs]
def page_index(self) -> int:
return self.page
[docs]
def page_by_index(self, index: int) -> 'Self':
return self.__class__(self.session, index)
[docs]
def query(self) -> 'Query[Screen]':
return self.session.query(Screen).order_by(Screen.number)
[docs]
def by_id(self, id: int) -> Screen | None:
return self.query().filter(Screen.id == id).first()
[docs]
def by_number(self, number: int) -> Screen | None:
return self.query().filter(Screen.number == number).first()
[docs]
def add(self, screen: Screen) -> None:
self.session.add(screen)
self.session.flush()
[docs]
def delete(self, screen: Screen) -> None:
self.session.delete(screen)
self.session.flush()
[docs]
def export(self) -> list[dict[str, Any]]:
return [
{
'number': screen.number,
'description': screen.description,
'type': screen.type,
'structure': screen.structure,
'css': screen.css,
'group': screen.group,
'duration': screen.duration,
}
for screen in self.query()
]