Skip to content

Commit

Permalink
Treats steps like a subnavigation of "Risk Assessment".
Browse files Browse the repository at this point in the history
I.e. the navigation entry remains active for all the steps.
  • Loading branch information
Daverball committed Sep 28, 2023
1 parent 308f159 commit 87695b6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/riskmatrix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from riskmatrix.flash import MessageQueue
from riskmatrix.i18n import LocaleNegotiator
from riskmatrix.layouts.steps import show_steps
from riskmatrix.route_factories import root_factory
from riskmatrix.security import authenticated_user
from riskmatrix.security_policy import SessionSecurityPolicy
Expand Down Expand Up @@ -45,6 +46,7 @@ def includeme(config: Configurator) -> None:

config.add_request_method(authenticated_user, 'user', property=True)
config.add_request_method(MessageQueue, 'messages', reify=True)
config.add_request_method(show_steps, 'show_steps', reify=True)


def main(
Expand Down
9 changes: 1 addition & 8 deletions src/riskmatrix/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import date
from typing import Any, TYPE_CHECKING

from riskmatrix.cache import request_cache
from riskmatrix.static import bootstrap_css
from riskmatrix.static import bootstrap_js

Expand All @@ -25,14 +24,8 @@ def __init__(self, context: Any, request: 'IRequest') -> None:
bootstrap_css.need()
bootstrap_js.need()

@request_cache()
def show_steps(self) -> bool:
route = self.request.matched_route
return hasattr(route, 'name') and route.name in (
'assessment',
'assess_impact',
'assess_likelihood',
)
return self.request.show_steps

def locale_name(self) -> str:
return self.request.locale_name
Expand Down
18 changes: 15 additions & 3 deletions src/riskmatrix/layouts/navbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable
from pyramid.interfaces import IRequest

from riskmatrix.types import RenderData
Expand All @@ -17,10 +18,20 @@ class NavbarEntry:
url: str
active: bool

def __init__(self, request: 'IRequest', title: str, url: str):
def __init__(
self,
request: 'IRequest',
title: str,
url: str,
active: 'Callable[[IRequest, str], bool] | None' = None
):
self.title = title
self.url = url
self.active = request.path_url == url

if active is None:
self.active = request.path_url == url
else:
self.active = active(request, url)

def __call__(self) -> str:
item_class = 'nav-item'
Expand Down Expand Up @@ -70,7 +81,8 @@ def navbar(context: object, request: 'IRequest') -> 'RenderData':
NavbarEntry(
request,
_('Risk Assessment'),
request.route_url('assessment')
request.route_url('assessment'),
lambda request, url: request.show_steps,
),
]
}
20 changes: 17 additions & 3 deletions src/riskmatrix/layouts/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def steps(context: 'Organization', request: 'IRequest') -> 'RenderData':
'steps': [
Step(
_('Identify Risks'),
request.route_url('assessment', id=context.id)
request.route_url('assessment')
),
Step(
_('Assess Impact'),
request.route_url('assess_impact', id=context.id)
request.route_url('assess_impact')
),
Step(
_('Assess Likelihood'),
request.route_url('assess_likelihood', id=context.id)
request.route_url('assess_likelihood')
),
Step(
_('Generate Risk Matrix'),
Expand All @@ -41,3 +41,17 @@ def steps(context: 'Organization', request: 'IRequest') -> 'RenderData':
),
]
}


def show_steps(request: 'IRequest') -> bool:
# FIXME: For increased robustness we probably should store the
# steps in shared configuration and generate this condition
# from the defined steps
route = request.matched_route
if hasattr(route, 'name'):
return route.name in (
'assessment',
'assess_impact',
'assess_likelihood'
)
return False
2 changes: 2 additions & 0 deletions stubs/pyramid/interfaces.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class IRequest(Interface, _Request):
user: User

messages: MessageQueue
show_steps: bool

response: IResponse
layout_manager: ILayoutManager

Expand Down

0 comments on commit 87695b6

Please sign in to comment.