Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(risk-matrix): use plotly #2

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ zope.event==5.0
zope.interface==6.1
zope.schema==7.0.1
zope.sqlalchemy==3.1
numpy==1.26.3
plotly==5.18.0
psycopg2-binary==2.9.9
redis[hiredis]==5.0.3

Expand Down
1 change: 1 addition & 0 deletions src/riskmatrix/static/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ def css(
)
datatable_js = js('datatables_custom.js', depends=[datatable_bootstrap])
xhr_edit_js = js('xhr_edit.js', depends=[datatable_js])
plotly_js = js('plotly.min.js', depends=[datatable_js])
5 changes: 5 additions & 0 deletions src/riskmatrix/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def request_none_generator(event: 'NewRequest') -> None:
request.set_property(lambda r: secrets.token_urlsafe(), 'csp_nonce', reify=True)


def request_none_generator(event: 'NewRequest') -> None:
request = event.request
request.set_property(lambda r: secrets.token_urlsafe(), 'csp_nonce', reify=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to also add a csp_nonce attribute to testing.DummyRequest, this can just be a static string for testing. Some tests currently fail because the attribute doesn't exist.



def includeme(config: 'Configurator') -> None:
config.add_subscriber(csp_header, NewResponse)
config.add_subscriber(request_none_generator, NewRequest)
Expand Down
42 changes: 21 additions & 21 deletions src/riskmatrix/views/risk_assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
if TYPE_CHECKING:
from pyramid.interfaces import IRequest
from sqlalchemy.orm.query import Query
from typing import TypeVar
from typing import TypeVar, Iterator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from typing import TypeVar, Iterator
from collections.abc import Iterator
from typing import TypeVar

Always use the collections.abc imports for these protocols, the typing aliases have been deprecated since 3.9. You could use pyupgrade to auto-correct imports. Although if you're also going to include isort it might be easier to just switch most of the linting over to ruff and only keep flake8 for some of the plugins that aren't included in ruff or not as up-to-date like flake8-type-checking.

ruff also includes auto fixes for a lot of the errors, if you're into that sort of thing.


from riskmatrix.models import Organization
from riskmatrix.types import MixedDataOrRedirect
Expand Down Expand Up @@ -130,25 +130,24 @@ def buttons(self, assessment: RiskAssessment | None = None) -> list[Button]:


class AssessmentOverviewTable(AssessmentBaseTable):
nr = DataColumn(_("Nr."))
name = DataColumn(_("Name"))
description = DataColumn(_("Description"), class_name="visually-hidden")
category = DataColumn(_("Category"))
asset_name = DataColumn(_("Asset"))
likelihood = DataColumn(_("Likelihood"))
impact = DataColumn(_("Impact"))

def __init__(self, org: "Organization", request: "IRequest") -> None:
super().__init__(org, request, id="risks-table")
nr = DataColumn(_('Nr.'))
name = DataColumn(_('Name'))
description = DataColumn(_('Description'), class_name='visually-hidden')
category = DataColumn(_('Category'))
asset_name = DataColumn(_('Asset'))
likelihood = DataColumn(_('Likelihood'))
impact = DataColumn(_('Impact'))

def __init__(self, org: 'Organization', request: 'IRequest') -> None:
super().__init__(org, request, id='risks-table')
xhr_edit_js.need()

def query(self) -> "Query[RiskMatrixAssessment]":
def query(self) -> 'Iterator[RiskMatrixAssessment]':
query = super().query()

return map(
lambda entry: setattr(entry[1], "nr", entry[0] + 1) or entry[1],
enumerate(query),
)
for idx, item in enumerate(query, start=1):
item.nr = idx
yield item


_RADIO_TEMPLATE = Markup(
Expand Down Expand Up @@ -287,7 +286,6 @@ def __html__(self) -> str:
params=params,
)


def generate_risk_matrix_view(
context: "Organization", request: "IRequest"
) -> "RenderData":
Expand Down Expand Up @@ -382,11 +380,13 @@ def plot_risk_matrix(risks: 'Query[RiskMatrixAssessment]') -> str:
font=dict(size=20),
)

return fig.to_html(
return Markup(fig.to_html(
full_html=False,
include_plotlyjs=False,
config={"modeBarButtonsToRemove": ["zoom", "pan", "select", "lasso2d"]},
)
include_plotlyjs=True,
config={
'modeBarButtonsToRemove': ['zoom', 'pan', 'select', 'lasso2d']
},
))


def edit_assessment_view(
Expand Down
Loading