Skip to content

Commit

Permalink
Election Day: Add type annotation for public vote json.
Browse files Browse the repository at this point in the history
TYPE: Feature
LINK: OGC-1588
  • Loading branch information
msom authored Apr 19, 2024
1 parent c84dd22 commit 7fc0c07
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 234 deletions.
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ install_requires =
polib
psycopg2
purl
pydantic
pycurl<7.45.3
pyparsing
pyquery
Expand Down
2 changes: 0 additions & 2 deletions src/onegov/election_day/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from onegov.core.framework import transaction_tween_factory
from onegov.election_day.directives import CsvFileAction
from onegov.election_day.directives import JsonFileAction
from onegov.election_day.directives import JsonSchemaAction
from onegov.election_day.directives import ManageFormAction
from onegov.election_day.directives import ManageHtmlAction
from onegov.election_day.directives import PdfFileViewAction
Expand Down Expand Up @@ -51,7 +50,6 @@ class ElectionDayApp(Framework, FormApp, UserApp, DepotApp):

csv_file = directive(CsvFileAction)
json_file = directive(JsonFileAction)
json_schema = directive(JsonSchemaAction)
manage_form = directive(ManageFormAction)
manage_html = directive(ManageHtmlAction)
pdf_file = directive(PdfFileViewAction)
Expand Down
37 changes: 0 additions & 37 deletions src/onegov/election_day/directives.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dectate import Action
from morepath.directive import HtmlAction
from morepath.directive import JsonAction
from morepath.directive import ViewAction
from morepath.request import Response
from onegov.core.csv import convert_list_of_dicts_to_csv
Expand Down Expand Up @@ -158,16 +157,6 @@ def render_csv(content: dict[str, Any], request: 'CoreRequest') -> Response:
)


def render_json_schema(
content: dict[str, Any],
request: 'CoreRequest'
) -> Response:
return Response(
json_body=request.app._dump_json(content, request), # type:ignore
content_type="application/schema+json",
)


class SvgFileViewAction(ViewAction):

""" View directive for viewing SVG files from filestorage. The SVGs
Expand Down Expand Up @@ -266,32 +255,6 @@ def __init__(
)


class JsonSchemaAction(JsonAction):

""" Directive to return JSON schema. """

def __init__(
self,
model: type | str,
render: 'Callable[[Any, _RequestT], BaseResponse] | str | None' = None,
template: 'StrOrBytesPath | None' = None,
load: 'Callable[[_RequestT], Any] | str | None' = None,
permission: object | str | None = None,
internal: bool = False,
**predicates: Any
) -> None:

super().__init__(
model,
render_json_schema,
template,
load,
permission,
internal,
**predicates
)


class ScreenWidgetRegistry(dict[str, dict[str, 'ScreenWidget']]):

def by_categories(
Expand Down
82 changes: 0 additions & 82 deletions src/onegov/election_day/schemas.py

This file was deleted.

71 changes: 71 additions & 0 deletions src/onegov/election_day/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Contains types used in the public API
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from onegov.ballot.types import BallotType
from onegov.ballot.types import DomainOfInfluence
from onegov.core.types import JSONObject
from typing import Literal
from typing import TypedDict

class ProgressJson(TypedDict):
counted: int
total: int

class TitleJson(TypedDict, total=False):
de_CH: str
fr_CH: str
it_CH: str
rm_CH: str

class VoteResultsJson(TypedDict):
answer: str | None
nays_percentage: float | None
yeas_percentage: float | None

class BallotTotalResultJson(TypedDict):
counted: bool
accepted: bool
eligible_voters: int
invalid: int
cast_ballots: int
turnout: float
empty: int
yeas: int
nays: int
yeas_percentage: float
nays_percentage: float

class BallotEntityResultJson(BallotTotalResultJson):
id: int
name: str
district: str

class BallotResultJson(TypedDict):
total: BallotTotalResultJson
entities: list[BallotEntityResultJson]

class BallotJson(TypedDict):
type: BallotType
title: TitleJson
progress: ProgressJson
results: BallotResultJson

class DataJson(TypedDict):
json: str
csv: str

class VoteJson(TypedDict):
completed: bool
date: str
domain: DomainOfInfluence
last_modified: str
progress: ProgressJson
related_link: str | None
title: TitleJson
type: Literal['vote']
results: VoteResultsJson
ballots: list[BallotJson]
url: str
embed: JSONObject # deprecated
media: JSONObject # deprecated
data: DataJson
Loading

0 comments on commit 7fc0c07

Please sign in to comment.