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

[Backport to 3.1.x #7105] Remove HTML tags from metadata detail #7116

Merged
merged 1 commit into from
Mar 19, 2021
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
12 changes: 9 additions & 3 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import re
import math
import uuid
import html
import logging
import traceback

Expand All @@ -40,7 +41,7 @@
from django.contrib.contenttypes.models import ContentType
from django.contrib.staticfiles.templatetags import staticfiles
from django.core.files.storage import default_storage as storage

from django.utils.html import strip_tags
from mptt.models import MPTTModel, TreeForeignKey

from PIL import Image, ImageOps
Expand Down Expand Up @@ -896,11 +897,16 @@ def __str__(self):
return "{0}".format(self.title)

def _remove_html_tags(self, attribute_str):
_attribute_str = attribute_str
try:
pattern = re.compile('<.*?>')
return re.sub(pattern, '', attribute_str)
_attribute_str = html.unescape(
re.sub(pattern, '', attribute_str).replace('\n', ' ').replace('\r', '').strip())
except Exception:
return attribute_str
if attribute_str:
_attribute_str = html.unescape(
attribute_str.replace('\n', ' ').replace('\r', '').strip())
return strip_tags(_attribute_str)

@property
def raw_abstract(self):
Expand Down
13 changes: 13 additions & 0 deletions geonode/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,19 @@ def test_complex_tags_in_attribute(self):
filtered_value = r._remove_html_tags(tagged_value)
self.assertEqual(filtered_value, attribute_target_value)

def test_converted_html_in_tags_with_char_references(self):
tagged_value = """<p>&lt;p&gt;Abstract value &amp; some text&lt;/p&gt;</p>"""
attribute_target_value = """Abstract value & some text"""
r = ResourceBase()
filtered_value = r._remove_html_tags(tagged_value)
self.assertEqual(filtered_value, attribute_target_value)

def test_converted_html_in_tags_with_with_multiple_tags(self):
tagged_value = """<p><p><p><p>Abstract value &amp; some text</p></p></p></p>"""
attribute_target_value = """Abstract value & some text"""
r = ResourceBase()
filtered_value = r._remove_html_tags(tagged_value)
self.assertEqual(filtered_value, attribute_target_value)

class TestTagThesaurus(TestCase):
# loading test thesausurs
Expand Down
10 changes: 5 additions & 5 deletions geonode/catalogue/templates/geonode_metadata_full.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h2 class="page-title">{{ resource.title }}</h2>
<dd>{{resource.date}}, {% trans resource.date_type|title %} </dd>

<dt>{% trans "Abstract" %}</dt>
<dd>{{resource.abstract}}</dd>
<dd>{{resource.raw_abstract}}</dd>

<dt>{% trans "Edition" %}</dt>
{% if resource.edition %}
Expand All @@ -79,7 +79,7 @@ <h2 class="page-title">{{ resource.title }}</h2>

<dt>{% trans "Purpose" %}</dt>
{% if resource.purpose %}
<dd>{{resource.purpose}}</dd>
<dd>{{resource.raw_purpose}}</dd>
{% else %}
<dd> -- </dd>
{% endif %}
Expand All @@ -93,7 +93,7 @@ <h2 class="page-title">{{ resource.title }}</h2>

<dt>{% trans "Restrictions" %}</dt>
<dd>{{resource.restriction_code_type}}</dd>
<dd>{{resource.constraints_other}}</dd>
<dd>{{resource.raw_constraints_other}}</dd>

<dt>{% trans "License" %}</dt>
<dd>{{resource.license}}</dd>
Expand All @@ -117,11 +117,11 @@ <h2 class="page-title">{{ resource.title }}</h2>


<dt>{% trans "Supplemental Information" %}</dt>
<dd>{{resource.supplemental_information}}</dd>
<dd>{{resource.raw_supplemental_information}}</dd>

<dt>{% trans "Data Quality" %} </dt>
{% if resource.data_quality_statement %}
<dd>{{resource.data_quality_statement}}</dd>
<dd>{{resource.raw_data_quality_statement}}</dd>
{% else %}
<dd> -- </dd>
{% endif %}
Expand Down