Skip to content

Commit

Permalink
Election Day: Allow application to be private.
Browse files Browse the repository at this point in the history
TYPE: Feature
LINK: OGC-1678
  • Loading branch information
msom authored Jun 14, 2024
1 parent ecc9325 commit f070906
Show file tree
Hide file tree
Showing 41 changed files with 436 additions and 213 deletions.
2 changes: 2 additions & 0 deletions src/onegov/election_day/models/principal.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(
custom_css: str | None = None,
official_host: str | None = None,
segmented_notifications: bool = False,
private: bool = False,
**kwargs: 'Never'
):
assert all((id_, domain, domains_election, domains_vote, entities))
Expand Down Expand Up @@ -137,6 +138,7 @@ def __init__(
self.custom_css = custom_css
self.official_host = official_host
self.segmented_notifications = segmented_notifications
self.private = private

@classmethod
def from_yaml(cls, yaml_source: '_ReadStream') -> 'Canton | Municipality':
Expand Down
51 changes: 51 additions & 0 deletions src/onegov/election_day/security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

from onegov.core.security import Private
from onegov.core.security import Public
from onegov.core.security.permissions import Intent
from onegov.core.security.rules import has_permission_logged_in
from onegov.core.security.rules import has_permission_not_logged_in
from onegov.election_day import ElectionDayApp

from typing import TYPE_CHECKING
if TYPE_CHECKING:
from morepath.authentication import Identity


class MaybePublic(Intent):
""" The general public is allowed to do this, if not otherwise defined
in the principal. """


@ElectionDayApp.permission_rule(
model=object,
permission=MaybePublic,
identity=None
)
def has_permission_maybe_public_not_logged_in(
app: ElectionDayApp,
identity: None,
model: object,
permission: object
) -> bool:

if getattr(app.principal, 'private', False):
return has_permission_not_logged_in(app, identity, model, Private)

return has_permission_not_logged_in(app, identity, model, Public)


@ElectionDayApp.permission_rule(
model=object,
permission=MaybePublic
)
def has_permission_maybe_public_logged_in(
app: ElectionDayApp,
identity: 'Identity',
model: object,
permission: MaybePublic
) -> bool:

if getattr(app.principal, 'private', False):
return has_permission_logged_in(app, identity, model, Private)

return has_permission_logged_in(app, identity, model, Public)
19 changes: 10 additions & 9 deletions src/onegov/election_day/views/archive.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from onegov.core.security import Public
from fs.errors import ResourceNotFound
from morepath.request import Response
from onegov.election_day import ElectionDayApp
from onegov.election_day.collections import ArchivedResultCollection
from onegov.election_day.collections import SearchableArchivedResultCollection
Expand All @@ -7,12 +8,11 @@
from onegov.election_day.layouts import DefaultLayout
from onegov.election_day.layouts.archive import ArchiveLayout
from onegov.election_day.models import Principal
from onegov.election_day.security import MaybePublic
from onegov.election_day.utils import add_cors_header
from onegov.election_day.utils import add_last_modified_header
from onegov.election_day.utils import get_summaries
from webob.exc import HTTPNotFound
from fs.errors import ResourceNotFound
from morepath.request import Response


from typing import TYPE_CHECKING
Expand All @@ -25,7 +25,7 @@
@ElectionDayApp.html(
model=ArchivedResultCollection,
template='archive.pt',
permission=Public
permission=MaybePublic
)
def view_archive(
self: ArchivedResultCollection,
Expand All @@ -49,7 +49,7 @@ def view_archive(
@ElectionDayApp.json(
model=ArchivedResultCollection,
name='json',
permission=Public
permission=MaybePublic
)
def view_archive_json(
self: ArchivedResultCollection,
Expand Down Expand Up @@ -81,7 +81,7 @@ def add_headers(response: Response) -> None:
@ElectionDayApp.html(
model=Principal,
template='archive.pt',
permission=Public
permission=MaybePublic
)
def view_principal(
self: Principal,
Expand All @@ -106,7 +106,7 @@ def view_principal(
@ElectionDayApp.json(
model=Principal,
name='json',
permission=Public
permission=MaybePublic
)
def view_principal_json(
self: Principal,
Expand Down Expand Up @@ -151,7 +151,7 @@ def search_form(
model=SearchableArchivedResultCollection,
template='archive_search.pt',
form=search_form,
permission=Public,
permission=MaybePublic,
)
def view_archive_search(
self: SearchableArchivedResultCollection,
Expand Down Expand Up @@ -182,7 +182,8 @@ def view_archive_search(
@ElectionDayApp.view(
model=Principal,
name='archive-download',
permission=Public)
permission=MaybePublic
)
def view_archive_download(
self: Principal,
request: 'ElectionDayRequest'
Expand Down
8 changes: 4 additions & 4 deletions src/onegov/election_day/views/election/candidate_district.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from onegov.core.security import Public
from onegov.election_day import _
from onegov.election_day import ElectionDayApp
from onegov.election_day.hidden_by_principal import \
hide_candidate_district_map_percentages
from onegov.election_day.layouts import ElectionLayout
from onegov.election_day.models import Candidate
from onegov.election_day.models import Election
from onegov.election_day.security import MaybePublic
from onegov.election_day.utils import add_last_modified_header


Expand Down Expand Up @@ -54,7 +54,7 @@ def ordering_completed(candidate: Candidate) -> tuple[bool, str, str]:
@ElectionDayApp.json(
model=Candidate,
name='by-district',
permission=Public
permission=MaybePublic
)
def view_candidate_by_district(
self: Candidate,
Expand All @@ -69,7 +69,7 @@ def view_candidate_by_district(
model=Election,
name='candidate-by-district',
template='election/heatmap.pt',
permission=Public
permission=MaybePublic
)
def view_election_candidate_by_district(
self: Election,
Expand Down Expand Up @@ -110,7 +110,7 @@ def view_election_candidate_by_district(
model=Election,
name='candidate-by-district-chart',
template='embed.pt',
permission=Public
permission=MaybePublic
)
def view_election_candidate_by_district_chart(
self: Election,
Expand Down
8 changes: 4 additions & 4 deletions src/onegov/election_day/views/election/candidate_entity.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from onegov.core.security import Public
from onegov.election_day import _
from onegov.election_day import ElectionDayApp
from onegov.election_day.hidden_by_principal import \
hide_candidate_entity_map_percentages
from onegov.election_day.layouts import ElectionLayout
from onegov.election_day.models import Candidate
from onegov.election_day.models import Election
from onegov.election_day.security import MaybePublic
from onegov.election_day.utils import add_last_modified_header


Expand Down Expand Up @@ -53,7 +53,7 @@ def ordering_completed(candidate: Candidate) -> tuple[bool, str, str]:
@ElectionDayApp.json(
model=Candidate,
name='by-entity',
permission=Public
permission=MaybePublic
)
def view_candidate_by_entity(
self: Candidate,
Expand All @@ -68,7 +68,7 @@ def view_candidate_by_entity(
model=Election,
name='candidate-by-entity',
template='election/heatmap.pt',
permission=Public
permission=MaybePublic
)
def view_election_candidate_by_entity(
self: Election,
Expand Down Expand Up @@ -109,7 +109,7 @@ def view_election_candidate_by_entity(
model=Election,
name='candidate-by-entity-chart',
template='embed.pt',
permission=Public
permission=MaybePublic
)
def view_election_candidate_by_entity_chart(
self: Election,
Expand Down
16 changes: 10 additions & 6 deletions src/onegov/election_day/views/election/candidates.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from onegov.core.security import Public
from onegov.election_day import _
from onegov.election_day import ElectionDayApp
from onegov.election_day.hidden_by_principal import hide_candidates_chart
from onegov.election_day.layouts import ElectionLayout
from onegov.election_day.models import Election
from onegov.election_day.security import MaybePublic
from onegov.election_day.utils import add_last_modified_header
from onegov.election_day.utils import get_entity_filter
from onegov.election_day.utils import get_parameter
Expand All @@ -29,7 +29,7 @@
@ElectionDayApp.json(
model=Election,
name='candidates-data',
permission=Public
permission=MaybePublic
)
def view_election_candidates_data(
self: Election,
Expand Down Expand Up @@ -62,7 +62,7 @@ def view_election_candidates_data(
model=Election,
name='candidates-chart',
template='embed.pt',
permission=Public
permission=MaybePublic
)
def view_election_candidates_chart(
self: Election,
Expand Down Expand Up @@ -90,7 +90,7 @@ def add_last_modified(response: 'Response') -> None:
model=Election,
name='candidates',
template='election/candidates.pt',
permission=Public
permission=MaybePublic
)
def view_election_candidates(
self: Election,
Expand Down Expand Up @@ -124,7 +124,7 @@ def view_election_candidates(
model=Election,
name='candidates-table',
template='embed.pt',
permission=Public
permission=MaybePublic
)
def view_election_lists_table(
self: Election,
Expand Down Expand Up @@ -153,7 +153,11 @@ def add_last_modified(response: 'Response') -> None:
}


@ElectionDayApp.svg_file(model=Election, name='candidates-svg')
@ElectionDayApp.svg_file(
model=Election,
name='candidates-svg',
permission=MaybePublic
)
def view_election_candidates_svg(
self: Election,
request: 'ElectionDayRequest'
Expand Down
16 changes: 10 additions & 6 deletions src/onegov/election_day/views/election/connections.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from onegov.core.security import Public
from onegov.election_day import ElectionDayApp
from onegov.election_day.hidden_by_principal import (
hide_connections_chart)
from onegov.election_day.layouts import ElectionLayout
from onegov.election_day.models import Election
from onegov.election_day.security import MaybePublic
from onegov.election_day.utils import add_last_modified_header
from onegov.election_day.utils.election import get_connection_results_api
from onegov.election_day.utils.election import get_connections_data
Expand All @@ -28,7 +28,7 @@
@ElectionDayApp.json(
model=Election,
name='connections-data',
permission=Public
permission=MaybePublic
)
def view_election_connections_data(
self: Election,
Expand All @@ -46,7 +46,7 @@ def view_election_connections_data(
model=Election,
name='connections-chart',
template='embed.pt',
permission=Public
permission=MaybePublic
)
def view_election_connections_chart(
self: Election,
Expand All @@ -73,7 +73,7 @@ def add_last_modified(response: 'Response') -> None:
model=Election,
name='connections-table',
template='embed.pt',
permission=Public
permission=MaybePublic
)
def view_election_connections_table(
self: Election,
Expand All @@ -98,7 +98,7 @@ def add_last_modified(response: 'Response') -> None:
model=Election,
name='connections',
template='election/connections.pt',
permission=Public
permission=MaybePublic
)
def view_election_connections(
self: Election,
Expand All @@ -116,7 +116,11 @@ def view_election_connections(
}


@ElectionDayApp.svg_file(model=Election, name='connections-svg')
@ElectionDayApp.svg_file(
model=Election,
name='connections-svg',
permission=MaybePublic
)
def view_election_connections_svg(
self: Election,
request: 'ElectionDayRequest'
Expand Down
Loading

0 comments on commit f070906

Please sign in to comment.