Skip to content

Commit

Permalink
Restructure tabs (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensWe authored Jan 15, 2025
2 parents cf5de19 + e9bb1e9 commit 36d12d0
Show file tree
Hide file tree
Showing 30 changed files with 13,100 additions and 7,764 deletions.
17 changes: 5 additions & 12 deletions amt/api/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DisplayText(Enum):
DETAILS = "details"
ORGANIZATIONS = "organizations"
MEMBERS = "members"
COMPLIANCE = "compliance"


def get_translation(key: DisplayText, translations: NullTranslations) -> str:
Expand Down Expand Up @@ -60,8 +61,8 @@ def get_translation(key: DisplayText, translations: NullTranslations) -> str:
DisplayText.MODEL: _("Model"),
DisplayText.INSTRUMENTS: _("Instruments"),
DisplayText.ORGANIZATIONS: _("Organizations"),
DisplayText.ALGORITHMS: _("Algorithms"),
DisplayText.MEMBERS: _("Members"),
DisplayText.COMPLIANCE: _("Compliance"),
}
return keys[key]

Expand Down Expand Up @@ -105,9 +106,6 @@ class Navigation:
ALGORITHM_DETAILS = BaseNavigationItem(
display_text=DisplayText.DETAILS, url="/algorithm/{algorithm_id}/details/system_card"
)
ALGORITHM_MODEL = BaseNavigationItem(
display_text=DisplayText.MODEL, url="/algorithm/{algorithm_id}/details/model/inference"
)
ALGORITHM_NEW = BaseNavigationItem(display_text=DisplayText.NEW, url="/algorithms/new")
ALGORITHM_INFO = BaseNavigationItem(display_text=DisplayText.INFO, url="/algorithm/{algorithm_id}/details")
ALGORITHM_ALGORITHM_DETAILS = BaseNavigationItem(
Expand All @@ -116,9 +114,6 @@ class Navigation:
ALGORITHM_SYSTEM_CARD = BaseNavigationItem(
display_text=DisplayText.SYSTEMCARD, url="/algorithm/{algorithm_id}/details/system_card"
)
ALGORITHM_DATA_CARD = BaseNavigationItem(
display_text=DisplayText.DATA, url="/algorithm/{algorithm_id}/details/system_card/data"
)
ALGORITHM_MODEL_CARD = BaseNavigationItem(
display_text=DisplayText.MODELCARD,
url="/algorithm/{algorithm_id}/details/system_card/models/{model_card}",
Expand All @@ -127,12 +122,10 @@ class Navigation:
display_text=DisplayText.ASSESSMENTCARD,
url="/algorithm/{algorithm_id}/details/system_card/assessment/{assessment_card}",
)
ALGORITHM_REQUIREMENTS = BaseNavigationItem(
display_text=DisplayText.REQUIREMENTS, url="/algorithm/{algorithm_id}/details/system_card/requirements"
)
ALGORITHM_INSTRUMENTS = BaseNavigationItem(
display_text=DisplayText.INSTRUMENTS, url="/algorithm/{algorithm_id}/details/system_card/instruments"
ALGORITHM_COMPLIANCE = BaseNavigationItem(
display_text=DisplayText.COMPLIANCE, url="/algorithm/{algorithm_id}/details/system_card/compliance"
)
ALGORITHM_MEMBERS = BaseNavigationItem(display_text=DisplayText.MEMBERS, url="/algorithm/{algorithm_id}/members")
ORGANIZATIONS_ROOT = BaseNavigationItem(
display_text=DisplayText.ORGANIZATIONS, url="/organizations", icon="rvo-icon-man-torso-voor-hoogbouw"
)
Expand Down
139 changes: 31 additions & 108 deletions amt/api/routes/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from amt.core.exceptions import AMTError, AMTNotFound, AMTRepositoryError
from amt.core.internationalization import get_current_translation
from amt.enums.status import Status
from amt.models import Algorithm
from amt.models import Algorithm, User
from amt.models.task import Task
from amt.repositories.organizations import OrganizationsRepository
from amt.repositories.users import UsersRepository
Expand Down Expand Up @@ -102,11 +102,8 @@ def get_algorithm_details_tabs(request: Request) -> list[NavigationItem]:
[
Navigation.ALGORITHM_INFO,
Navigation.ALGORITHM_ALGORITHM_DETAILS,
Navigation.ALGORITHM_MODEL,
Navigation.ALGORITHM_REQUIREMENTS,
Navigation.ALGORITHM_DATA_CARD,
Navigation.ALGORITHM_COMPLIANCE,
Navigation.ALGORITHM_TASKS,
Navigation.ALGORITHM_INSTRUMENTS,
],
request,
)
Expand Down Expand Up @@ -377,43 +374,7 @@ async def get_system_card(
return templates.TemplateResponse(request, "pages/system_card.html.j2", context)


@router.get("/{algorithm_id}/details/model/inference")
async def get_algorithm_inference(
request: Request, algorithm_id: int, algorithms_service: Annotated[AlgorithmsService, Depends(AlgorithmsService)]
) -> HTMLResponse:
algorithm = await get_algorithm_or_error(algorithm_id, algorithms_service, request)

breadcrumbs = resolve_base_navigation_items(
[
Navigation.ALGORITHMS_ROOT,
BaseNavigationItem(
custom_display_text=algorithm.name, url="/algorithm/{algorithm_id}/details/model/inference"
),
Navigation.ALGORITHM_MODEL,
],
request,
)

instrument_state = await get_instrument_state(algorithm.system_card)
requirements_state = await get_requirements_state(algorithm.system_card)

tab_items = get_algorithm_details_tabs(request)

context = {
"last_edited": algorithm.last_edited,
"system_card": algorithm.system_card,
"instrument_state": instrument_state,
"requirements_state": requirements_state,
"algorithm": algorithm,
"algorithm_id": algorithm.id,
"breadcrumbs": breadcrumbs,
"tab_items": tab_items,
}

return templates.TemplateResponse(request, "algorithms/details_inference.html.j2", context)


@router.get("/{algorithm_id}/details/system_card/requirements")
@router.get("/{algorithm_id}/details/system_card/compliance")
async def get_system_card_requirements(
request: Request,
algorithm_id: int,
Expand All @@ -435,7 +396,7 @@ async def get_system_card_requirements(
[
Navigation.ALGORITHMS_ROOT,
BaseNavigationItem(custom_display_text=algorithm.name, url="/algorithm/{algorithm_id}/details/system_card"),
Navigation.ALGORITHM_SYSTEM_CARD,
Navigation.ALGORITHM_COMPLIANCE,
],
request,
)
Expand Down Expand Up @@ -482,7 +443,17 @@ async def get_system_card_requirements(
"measure_task_functions": measure_task_functions,
}

return templates.TemplateResponse(request, "algorithms/details_requirements.html.j2", context)
return templates.TemplateResponse(request, "algorithms/details_compliance.html.j2", context)


async def _fetch_members(
users_repository: UsersRepository,
search_name: str,
sort_by: dict[str, str],
filters: dict[str, str],
) -> User | None:
members = await users_repository.find_all(search=search_name, sort=sort_by, filters=filters)
return members[0] if members else None


async def get_measure_task_functions(
Expand All @@ -492,33 +463,18 @@ async def get_measure_task_functions(
filters: dict[str, str],
) -> dict[str, list[Any]]:
measure_task_functions: dict[str, list[Any]] = defaultdict(list)

for measure_task in measure_tasks:
if measure_task.accountable_persons: # pyright: ignore [reportOptionalMemberAccess]
members_accountable = await users_repository.find_all(
search=measure_task.accountable_persons[0].name, # pyright: ignore [reportOptionalMemberAccess]
sort=sort_by,
filters=filters,
)
if members_accountable:
measure_task_functions[measure_task.urn].append(members_accountable[0]) # pyright: ignore [reportOptionalMemberAccess]

if measure_task.reviewer_persons: # pyright: ignore [reportOptionalMemberAccess]
members_reviewer = await users_repository.find_all(
search=measure_task.reviewer_persons[0].name, # pyright: ignore [reportOptionalMemberAccess]
sort=sort_by,
filters=filters,
)
if members_reviewer:
measure_task_functions[measure_task.urn].append(members_reviewer[0]) # pyright: ignore [reportOptionalMemberAccess]

if measure_task.responsible_persons: # pyright: ignore [reportOptionalMemberAccess]
members_responsible = await users_repository.find_all(
search=measure_task.responsible_persons[0].name, # pyright: ignore [reportOptionalMemberAccess]
sort=sort_by,
filters=filters,
)
if members_responsible:
measure_task_functions[measure_task.urn].append(members_responsible[0]) # pyright: ignore [reportOptionalMemberAccess]
if measure_task is None:
continue

person_types = ["accountable_persons", "reviewer_persons", "responsible_persons"]
for person_type in person_types:
person_list = getattr(measure_task, person_type)
if person_list:
member = await _fetch_members(users_repository, person_list[0].name, sort_by, filters)
if member:
measure_task_functions[measure_task.urn].append(member)
return measure_task_functions


Expand Down Expand Up @@ -699,44 +655,11 @@ async def update_measure_value(

await algorithms_service.update(algorithm)
# TODO: FIX THIS!! The page now reloads at the top, which is annoying
return templates.Redirect(request, f"/algorithm/{algorithm_id}/details/system_card/requirements")


@router.get("/{algorithm_id}/details/system_card/data")
async def get_system_card_data_page(
request: Request,
algorithm_id: int,
algorithms_service: Annotated[AlgorithmsService, Depends(AlgorithmsService)],
) -> HTMLResponse:
algorithm = await get_algorithm_or_error(algorithm_id, algorithms_service, request)
instrument_state = await get_instrument_state(algorithm.system_card)
requirements_state = await get_requirements_state(algorithm.system_card)

tab_items = get_algorithm_details_tabs(request)

breadcrumbs = resolve_base_navigation_items(
[
Navigation.ALGORITHMS_ROOT,
BaseNavigationItem(custom_display_text=algorithm.name, url="/algorithm/{algorithm_id}/details/system_card"),
Navigation.ALGORITHM_SYSTEM_CARD,
],
request,
)
return templates.Redirect(request, f"/algorithm/{algorithm_id}/details/system_card/compliance")

context = {
"instrument_state": instrument_state,
"requirements_state": requirements_state,
"algorithm": algorithm,
"algorithm_id": algorithm.id,
"tab_items": tab_items,
"breadcrumbs": breadcrumbs,
}

return templates.TemplateResponse(request, "algorithms/details_data.html.j2", context)


@router.get("/{algorithm_id}/details/system_card/instruments")
async def get_system_card_instruments(
@router.get("/{algorithm_id}/members")
async def get_algorithm_members(
request: Request,
algorithm_id: int,
algorithms_service: Annotated[AlgorithmsService, Depends(AlgorithmsService)],
Expand All @@ -751,7 +674,7 @@ async def get_system_card_instruments(
[
Navigation.ALGORITHMS_ROOT,
BaseNavigationItem(custom_display_text=algorithm.name, url="/algorithm/{algorithm_id}/details/system_card"),
Navigation.ALGORITHM_SYSTEM_CARD,
Navigation.ALGORITHM_MEMBERS,
],
request,
)
Expand All @@ -765,7 +688,7 @@ async def get_system_card_instruments(
"breadcrumbs": breadcrumbs,
}

return templates.TemplateResponse(request, "algorithms/details_instruments.html.j2", context)
return templates.TemplateResponse(request, "algorithms/members.html.j2", context)


@router.get("/{algorithm_id}/details/system_card/assessments/{assessment_card}")
Expand Down
2 changes: 1 addition & 1 deletion amt/api/routes/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ async def post_new(
# TODO (Robbert): we need to handle (show) repository or service errors in the forms
if user:
algorithm = await algorithms_service.create(algorithm_new, user["sub"])
response = templates.Redirect(request, f"/algorithm/{algorithm.id}/details/tasks")
response = templates.Redirect(request, f"/algorithm/{algorithm.id}/details")
return response
raise AMTAuthorizationError
Loading

0 comments on commit 36d12d0

Please sign in to comment.