Skip to content

Commit

Permalink
Add document link field in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherSpelt committed Dec 3, 2024
1 parent 2b0ce4b commit 0b48fc0
Show file tree
Hide file tree
Showing 14 changed files with 725 additions and 174 deletions.
52 changes: 52 additions & 0 deletions amt/api/forms/measure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from gettext import NullTranslations

from amt.schema.webform import WebForm, WebFormField, WebFormFieldType, WebFormOption, WebFormTextCloneableField


async def get_measure_form(
id: str, current_values: dict[str, str | list[str]], translations: NullTranslations
) -> WebForm:
_ = translations.gettext

measure_form: WebForm = WebForm(id="", post_url="")

measure_form.fields = [
WebFormField(
type=WebFormFieldType.SELECT,
name="measure_state",
label=_("Status"),
options=[
WebFormOption(value="to do", display_value="to do"),
WebFormOption(value="in progress", display_value="in progress"),
WebFormOption(value="done", display_value="done"),
],
default_value=current_values.get("measure_state"),
group="1",
),
WebFormField(
type=WebFormFieldType.TEXTAREA,
name="measure_value",
default_value=current_values.get("measure_value"),
label=_("Information on how this measure is implemented"),
placeholder=_("Describe how the measure has been implemented, including challenges and solutions."),
group="1",
),
WebFormField(
type=WebFormFieldType.FILE,
name="measure_files",
default_value=current_values.get("measure_files"),
label=_("Add files"),
placeholder=_(""),
group="1",
),
WebFormTextCloneableField(
clone_button_name=_("Add URI"),
name="measure_links",
default_value=current_values.get("measure_links"),
label=_("Add links to documents"),
placeholder=_("URI"),
group="1",
),
]

return measure_form
34 changes: 28 additions & 6 deletions amt/api/routes/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from collections.abc import Sequence
from typing import Annotated, Any, cast

from fastapi import APIRouter, Depends, Request
from fastapi import APIRouter, Depends, File, Form, Request, UploadFile
from fastapi.responses import HTMLResponse
from pydantic import BaseModel, Field

from amt.api.deps import templates
from amt.api.forms.measure import get_measure_form
from amt.api.navigation import (
BaseNavigationItem,
Navigation,
Expand All @@ -17,6 +18,7 @@
)
from amt.core.authorization import get_user
from amt.core.exceptions import AMTNotFound, AMTRepositoryError
from amt.core.internationalization import get_current_translation
from amt.enums.status import Status
from amt.models import Algorithm
from amt.models.task import Task
Expand Down Expand Up @@ -506,12 +508,21 @@ async def get_measure(
measures_service = create_measures_service()
measure = await measures_service.fetch_measures([measure_urn])
measure_task = find_measure_task(algorithm.system_card, measure_urn)
measure_form = await get_measure_form(
id="measure_state",
current_values={
"measure_state": measure_task.state, # pyright: ignore [reportOptionalMemberAccess]
"measure_value": measure_task.value, # pyright: ignore [reportOptionalMemberAccess]
"measure_links": measure_task.links, # pyright: ignore [reportOptionalMemberAccess]
"measure_files": measure_task.files, # pyright: ignore [reportOptionalMemberAccess]
},
translations=get_current_translation(request),
)

context = {
"measure": measure[0],
"measure_state": measure_task.state, # pyright: ignore [reportOptionalMemberAccess]
"measure_value": measure_task.value, # pyright: ignore [reportOptionalMemberAccess]
"algorithm_id": algorithm_id,
"form": measure_form,
}

return templates.TemplateResponse(request, "algorithms/details_measure_modal.html.j2", context)
Expand All @@ -520,22 +531,33 @@ async def get_measure(
class MeasureUpdate(BaseModel):
measure_state: str = Field(default=None)
measure_value: str = Field(default=None)
measure_links: list[str] = Field(default=[])
measure_files: list[str] = Field(default=[])


@router.post("/{algorithm_id}/measure/{measure_urn}")
async def update_measure_value(
request: Request,
algorithm_id: int,
measure_urn: str,
measure_update: MeasureUpdate,
algorithms_service: Annotated[AlgorithmsService, Depends(AlgorithmsService)],
requirements_service: Annotated[RequirementsService, Depends(create_requirements_service)],
measure_state: Annotated[str, Form()],
measure_value: Annotated[str | None, Form()] = None,
measure_links: Annotated[list[str] | None, Form()] = None,
measure_files: Annotated[list[UploadFile] | None, File()] = None,
) -> HTMLResponse:
algorithm = await get_algorithm_or_error(algorithm_id, algorithms_service, request)

measure_task = find_measure_task(algorithm.system_card, measure_urn)
measure_task.state = measure_update.measure_state # pyright: ignore [reportOptionalMemberAccess]
measure_task.value = measure_update.measure_value # pyright: ignore [reportOptionalMemberAccess]
measure_task.state = measure_state # measure_update.measure_state # pyright: ignore [reportOptionalMemberAccess]
if measure_value:
measure_task.value = (
measure_value # measure_update.measure_value # pyright: ignore [reportOptionalMemberAccess]
)
measure_task.links = [link for link in measure_links if link] if measure_links else [] # pyright: ignore [reportOptionalMemberAccess]
new_files = [file.filename for file in measure_files if file and file.filename] if measure_files else []
measure_task.files.extend(new_files) # pyright: ignore [reportOptionalMemberAccess]

# update for the linked requirements the state based on all it's measures
requirement_tasks = await find_requirement_tasks_by_measure_urn(algorithm.system_card, measure_urn)
Expand Down
69 changes: 37 additions & 32 deletions amt/locale/base.pot
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@ msgstr ""
msgid "Organization"
msgstr ""

#: amt/api/forms/measure.py:17
msgid "Status"
msgstr ""

#: amt/api/forms/measure.py:30
msgid "Information on how this measure is implemented"
msgstr ""

#: amt/api/forms/measure.py:31
msgid ""
"Describe how the measure has been implemented, including challenges and "
"solutions."
msgstr ""

#: amt/api/forms/measure.py:38
msgid "Add files"
msgstr ""

#: amt/api/forms/measure.py:43
msgid "Add URI"
msgstr ""

#: amt/api/forms/measure.py:46
msgid "Add links to documents"
msgstr ""

#: amt/api/forms/measure.py:47
msgid "URI"
msgstr ""

#: amt/api/forms/organization.py:15
#: amt/site/templates/algorithms/details_info.html.j2:8
#: amt/site/templates/auth/profile.html.j2:34
Expand Down Expand Up @@ -450,41 +480,12 @@ msgstr ""
msgid "Read more on the algoritmekader"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:38
msgid "Status"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:45
#: amt/site/templates/algorithms/details_measure_modal.html.j2:47
msgid "to do"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:50
#: amt/site/templates/algorithms/details_measure_modal.html.j2:52
msgid "in progress"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:55
#: amt/site/templates/algorithms/details_measure_modal.html.j2:57
msgid "done"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:68
msgid "Information on how this measure is implemented"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:74
msgid ""
"Describe how the measure has been implemented, including challenges and "
"solutions."
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:84
#: amt/site/templates/algorithms/details_measure_modal.html.j2:40
#: amt/site/templates/macros/editable.html.j2:82
msgid "Save"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:88
#: amt/site/templates/algorithms/details_measure_modal.html.j2:44
#: amt/site/templates/macros/editable.html.j2:87
msgid "Cancel"
msgstr ""
Expand Down Expand Up @@ -617,10 +618,14 @@ msgstr ""
msgid "There is one error:"
msgstr ""

#: amt/site/templates/layouts/base.html.j2:1
#: amt/site/templates/layouts/base.html.j2:11
msgid "Algorithmic Management Toolkit (AMT)"
msgstr ""

#: amt/site/templates/macros/form_macros.html.j2:127
msgid "Saved files"
msgstr ""

#: amt/site/templates/macros/table_row.html.j2:19
msgid " ago"
msgstr ""
Expand Down
69 changes: 37 additions & 32 deletions amt/locale/en_US/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,36 @@ msgstr ""
msgid "Organization"
msgstr ""

#: amt/api/forms/measure.py:17
msgid "Status"
msgstr ""

#: amt/api/forms/measure.py:30
msgid "Information on how this measure is implemented"
msgstr ""

#: amt/api/forms/measure.py:31
msgid ""
"Describe how the measure has been implemented, including challenges and "
"solutions."
msgstr ""

#: amt/api/forms/measure.py:38
msgid "Add files"
msgstr ""

#: amt/api/forms/measure.py:43
msgid "Add URI"
msgstr ""

#: amt/api/forms/measure.py:46
msgid "Add links to documents"
msgstr ""

#: amt/api/forms/measure.py:47
msgid "URI"
msgstr ""

#: amt/api/forms/organization.py:15
#: amt/site/templates/algorithms/details_info.html.j2:8
#: amt/site/templates/auth/profile.html.j2:34
Expand Down Expand Up @@ -451,41 +481,12 @@ msgstr ""
msgid "Read more on the algoritmekader"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:38
msgid "Status"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:45
#: amt/site/templates/algorithms/details_measure_modal.html.j2:47
msgid "to do"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:50
#: amt/site/templates/algorithms/details_measure_modal.html.j2:52
msgid "in progress"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:55
#: amt/site/templates/algorithms/details_measure_modal.html.j2:57
msgid "done"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:68
msgid "Information on how this measure is implemented"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:74
msgid ""
"Describe how the measure has been implemented, including challenges and "
"solutions."
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:84
#: amt/site/templates/algorithms/details_measure_modal.html.j2:40
#: amt/site/templates/macros/editable.html.j2:82
msgid "Save"
msgstr ""

#: amt/site/templates/algorithms/details_measure_modal.html.j2:88
#: amt/site/templates/algorithms/details_measure_modal.html.j2:44
#: amt/site/templates/macros/editable.html.j2:87
msgid "Cancel"
msgstr ""
Expand Down Expand Up @@ -618,10 +619,14 @@ msgstr ""
msgid "There is one error:"
msgstr ""

#: amt/site/templates/layouts/base.html.j2:1
#: amt/site/templates/layouts/base.html.j2:11
msgid "Algorithmic Management Toolkit (AMT)"
msgstr ""

#: amt/site/templates/macros/form_macros.html.j2:127
msgid "Saved files"
msgstr ""

#: amt/site/templates/macros/table_row.html.j2:19
msgid " ago"
msgstr ""
Expand Down
Loading

0 comments on commit 0b48fc0

Please sign in to comment.