from functools import cached_property
from onegov.org.layout import DefaultLayout
from onegov.core.elements import Link, LinkGroup, Intercooler, Confirm, Block
from onegov.winterthur import _
from onegov.winterthur.collections import AddressCollection
from onegov.winterthur.collections import MissionReportCollection
from onegov.winterthur.collections import MissionReportFileCollection
from onegov.winterthur.collections import MissionReportVehicleCollection
from onegov.winterthur.models import MissionReport
from onegov.winterthur.models import MissionReportVehicle
from onegov.winterthur.roadwork import RoadworkCollection
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterator
from onegov.winterthur.request import WinterthurRequest
from typing import TypeAlias
[docs]
MissionReportContext: TypeAlias = (
MissionReport | MissionReportVehicle
| MissionReportCollection | MissionReportVehicleCollection
| MissionReportFileCollection
)
[docs]
class AddressLayout(DefaultLayout):
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(_('Addresses'), '#'),
]
@cached_property
[docs]
def editbar_links(self) -> list[Link | LinkGroup] | None:
if not self.request.is_manager:
return None
return [
Link(
text=_('Update'),
url=self.csrf_protected_url(
self.request.link(self.model, '+update')
),
attrs={'class': 'sync'},
traits=Intercooler(
request_method='POST',
redirect_after=self.request.url
)
)
]
[docs]
class AddressSubsetLayout(DefaultLayout):
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(_('Addresses'), self.request.class_link(AddressCollection)),
Link(_(self.model.street), '#')
]
[docs]
class RoadworkCollectionLayout(DefaultLayout):
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(_('Roadworks'), '#'),
]
[docs]
class RoadworkLayout(DefaultLayout):
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(_('Roadworks'), self.request.class_link(RoadworkCollection)),
Link(self.model.title, self.request.link(self.model))
]
[docs]
class DaycareSubsidyCalculatorLayout(DefaultLayout):
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return [
Link(_('Homepage'), self.homepage_url),
Link(
_('Daycare Subsidy Calculator'),
self.request.link(self.model)
)
]
[docs]
class MissionReportLayout(DefaultLayout):
[docs]
model: 'MissionReportContext'
def __init__(
self,
model: 'MissionReportContext',
request: 'WinterthurRequest',
*suffixes: Link
) -> None:
[docs]
self.suffixes = suffixes
super().__init__(model, request)
request.include('iframe-enhancements')
[docs]
def breadcrumbs_iter(self) -> 'Iterator[Link]':
yield Link(
_('Homepage'),
self.homepage_url)
yield Link(
_('Mission Reports'),
self.request.class_link(MissionReportCollection))
yield from self.suffixes
@cached_property
[docs]
def breadcrumbs(self) -> list[Link]:
return list(self.breadcrumbs_iter())
@cached_property
[docs]
def image_upload_url(self) -> str:
if not hasattr(self.model, 'report'):
return super().image_upload_url
url = self.request.link(
MissionReportFileCollection(
self.request.session, self.model.report), name='upload')
return self.csrf_protected_url(url)
@cached_property
[docs]
def image_upload_json_url(self) -> str:
if not hasattr(self.model, 'report'):
return super().image_upload_json_url
url = self.request.link(
MissionReportFileCollection(
self.request.session, self.model.report), name='upload.json')
return self.csrf_protected_url(url)
@cached_property
[docs]
def editbar_links(self) -> list[Link | LinkGroup] | None:
# this is a bit different than usual, trying out some things as part
# of this project - probably not a good idea to copy this elsewhere
if not self.request.is_manager:
return None
if self.suffixes and not getattr(self.suffixes[-1], 'editbar', True):
return None
if isinstance(self.model, MissionReportCollection):
return [
Link(
_('Vehicles'), self.request.class_link(
MissionReportVehicleCollection
), attrs={'class': 'vehicles'}
),
LinkGroup(
title=_('Add'),
links=[
Link(
text=_('Mission Report'),
url=self.request.link(
self.model,
name='+new'
),
attrs={'class': 'new-report'}
)
]
),
]
if isinstance(self.model, MissionReportVehicleCollection):
return [
LinkGroup(
title=_('Add'),
links=[
Link(
text=_('Vehicle'),
url=self.request.link(
self.model,
name='+new'
),
attrs={'class': 'new-vehicle'}
)
]
),
]
if isinstance(self.model, MissionReport):
return [
Link(
_('Images'),
self.request.link(self.model, name='images'),
attrs={'class': 'upload-images'}
),
Link(
_('Edit'),
self.request.link(self.model, name='edit'),
attrs={'class': 'edit-link'}
),
Link(
_('Delete'),
self.csrf_protected_url(
self.request.link(self.model)
),
attrs={'class': 'delete-link'},
traits=(
Confirm(
_(
'Do you really want to delete '
'this mission report?'
),
_('This cannot be undone.'),
_('Delete mission report'),
_('Cancel')
),
Intercooler(
request_method='DELETE',
redirect_after=self.request.class_link(
MissionReportCollection
)
)
)
)
]
if isinstance(self.model, MissionReportVehicle):
if self.model.uses:
return [
Link(
_('Delete'),
'#',
attrs={'class': 'delete-link'},
traits=(
Block(
_("This vehicle can't be deleted."),
_(
'There are mission reports associated '
'with this vehicle.'
),
_('Cancel')
),
)
)
]
else:
return [
Link(
_('Delete'),
self.csrf_protected_url(
self.request.link(self.model)
),
attrs={'class': 'delete-link'},
traits=(
Confirm(
_(
'Do you really want to delete '
'this vehicle?'
),
_('This cannot be undone.'),
_('Delete vehicle'),
_('Cancel')
),
Intercooler(
request_method='DELETE',
redirect_after=self.request.class_link(
MissionReportVehicleCollection
)
)
)
)
]
return None