From 1bbaaa9bdc8e5ac6ad6e9524e224f6c9b0f5fb3b Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Mon, 7 Oct 2019 13:23:52 +0200 Subject: [PATCH 1/4] Convert Analysis Remarks to HTML --- bika/lims/api/__init__.py | 18 +++++++++++++++++ bika/lims/browser/analyses/view.py | 3 ++- bika/lims/tests/doctests/API.rst | 31 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/bika/lims/api/__init__.py b/bika/lims/api/__init__.py index 17741e1069..b93fff698c 100644 --- a/bika/lims/api/__init__.py +++ b/bika/lims/api/__init__.py @@ -1376,3 +1376,21 @@ def to_display_list(pairs, sort_by="key", allow_empty=True): dl = dl.sortedByValue() return dl + + +def text_to_html(text, wrap="p", encoding="utf8"): + """Convert `\n` sequences in the text to HTML `\n` + + :param text: Plain text to convert + :param wrap: Toggle to wrap the text in a + :returns: HTML converted and encoded text + """ + # handle text internally as unicode + text = safe_unicode(text) + # replace newline characters with HTML entities + html = text.replace("\n", "
") + if wrap: + html = u"<{tag}>{html}".format( + tag=wrap, html=html) + # return encoded html + return html.encode(encoding) diff --git a/bika/lims/browser/analyses/view.py b/bika/lims/browser/analyses/view.py index dee675bea0..94040a8215 100644 --- a/bika/lims/browser/analyses/view.py +++ b/bika/lims/browser/analyses/view.py @@ -1212,7 +1212,8 @@ def _folder_item_remarks(self, analysis_brain, item): """ if self.analysis_remarks_enabled(): - item["Remarks"] = analysis_brain.getRemarks + remarks = analysis_brain.getRemarks + item["Remarks"] = api.text_to_html(remarks) if self.is_analysis_edition_allowed(analysis_brain): item["allow_edit"].extend(["Remarks"]) diff --git a/bika/lims/tests/doctests/API.rst b/bika/lims/tests/doctests/API.rst index a969cdf2f3..11694a3590 100644 --- a/bika/lims/tests/doctests/API.rst +++ b/bika/lims/tests/doctests/API.rst @@ -1601,3 +1601,34 @@ It can be sorted either by key or by value: >>> api.to_display_list(pairs, sort_by="value") + + +Converting a text to HTML +------------------------- + +This function converts newline (`\n`) escape sequences in plain text to `
` +tags for HTML rendering. + +The function can handle plain texts: + + >>> text = "First\r\nSecond\r\nThird" + >>> api.text_to_html(text) + '

First\r
Second\r
Third

' + +Unicodes texts work as well: + + >>> text = u"Ä\r\nÖ\r\nÜ" + >>> api.text_to_html(text) + '

\xc3\x83\xc2\x84\r
\xc3\x83\xc2\x96\r
\xc3\x83\xc2\x9c

' + +The outer `

` wrap can be also omitted: + + >>> text = "One\r\nTwo" + >>> api.text_to_html(text, wrap=None) + 'One\r
Two' + +Or changed to another tag: + + >>> text = "One\r\nTwo" + >>> api.text_to_html(text, wrap="div") + '

One\r
Two
' From 6f98a611bfe7428c84f73cad7522b3a2e87098ee Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Mon, 7 Oct 2019 13:28:43 +0200 Subject: [PATCH 2/4] Leave empty texts unchanged --- bika/lims/api/__init__.py | 2 ++ bika/lims/tests/doctests/API.rst | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/bika/lims/api/__init__.py b/bika/lims/api/__init__.py index b93fff698c..aa4d63a2d9 100644 --- a/bika/lims/api/__init__.py +++ b/bika/lims/api/__init__.py @@ -1385,6 +1385,8 @@ def text_to_html(text, wrap="p", encoding="utf8"): :param wrap: Toggle to wrap the text in a :returns: HTML converted and encoded text """ + if not text: + return "" # handle text internally as unicode text = safe_unicode(text) # replace newline characters with HTML entities diff --git a/bika/lims/tests/doctests/API.rst b/bika/lims/tests/doctests/API.rst index 11694a3590..e6f4b28a8d 100644 --- a/bika/lims/tests/doctests/API.rst +++ b/bika/lims/tests/doctests/API.rst @@ -1632,3 +1632,9 @@ Or changed to another tag: >>> text = "One\r\nTwo" >>> api.text_to_html(text, wrap="div") '
One\r
Two
' + +Empty strings are returned unchanged: + + >>> text = "" + >>> api.text_to_html(text, wrap="div") + '' From 7269939c4627f44718aab467eaaeb5d0de0e9d3b Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Mon, 7 Oct 2019 13:37:39 +0200 Subject: [PATCH 3/4] Remove outer wrap --- bika/lims/browser/analyses/view.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bika/lims/browser/analyses/view.py b/bika/lims/browser/analyses/view.py index 94040a8215..197322c201 100644 --- a/bika/lims/browser/analyses/view.py +++ b/bika/lims/browser/analyses/view.py @@ -1212,11 +1212,14 @@ def _folder_item_remarks(self, analysis_brain, item): """ if self.analysis_remarks_enabled(): - remarks = analysis_brain.getRemarks - item["Remarks"] = api.text_to_html(remarks) + item["Remarks"] = analysis_brain.getRemarks if self.is_analysis_edition_allowed(analysis_brain): item["allow_edit"].extend(["Remarks"]) + else: + # render HTMLified text in readonly mode + item["Remarks"] = api.text_to_html( + analysis_brain.getRemarks, wrap=None) def _append_html_element(self, item, element, html, glue=" ", after=True): From 12d5bd38c113c0e95f7c92159284ed503f8985ce Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Mon, 7 Oct 2019 13:37:50 +0200 Subject: [PATCH 4/4] Changelog updated --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index ad9aaf0308..4d605ac9eb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -17,6 +17,7 @@ Changelog **Changed** +- #1451 Render Analysis Remarks in Listings as HTML - #1445 Allow formatted HTML in the other rejection reasons - #1428 Publish verified partitions - #1429 Add2: Do not set template values on already filled fields