Skip to content

Commit

Permalink
Resolves GeoNode#6925: change metadata template to let it reads the n…
Browse files Browse the repository at this point in the history
…ew thesaurus information as template tags, add tests for new template tags
  • Loading branch information
mattiagiupponi committed Feb 5, 2021
1 parent f409342 commit f2f433e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 11 deletions.
9 changes: 0 additions & 9 deletions geonode/base/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,3 @@ def label_from_instance(self, obj):
# code. The hard-coded language is currently used throughout
# geonode.
return obj.keyword.filter(lang='en').first().label


class MultiThesauriXField(forms.MultipleChoiceField):

def label_from_instance(self, obj):
# Note: Not using .get() because filter()[0] is used in original
# code. The hard-coded language is currently used throughout
# geonode.
return obj.keyword.filter(lang='en').first().label
23 changes: 23 additions & 0 deletions geonode/base/templatetags/thesaurus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django import template
from geonode.base.models import Thesaurus, ThesaurusKeywordLabel
from django.conf import settings

register = template.Library()


@register.filter
def get_unique_thesaurus_set(thesaurus_from_keyword):
return set(thesaurus_from_keyword.values_list('thesaurus', flat=True))

@register.filter
def get_keyword_label(keyword):
lang = 'en' if not hasattr(settings, 'THESAURUS_DEFAULT_LANG') else settings.THESAURUS_DEFAULT_LANG
return ThesaurusKeywordLabel.objects.filter(keyword=keyword).filter(lang=lang).first().label

@register.filter
def get_thesaurus_title(thesaurus_id):
return Thesaurus.objects.get(id=thesaurus_id).title

@register.filter
def get_thesaurus_date(thesaurus_id):
return Thesaurus.objects.get(id=thesaurus_id).date
55 changes: 54 additions & 1 deletion geonode/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from geonode.services.models import Service
from geonode.tests.base import GeoNodeBaseTestSupport
from geonode.base.models import (
ResourceBase, MenuPlaceholder, Menu, MenuItem, Configuration, TopicCategory
ResourceBase, MenuPlaceholder, Menu, MenuItem, Configuration, Thesaurus, ThesaurusKeyword, TopicCategory
)
from django.template import Template, Context
from django.contrib.auth import get_user_model
Expand All @@ -43,13 +43,16 @@
from geonode.base.middleware import ReadOnlyMiddleware, MaintenanceMiddleware
from geonode.base.models import CuratedThumbnail
from geonode.base.templatetags.base_tags import get_visibile_resources
from geonode.base.templatetags.thesaurus import get_unique_thesaurus_set, get_keyword_label, \
get_thesaurus_title, get_thesaurus_date
from geonode.base.templatetags.user_messages import show_notification
from geonode import geoserver
from geonode.decorators import on_ogc_backend

from django.core.files import File
from django.core.management import call_command
from django.core.management.base import CommandError
from unittest.mock import patch

test_image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))

Expand Down Expand Up @@ -834,3 +837,53 @@ def test_complex_tags_in_attribute(self):
r = ResourceBase()
filtered_value = r._remove_html_tags(tagged_value)
self.assertEqual(filtered_value, attribute_target_value)


class TestTagThesaurus(TestCase):
# loading test thesausurs
@classmethod
def setUpTestData(cls):
from django.core import management
from os.path import dirname, abspath
management.call_command(
"load_thesaurus",
file=f"{dirname(dirname(abspath(__file__)))}/tests/data/thesaurus.rdf",
name="foo_name",
stdout="out",
)

def setUp(self):
self.sut = Thesaurus(
identifier="foo_name",
title="Mocked Title",
date="2018-05-23T10:25:56",
description="Mocked Title",
slug="",
about="http://inspire.ec.europa.eu/theme",
)
self.tkeywords = ThesaurusKeyword.objects.all()


def test_get_unique_thesaurus_list(self):
tid = self.__get_last_thesaurus().id
actual = get_unique_thesaurus_set(self.tkeywords)
self.assertSetEqual({tid}, actual)

@patch.dict('os.environ', {"THESAURUS_DEFAULT_LANG": "en"})
def test_get_keyword_label(self):
actual = get_keyword_label(self.tkeywords[0])
self.assertEqual("Addresses", actual)

def test_get_thesaurus_title(self):
tid = self.__get_last_thesaurus().id
actual = get_thesaurus_title(tid)
self.assertEqual(self.sut.title, actual)

def test_get_thesaurus_date(self):
tid = self.__get_last_thesaurus().id
actual = get_thesaurus_date(tid)
self.assertEqual(self.sut.date, actual)

@staticmethod
def __get_last_thesaurus():
return Thesaurus.objects.all().order_by("-id")[0]
35 changes: 35 additions & 0 deletions geonode/catalogue/templates/catalogue/full_metadata.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load thesaurus %}
<gmd:MD_Metadata xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml" xmlns:gmx="http://www.isotc211.org/2005/gmx" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://www.isotc211.org/2005/gmd/gmd.xsd">
<gmd:fileIdentifier>
<gco:CharacterString>{{layer.uuid}}</gco:CharacterString>
Expand Down Expand Up @@ -350,6 +351,40 @@
</gmd:MD_Keywords>
</gmd:descriptiveKeywords>
{% endif %}
{% if layer.tkeywords %}
{% for thesaurus_id in layer.tkeywords|get_unique_thesaurus_set %}
<gmd:descriptiveKeywords>
<gmd:MD_Keywords>
{% for keyword in layer.tkeywords.all %}
{% if keyword.thesaurus.id == thesaurus_id %}
<gmd:keyword>
<gco:CharacterString>{{keyword|get_keyword_label}}</gco:CharacterString>
</gmd:keyword>
{% endif %}
{% endfor %}
<gmd:thesaurusName>
<gmd:CI_Citation>
<gmd:title>
<gco:CharacterString>{{thesaurus_id|get_thesaurus_title}}</gco:CharacterString>
</gmd:title>
<gmd:date>
<gmd:CI_Date>
<gmd:date>
<gco:Date>{{thesaurus_id|get_thesaurus_date}}</gco:Date>
</gmd:date>
<gmd:dateType>
<gmd:CI_DateTypeCode
codeList="http://standards.iso.org/iso/19139/resources/gmxCodelists.xml#CI_DateTypeCode"
codeListValue="publication">pubblicazione</gmd:CI_DateTypeCode>
</gmd:dateType>
</gmd:CI_Date>
</gmd:date>
</gmd:CI_Citation>
</gmd:thesaurusName>
</gmd:MD_Keywords>
</gmd:descriptiveKeywords>
{% endfor %}
{% endif %}
{% for region in layer.regions.all %}
<gmd:descriptiveKeywords>
<gmd:MD_Keywords>
Expand Down
5 changes: 4 additions & 1 deletion geonode/layers/templates/layers/layer_metadata.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h2 class="page-title">{% trans "Metadata" %}&nbsp;{% blocktrans with layer.titl
Some of your original metadata may have been lost.{% endblocktrans %}</p>
{% endif %}

{% if layer_form.errors or attribute_form.errors or category_form.errors or author_form.errors or poc.errors %}
{% if layer_form.errors or attribute_form.errors or category_form.errors or author_form.errors or poc.errors or tkeywords_form.errors %}
<p class="bg-danger">{% blocktrans %}Error updating metadata. Please check the following fields: {% endblocktrans %}</p>
<ul class="bg-danger">
{% if author_form.errors %}
Expand All @@ -69,6 +69,9 @@ <h2 class="page-title">{% trans "Metadata" %}&nbsp;{% blocktrans with layer.titl
{% if category_form.errors %}
<li>{{ category_form.errors.as_ul }}</li>
{% endif %}
{% if tkeywords_form.errors %}
<li>{{ tkeywords_form.errors.as_ul }}</li>
{% endif %}
</ul>
{% endif %}

Expand Down
14 changes: 14 additions & 0 deletions geonode/people/migrations/0031_merge_20210205_0824.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.2.16 on 2021-02-05 08:24

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('people', '0030_auto_20210118_1112'),
('people', '0030_auto_20210201_0953'),
]

operations = [
]

0 comments on commit f2f433e

Please sign in to comment.