Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render Analysis Remarks in Listings as HTML #1451

Merged
merged 4 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions bika/lims/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,3 +1376,23 @@ 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
"""
if not text:
return ""
# handle text internally as unicode
text = safe_unicode(text)
# replace newline characters with HTML entities
html = text.replace("\n", "<br/>")
if wrap:
html = u"<{tag}>{html}</{tag}>".format(
tag=wrap, html=html)
# return encoded html
return html.encode(encoding)
4 changes: 4 additions & 0 deletions bika/lims/browser/analyses/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,10 @@ def _folder_item_remarks(self, analysis_brain, item):

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="&nbsp;",
after=True):
Expand Down
37 changes: 37 additions & 0 deletions bika/lims/tests/doctests/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1601,3 +1601,40 @@ It can be sorted either by key or by value:

>>> api.to_display_list(pairs, sort_by="value")
<DisplayList [('b', 10), ('a', 100), ('', '')] at ...>


Converting a text to HTML
-------------------------

This function converts newline (`\n`) escape sequences in plain text to `<br/>`
tags for HTML rendering.

The function can handle plain texts:

>>> text = "First\r\nSecond\r\nThird"
>>> api.text_to_html(text)
'<p>First\r<br/>Second\r<br/>Third</p>'

Unicodes texts work as well:

>>> text = u"Ä\r\nÖ\r\nÜ"
>>> api.text_to_html(text)
'<p>\xc3\x83\xc2\x84\r<br/>\xc3\x83\xc2\x96\r<br/>\xc3\x83\xc2\x9c</p>'

The outer `<p>` wrap can be also omitted:

>>> text = "One\r\nTwo"
>>> api.text_to_html(text, wrap=None)
'One\r<br/>Two'

Or changed to another tag:

>>> text = "One\r\nTwo"
>>> api.text_to_html(text, wrap="div")
'<div>One\r<br/>Two</div>'

Empty strings are returned unchanged:

>>> text = ""
>>> api.text_to_html(text, wrap="div")
''