Skip to content

Commit

Permalink
Make AI Act profile editable and use a new version of the beslishulp
Browse files Browse the repository at this point in the history
  • Loading branch information
uittenbroekrobbert committed Feb 27, 2025
1 parent cdac1c0 commit 6782e6e
Show file tree
Hide file tree
Showing 29 changed files with 445 additions and 292 deletions.
11 changes: 11 additions & 0 deletions amt/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ def hasattr_jinja(obj: object, attributes: str) -> bool:
return True


def equal_or_includes(my_value: str, check_against_value: str | list[str] | tuple[str]) -> bool:
"""Test if my_value equals or exists in check_against_value"""
if isinstance(check_against_value, list | tuple):
return my_value in check_against_value
elif isinstance(check_against_value, str):
return my_value == check_against_value
return False


templates = LocaleJinja2Templates(
directory="amt/site/templates/", context_processors=[custom_context_processor], undefined=get_undefined_behaviour()
)
Expand All @@ -172,5 +181,7 @@ def hasattr_jinja(obj: object, attributes: str) -> bool:
templates.env.globals.update(is_parent_editable=is_parent_editable) # pyright: ignore [reportUnknownMemberType]
templates.env.globals.update(resolve_resource_list_path=resolve_resource_list_path) # pyright: ignore [reportUnknownMemberType]
templates.env.globals.update(get_localized_value=get_localized_value) # pyright: ignore [reportUnknownMemberType]
# env tests allows for usage in templates like: if value is test_name(other_value)
templates.env.tests["permission"] = permission # pyright: ignore [reportUnknownMemberType]
templates.env.tests["equal_or_includes"] = equal_or_includes # pyright: ignore [reportUnknownMemberType]
templates.env.add_extension("jinja2_base64_filters.Base64Filters") # pyright: ignore [reportUnknownMemberType]
53 changes: 50 additions & 3 deletions amt/api/editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
replace_wildcard_with_digits_in_brackets,
)
from amt.api.editable_validators import EditableValidatorMinMaxLength, EditableValidatorSlug
from amt.api.editable_value_providers import AIActValuesProvider
from amt.api.lifecycles import get_localized_lifecycles
from amt.api.routes.shared import nested_value
from amt.api.utils import SafeDict
Expand Down Expand Up @@ -270,6 +271,48 @@ class Editables:
validator=EditableValidatorSlug(),
)

ALGORITHM_EDITABLE_AIACT = Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile",
implementation_type=WebFormFieldImplementationType.PARENT,
children=[
Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile/role",
implementation_type=WebFormFieldImplementationType.MULTIPLE_CHECKBOX_AI_ACT,
values_provider=AIActValuesProvider(type="role"),
),
Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile/type",
implementation_type=WebFormFieldImplementationType.SELECT_AI_ACT,
values_provider=AIActValuesProvider(type="type"),
),
Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile/open_source",
implementation_type=WebFormFieldImplementationType.SELECT_AI_ACT,
values_provider=AIActValuesProvider(type="open_source"),
),
Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile/risk_group",
implementation_type=WebFormFieldImplementationType.SELECT_AI_ACT,
values_provider=AIActValuesProvider(type="risk_group"),
),
Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile/conformity_assessment_body",
implementation_type=WebFormFieldImplementationType.SELECT_AI_ACT,
values_provider=AIActValuesProvider(type="conformity_assessment_body"),
),
Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile/systemic_risk",
implementation_type=WebFormFieldImplementationType.SELECT_AI_ACT,
values_provider=AIActValuesProvider(type="systemic_risk"),
),
Editable(
full_resource_path="algorithm/{algorithm_id}/system_card/ai_act_profile/transparency_obligations",
implementation_type=WebFormFieldImplementationType.SELECT_AI_ACT,
values_provider=AIActValuesProvider(type="transparency_obligations"),
),
],
)

# TODO: rethink if this is a wise solution.. we do this to keep all elements in 1 class and still
# be able to execute other code (like making relationships)
def __iter__(self) -> typing.Generator[tuple[str, Any], Any, Any]:
Expand Down Expand Up @@ -375,8 +418,13 @@ async def enrich_editable( # noqa: C901
)

# TODO: can we move this to the editable object instead of here?
# TODO: consider if values_providers could solve & replace the specific conditions below
if edit_mode == EditModes.EDIT:
if editable.implementation_type == WebFormFieldImplementationType.SELECT_MY_ORGANIZATIONS:
if editable.values_provider:
if request is None:
raise ValueError("Request is required when resolving a 'editable values provider'")
editable.form_options = await editable.values_provider.get_values(request)
elif editable.implementation_type == WebFormFieldImplementationType.SELECT_MY_ORGANIZATIONS:
if organizations_service is None:
raise ValueError("Organization service is required when resolving an organization")
my_organizations = await organizations_service.get_organizations_for_user(user_id=user_id)
Expand Down Expand Up @@ -427,6 +475,7 @@ def resolve_editable_path(
full_resource_path=full_resource_path,
relative_resource_path=relative_resource_path,
implementation_type=editable.implementation_type,
values_provider=editable.values_provider,
couples=couples,
children=children,
converter=editable.converter,
Expand Down Expand Up @@ -556,8 +605,6 @@ def is_parent_editable(editables: dict[str, ResolvedEditable], full_resource_pat
full_resource_path = replace_digits_in_brackets(full_resource_path)
editable = editables.get(full_resource_path)
if editable is None:
print(full_resource_path + " : " + "false, no match")
return False
result = editable.implementation_type == WebFormFieldImplementationType.PARENT
print(full_resource_path + " : " + str(result))
return result
5 changes: 5 additions & 0 deletions amt/api/editable_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
from amt.api.editable_enforcers import EditableEnforcer
from amt.api.editable_validators import EditableValidator
from amt.api.editable_value_providers import EditableValuesProvider
from amt.models.base import Base
from amt.schema.webform import WebFormFieldImplementationTypeFields, WebFormOption

Expand Down Expand Up @@ -35,6 +36,7 @@ def __init__(
self,
full_resource_path: str,
implementation_type: WebFormFieldImplementationTypeFields,
values_provider: EditableValuesProvider | None = None,
couples: set[EditableType] | None = None,
children: list[EditableType] | None = None,
converter: EditableConverter | None = None,
Expand All @@ -45,6 +47,7 @@ def __init__(
) -> None:
self.full_resource_path = full_resource_path
self.implementation_type = implementation_type
self.values_provider = values_provider
self.couples = set[EditableType]() if couples is None else couples
self.children = list[EditableType]() if children is None else children
self.converter = converter
Expand Down Expand Up @@ -84,6 +87,7 @@ def __init__(
# fields copied from the Editable class
full_resource_path: str,
implementation_type: WebFormFieldImplementationTypeFields,
values_provider: EditableValuesProvider | None = None,
couples: set[ResolvedEditableType] | None = None,
children: list[ResolvedEditableType] | None = None,
converter: EditableConverter | None = None,
Expand All @@ -96,6 +100,7 @@ def __init__(
) -> None:
self.full_resource_path = full_resource_path
self.implementation_type = implementation_type
self.values_provider = values_provider
self.couples = set[ResolvedEditableType]() if couples is None else couples
self.children = list[ResolvedEditableType]() if children is None else children
self.converter = converter
Expand Down
31 changes: 31 additions & 0 deletions amt/api/editable_value_providers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from abc import ABC, abstractmethod

from starlette.requests import Request

from amt.api.ai_act_profile import SelectAiProfileItem, get_ai_act_profile_selector
from amt.schema.webform import WebFormOption


class EditableValuesProvider(ABC):
@abstractmethod
async def get_values(self, request: Request) -> list[WebFormOption]:
pass


class AIActValuesProvider(EditableValuesProvider):
def __init__(self, type: str) -> None:
self.type = type

async def get_values(self, request: Request) -> list[WebFormOption]:
profile = get_ai_act_profile_selector(request)
target_ai_act_profile: SelectAiProfileItem | None = next(
(
ai_act_profile
for ai_act_profile in (profile.dropdown_select or []) + (profile.multiple_select or [])
if ai_act_profile.target_name == self.type
),
None,
)
if target_ai_act_profile is not None and target_ai_act_profile.options is not None:
return [WebFormOption(value=option, display_value=option) for option in target_ai_act_profile.options]
return []
59 changes: 28 additions & 31 deletions amt/locale/base.pot
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ msgstr ""

#: amt/api/forms/organization.py:40
#: amt/site/templates/organizations/parts/add_members_modal.html.j2:4
#: amt/site/templates/organizations/parts/add_members_modal.html.j2:23
#: amt/site/templates/organizations/parts/add_members_modal.html.j2:26
msgid "Add members"
msgstr ""

Expand Down Expand Up @@ -380,43 +380,43 @@ msgstr ""
msgid "Something went wrong storing your file. PLease try again later."
msgstr ""

#: amt/site/templates/algorithms/details_base.html.j2:12
#: amt/site/templates/algorithms/details_base.html.j2:13
msgid "Delete algoritmic system"
msgstr ""

#: amt/site/templates/algorithms/details_base.html.j2:19
#: amt/site/templates/algorithms/details_base.html.j2:17
msgid "Are you sure you want to delete your algoritmic system "
msgstr ""

#: amt/site/templates/algorithms/details_base.html.j2:22
#: amt/site/templates/algorithms/details_base.html.j2:20
msgid "Data will be stored for at least 45 days before permanent deletion."
msgstr ""

#: amt/site/templates/algorithms/details_base.html.j2:28
#: amt/site/templates/algorithms/details_base.html.j2:26
#: amt/site/templates/algorithms/new.html.j2:153
#: amt/site/templates/macros/form_macros.html.j2:170
#: amt/site/templates/organizations/members.html.j2:33
#: amt/site/templates/macros/form_macros.html.j2:168
#: amt/site/templates/organizations/members.html.j2:34
msgid "Yes"
msgstr ""

#: amt/site/templates/algorithms/details_base.html.j2:31
#: amt/site/templates/algorithms/details_base.html.j2:29
#: amt/site/templates/algorithms/new.html.j2:163
#: amt/site/templates/macros/form_macros.html.j2:175
#: amt/site/templates/organizations/members.html.j2:36
#: amt/site/templates/macros/form_macros.html.j2:173
#: amt/site/templates/organizations/members.html.j2:37
msgid "No"
msgstr ""

#: amt/site/templates/algorithms/details_base.html.j2:50
#: amt/site/templates/algorithms/details_base.html.j2:49
msgid "Download as YAML"
msgstr ""

#: amt/site/templates/algorithms/details_compliance.html.j2:36
#: amt/site/templates/algorithms/details_compliance.html.j2:29
msgid "measure executed"
msgstr ""

#: amt/site/templates/algorithms/details_compliance.html.j2:69
#: amt/site/templates/macros/editable.html.j2:68
#: amt/site/templates/macros/editable.html.j2:73
#: amt/site/templates/algorithms/details_compliance.html.j2:62
#: amt/site/templates/macros/editable.html.j2:77
#: amt/site/templates/macros/editable.html.j2:82
#: amt/site/templates/macros/tasks.html.j2:98
msgid "Edit"
msgstr ""
Expand All @@ -430,7 +430,7 @@ msgid "Go to all requirements"
msgstr ""

#: amt/site/templates/algorithms/details_info.html.j2:85
#: amt/site/templates/algorithms/details_measure_modal.html.j2:27
#: amt/site/templates/algorithms/details_measure_modal.html.j2:25
msgid "Description"
msgstr ""

Expand Down Expand Up @@ -460,18 +460,18 @@ msgstr ""
msgid "To be implemented"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:37
#: amt/site/templates/algorithms/details_measure_modal.html.j2:35
msgid "Read more on the algoritmekader"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:64
#: amt/site/templates/macros/editable.html.j2:175
#: amt/site/templates/algorithms/details_measure_modal.html.j2:62
#: amt/site/templates/macros/editable.html.j2:205
msgid "Save"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:68
#: amt/site/templates/macros/editable.html.j2:180
#: amt/site/templates/organizations/parts/add_members_modal.html.j2:26
#: amt/site/templates/algorithms/details_measure_modal.html.j2:66
#: amt/site/templates/macros/editable.html.j2:210
#: amt/site/templates/organizations/parts/add_members_modal.html.j2:29
msgid "Cancel"
msgstr ""

Expand Down Expand Up @@ -514,6 +514,7 @@ msgid ""
msgstr ""

#: amt/site/templates/algorithms/new.html.j2:76
#: amt/site/templates/macros/editable.html.j2:9
msgid "Find your AI Act profile"
msgstr ""

Expand All @@ -526,10 +527,6 @@ msgstr ""
msgid "Add algorithm"
msgstr ""

#: amt/site/templates/algorithms/new.html.j2:193
msgid "Copy results and close"
msgstr ""

#: amt/site/templates/auth/profile.html.j2:10
msgid "of"
msgstr ""
Expand Down Expand Up @@ -642,11 +639,11 @@ msgstr ""
msgid "Delete member"
msgstr ""

#: amt/site/templates/macros/form_macros.html.j2:152
#: amt/site/templates/macros/form_macros.html.j2:153
msgid "Delete file"
msgstr ""

#: amt/site/templates/macros/form_macros.html.j2:159
#: amt/site/templates/macros/form_macros.html.j2:157
msgid "Are you sure you want to delete"
msgstr ""

Expand All @@ -670,7 +667,7 @@ msgstr ""
msgid "Modified at"
msgstr ""

#: amt/site/templates/organizations/members.html.j2:24
#: amt/site/templates/organizations/members.html.j2:25
#: amt/site/templates/organizations/parts/add_members_modal.html.j2:8
msgid "Close"
msgstr ""
Expand Down Expand Up @@ -822,11 +819,11 @@ msgstr ""
msgid "This page is yet to be build."
msgstr ""

#: amt/site/templates/parts/algorithm_search.html.j2:62
#: amt/site/templates/parts/algorithm_search.html.j2:64
msgid "Category"
msgstr ""

#: amt/site/templates/parts/algorithm_search.html.j2:83
#: amt/site/templates/parts/algorithm_search.html.j2:85
msgid "Group by"
msgstr ""

Expand Down
Binary file modified amt/locale/en_US/LC_MESSAGES/messages.mo
Binary file not shown.
Loading

0 comments on commit 6782e6e

Please sign in to comment.