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

Commit

Permalink
Translate boolean to french readable in reports
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreJunod committed May 22, 2024
1 parent cd354d1 commit d391ca7
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions geocity/apps/reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@
from .services import generate_report_pdf_as_response


def preprocess_comment(comment):
if comment:
return mark_safe(comment.replace("\r\n", "<br>").replace("\n", "<br>"))
return comment


def preprocess_field_format(value):
"""
Used to transform fields in reports like ["first", "seconde"] to:
- first
- second
Without breaking other fields and not adding "- " to single choices fields
Uses of this function :
- Interpret line breaks
- Translate bool values in french
- Transform list in formatted strings
"""
if value:
if isinstance(value, list):
print(len(value))
if len(value) > 1:
result = "- " + "<br>- ".join(str(item) for item in value)
else:
result = value[0]
return mark_safe(result)

if isinstance(value, list):
print(len(value))
if len(value) > 1:
result = "- " + "<br>- ".join(str(item) for item in value)
else:
return preprocess_comment(value)
result = value[0]
return mark_safe(result)

if isinstance(value, bool):
return "Vrai" if value else "Faux"

if value:
return mark_safe(value.replace("\r\n", "<br>").replace("\n", "<br>"))

return value


Expand Down Expand Up @@ -77,15 +75,15 @@ def report_content(request, submission_id, form_id, report_id, **kwargs):
request_json_data["properties"].get("validations", {}).items()
):
if "comment" in validation:
validation["comment"] = preprocess_comment(validation["comment"])
validation["comment"] = preprocess_field_format(validation["comment"])

# Add line breaks for amend_fields
for form_key, forms in (
request_json_data["properties"].get("amend_fields", {}).items()
):
for comment_key, comment in forms.get("fields", {}).items():
if "value" in comment:
comment["value"] = preprocess_comment(comment["value"])
comment["value"] = preprocess_field_format(comment["value"])

# Reformat fields to remove lists and add line breaks
for form_key, forms in (
Expand Down

0 comments on commit d391ca7

Please sign in to comment.