Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #481 from communitiesuk/FS-3705-Assessor-comment-e…
Browse files Browse the repository at this point in the history
…diting

Fs 3705 assessor comment editing
  • Loading branch information
RamuniN authored Nov 14, 2023
2 parents 78dd5df + 7e51057 commit 433dfce
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 27 deletions.
18 changes: 18 additions & 0 deletions app/blueprints/assessments/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ def display_sub_criteria(
)
)

edit_comment_argument = request.args.get("edit_comment")
comment_id = request.args.get("comment_id")
if edit_comment_argument and comment_form.validate_on_submit():
comment = comment_form.comment.data
submit_comment(comment=comment, comment_id=comment_id)

return redirect(
url_for(
"assessment_bp.display_sub_criteria",
application_id=application_id,
sub_criteria_id=sub_criteria_id,
theme_id=theme_id,
_anchor="comments",
)
)

state = get_state_for_tasklist_banner(application_id)
flags_list = get_flags(application_id)

Expand Down Expand Up @@ -426,6 +442,8 @@ def display_sub_criteria(
"comments": theme_matched_comments,
"is_flaggable": False, # Flag button is disabled in sub-criteria page,
"display_comment_box": add_comment_argument,
"display_comment_edit_box": edit_comment_argument,
"comment_id": comment_id,
"comment_form": comment_form,
"current_theme": current_theme,
"flag_status": flag_status,
Expand Down
17 changes: 15 additions & 2 deletions app/blueprints/assessments/templates/macros/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<h2 class="govuk-heading-m govuk-!-margin-bottom-2">Comments</h2>
<div id="more-detail-hint" class="govuk-hint">
<p class="govuk-hint govuk-!-margin-top-0">Summarise any thoughts you have on the information provided.</p>
<p class="govuk-hint govuk-!-margin-top-0">You cannot edit or delete your comment once you have saved it.</p>
</div>
{% if comments == None %}
<div class="govuk-body govuk-!-margin-top-6">
Expand All @@ -18,9 +17,23 @@ <h2 class="govuk-heading-m govuk-!-margin-bottom-2">Comments</h2>
</div>
{% else %}
<div class="comment-group">
<p class="govuk-body">{{ comment.comment }}</p>
<p class="govuk-body">{{ comment.updates[-1]['comment'] }}</p>
<p class="govuk-body-s">{{ comment.full_name }} ({{ comment.highest_role|all_caps_to_human }}) {{ comment.email_address }}</p>
<p class="govuk-body-s">{{ comment.date_created|utc_to_bst }}</p>
{% if g.account_id == comment.user_id %}
<div class="govuk-button-group">
<a class="govuk-link" href="{{ url_for(
"assessment_bp.display_sub_criteria",
application_id=comment.application_id,
sub_criteria_id=comment.sub_criteria_id,
edit_comment="1",
comment_id=comment.id
) }}" >Edit comment</a>
{# {% if comment.updates|length > 1 %}
<a class="govuk-link" href="./comment-history.html">See history</a>
{% endif %} #}
</div>
{% endif %}
</div>
{% endif %}
{% endfor %}
Expand Down
6 changes: 4 additions & 2 deletions app/blueprints/assessments/templates/macros/comments_box.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{%- from 'govuk_frontend_jinja/components/textarea/macro.html' import govukTextarea -%}
{%- from "govuk_frontend_jinja/components/button/macro.html" import govukButton -%}

{% macro comment_box(comment_form) %}
{% macro comment_box(comment_form, html_text, placeholder) %}

<form method="post">
<div class="govuk-grid-row">
Expand All @@ -12,10 +12,12 @@
"id": comment_form.comment.id,
"rows": 8,
"label": {
"text": "Add a comment",
"text": "" if html_text else "Add a comment",
"html": html_text if html_text else "",
"classes": "govuk-label--m",
isPageHeading: true
},
"value": placeholder if placeholder else "",
"errorMessage": {
"text": comment_form.comment.errors.0
} if comment_form.comment.errors
Expand Down
30 changes: 30 additions & 0 deletions app/blueprints/assessments/templates/macros/comments_edit_box.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% from "macros/comments_box.html" import comment_box %}

{% macro edit_comment_box(comments, comment_id, comment_form) %}

{# Using namespace objects to access variables set outside of for loop #}
{% set comment_text = namespace(str = "") %}
{% set comment_author = namespace(str = "") %}
{% set comment_date = namespace(str = "") %}

{% for comment_list in comments.values() %}
{% for comment in comment_list %}
{% if comment.id == comment_id %}
{% set comment_text.str = comment.updates[-1]['comment'] %}
{% set comment_author.str = comment.full_name + " (" + comment.highest_role|all_caps_to_human + ") "+ comment.email_address %}
{% set comment_date.str = comment.date_created|utc_to_bst %}
{% endif %}
{% endfor %}
{% endfor %}

{% set HtmlData %}
<h2 class="govuk-heading-m govuk-!-margin-bottom-2">Edit comment</h2>
<div class="comment-group">
<p class="govuk-body">{{ comment_text.str }}</p>
<p class="govuk-body-s">{{ comment_author.str }}</p>
<p class="govuk-body-s">{{ comment_date.str }}</p>
</div>
{% endset %}

{{ comment_box(comment_form, HtmlData, comment_text.str) }}
{% endmacro %}
6 changes: 5 additions & 1 deletion app/blueprints/assessments/templates/sub_criteria.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% from "macros/banner_summary.html" import banner_summary %}
{% from "macros/flag_application_button.html" import flag_application_button %}
{% from "macros/comments.html" import comment %}
{% from "macros/comments_edit_box.html" import edit_comment_box %}
{% from "macros/comments_box.html" import comment_box %}

{% set pageHeading -%}
Expand Down Expand Up @@ -55,7 +56,7 @@ <h2 class="govuk-heading-l scoring-heading">{{ sub_criteria.name }}</h2>
<div class="theme govuk-grid-column-two-thirds">
{{ theme(answers_meta)}}
{{ comment(comments)}}
{% if display_comment_box != True %}
{% if display_comment_box != True and not display_comment_edit_box %}
<a
id="comment"
class="govuk-button secondary-button govuk-!-margin-top-2 govuk-!-margin-bottom-6"
Expand All @@ -79,6 +80,9 @@ <h2 class="govuk-heading-l scoring-heading">{{ sub_criteria.name }}</h2>
{% if display_comment_box == True %}
{{ comment_box(comment_form) }}
{% endif %}
{% if display_comment_edit_box %}
{{ edit_comment_box(comments, comment_id, comment_form)}}
{% endif %}
</div>
</div>
{% endblock content %}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3 class="govuk-heading-m">{{ theme.name }}</h3>
</div>
{% else %}
<div class="comment-group">
<p class="govuk-body">{{ comment.comment }}</p>
<p class="govuk-body">{{ comment.updates[-1].comment }}</p>
<p class="govuk-body-s">{{ comment.full_name }} ({{ comment.highest_role|all_caps_to_human }})
{{ comment.email_address }}</p>
<p class="govuk-body-s">{{ comment.date_created|utc_to_bst }}</p>
Expand Down
37 changes: 25 additions & 12 deletions app/blueprints/services/data_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,22 +729,35 @@ def match_comment_to_theme(comment_response, themes, fund_short_name):


def submit_comment(
comment, application_id, sub_criteria_id, user_id, theme_id
comment,
application_id=None,
sub_criteria_id=None,
user_id=None,
theme_id=None,
comment_id=None,
):
data_dict = {
"comment": comment,
"user_id": user_id,
"application_id": application_id,
"sub_criteria_id": sub_criteria_id,
"comment_type": "COMMENT",
"theme_id": theme_id,
}
url = Config.ASSESSMENT_COMMENT_ENDPOINT
response = requests.post(url, json=data_dict)
if not comment_id:
data_dict = {
"comment": comment,
"user_id": user_id,
"application_id": application_id,
"sub_criteria_id": sub_criteria_id,
"comment_type": "COMMENT",
"theme_id": theme_id,
}
url = Config.ASSESSMENT_COMMENT_ENDPOINT
response = requests.post(url, json=data_dict)
else:
data_dict = {
"comment": comment,
"comment_id": comment_id,
}
url = Config.ASSESSMENT_COMMENT_ENDPOINT
response = requests.put(url, json=data_dict)

current_app.logger.info(
f"Response from Assessment Store: '{response.json()}'."
)

return response.ok


Expand Down
18 changes: 17 additions & 1 deletion app/blueprints/services/models/comment.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import inspect
from dataclasses import dataclass
from datetime import datetime


@dataclass
class Comment:
comment: str
id: str
user_id: str
date_created: str
email_address: str
full_name: str
highest_role: str
theme_id: str
fund_short_name: str
updates: list
application_id: str
sub_criteria_id: str

def __post_init__(self):
for item in self.updates:
item["date_created"] = datetime.fromisoformat(
item["date_created"]
).strftime("%Y-%m-%d %X")

# sort the updates in the order they are created
if self.updates:
self.updates = sorted(
self.updates, key=lambda x: x["date_created"]
)

@classmethod
def from_dict(cls, d: dict):
Expand Down
61 changes: 53 additions & 8 deletions tests/api_data/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

test_fund_id = "test-fund"
test_round_id = "test-round"
test_user_id_lead_assessor = "test_user_lead_assessor"
test_user_id_assessor = "test_user_assessor"
test_user_id_commenter = "test_user_commenter"
test_user_id_lead_assessor = "lead"
test_user_id_assessor = "assessor"
test_user_id_commenter = "commenter"
test_funding_requested = 5000.0

# application specific config
Expand Down Expand Up @@ -583,34 +583,79 @@
],
"assessment_store/comment?": [
{
"comment": "This is a comment",
"id": "test_id_1",
"user_id": test_user_id_lead_assessor,
"date_created": "2022-12-08T08:00:01.748170",
"theme_id": resolved_app["theme_id"],
"sub_criteria_id": "test_sub_criteria_id",
"application_id": resolved_app["id"],
"updates": [
{
"comment": "This is a comment",
"comment_id": "test_id_1",
"date_created": "2022-12-08T08:00:01.748170",
}
],
},
{
"comment": "You're missing some details",
"id": "test_id_2",
"user_id": test_user_id_lead_assessor,
"date_created": "2022-10-27T08:00:02.748170",
"theme_id": resolved_app["theme_id"],
"sub_criteria_id": "test_sub_criteria_id",
"application_id": resolved_app["id"],
"updates": [
{
"comment": "You're missing some details",
"comment_id": "test_id_2",
"date_created": "2022-10-27T08:00:02.748170",
}
],
},
{
"comment": "Im a lead assessor",
"id": "test_id_3",
"user_id": test_user_id_lead_assessor,
"date_created": "2022-10-27T08:00:03.748170",
"theme_id": resolved_app["theme_id"],
"sub_criteria_id": "test_sub_criteria_id",
"application_id": resolved_app["id"],
"updates": [
{
"comment": "You're missing some details",
"comment_id": "test_id_3",
"date_created": "2022-10-27T08:00:03.748170",
}
],
},
{
"comment": "Im an assessor",
"id": "test_id_4",
"user_id": test_user_id_assessor,
"date_created": "2022-10-27T08:00:04.748170",
"theme_id": resolved_app["theme_id"],
"sub_criteria_id": "test_sub_criteria_id",
"application_id": resolved_app["id"],
"updates": [
{
"comment": "Im an assessor",
"comment_id": "test_id_4",
"date_created": "2022-10-27T08:00:04.748170",
}
],
},
{
"comment": "Im a commenter",
"id": "test_id_5",
"user_id": test_user_id_commenter,
"date_created": "2022-10-27T08:00:05.748170",
"theme_id": resolved_app["theme_id"],
"sub_criteria_id": "test_sub_criteria_id",
"application_id": resolved_app["id"],
"updates": [
{
"comment": "Im a commenter",
"comment_id": "test_id_5",
"date_created": "2022-10-27T08:00:05.748170",
}
],
},
],
"assessment_store/score?": [
Expand Down
14 changes: 14 additions & 0 deletions tests/test_authorisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ def test_different_user_levels_see_correct_comments_on_sub_criteria_view(
)
assert g.user.roles is not None

soup = BeautifulSoup(response.data, "html.parser")
all_comments = soup.find_all("div", class_="comment-group")

if claim["accountId"] == "commenter":
assert "Permission required to see comment." in str(all_comments)

for comment in all_comments:
comment_str = str(comment)
# "Edit comment" button is available only for the comment owner
if claim["email"] in comment_str:
assert "Edit comment" in comment_str
else:
assert "Edit comment" not in comment_str

if expect_all_comments_available:
assert is_lead_assessor_comment_visible
assert is_assessor_comment_visible
Expand Down

0 comments on commit 433dfce

Please sign in to comment.