Skip to content

Commit

Permalink
Resolves GeoNode#6925: fix thesauri label in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Feb 23, 2021
1 parent 11f40ba commit f613abc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 58 deletions.
3 changes: 1 addition & 2 deletions geonode/base/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ 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.
lang = settings.THESAURUS_DEFAULT_LANG if hasattr(settings, "THESAURUS_DEFAULT_LANG") else "en"
return obj.keyword.filter(lang=lang).first().label
return obj.keyword.first().label
62 changes: 37 additions & 25 deletions geonode/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from django.core import validators
from django.db.models import Prefetch, Q
from django.forms import ModelForm, models
from django.forms.fields import ChoiceField
from django.forms.fields import ChoiceField, MultipleChoiceField
from django.forms.utils import flatatt
from django.utils.encoding import force_text
from django.utils.html import format_html
Expand All @@ -39,7 +39,7 @@
from modeltranslation.forms import TranslationModelForm
from taggit.forms import TagField
from tinymce.widgets import TinyMCE

from django.contrib.admin.utils import flatten
from geonode.base.enumerations import ALL_LANGUAGES
from geonode.base.models import (CuratedThumbnail, HierarchicalKeyword,
License, Region, ResourceBase, Thesaurus,
Expand All @@ -48,7 +48,7 @@
from geonode.base.widgets import TaggitSelect2Custom
from geonode.documents.models import Document
from geonode.layers.models import Layer

from django.utils.translation import get_language
from .fields import MultiThesauriField

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -317,9 +317,9 @@ class Meta:
class ThesaurusAvailableForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ThesaurusAvailableForm, self).__init__(*args, **kwargs)
lang = settings.THESAURUS_DEFAULT_LANG if hasattr(settings, "THESAURUS_DEFAULT_LANG") else "en"
lang = get_language()
for item in Thesaurus.objects.all():
tname = ThesaurusLabel.objects.values_list("label", flat=True).filter(id=item.id).filter(lang=lang)
tname = self._get_thesauro_title_label(item, lang)
if item.card_max == 0:
continue
elif item.card_max == 1 and item.card_min == 0:
Expand All @@ -338,30 +338,42 @@ def cleanx(self, x):
for y in value:
cleaned_values.append(y.id)
elif value:
cleaned_values.append(value.id)
return ThesaurusKeyword.objects.filter(id__in=cleaned_values)

@staticmethod
def _define_multifield(item, required, tname, lang):
return MultiThesauriField(
ThesaurusKeyword.objects.prefetch_related(
Prefetch(
"keyword",
queryset=ThesaurusKeywordLabel.objects.filter(keyword__thesaurus_id=item.id).filter(lang=lang),
)
),
widget=autocomplete.ModelSelect2Multiple(url=f"/base/thesaurus_available/?sysid={item.id}&lang={lang}"),
label=_(f"{tname[0] if len(tname) > 0 else item.title}"),
cleaned_values.append(value)
return ThesaurusKeyword.objects.filter(id__in=flatten(cleaned_values))

def _define_multifield(self, item, required, tname, lang):
return MultipleChoiceField(
choices=self._get_thesauro_keyword_label(item, lang),
widget=autocomplete.Select2Multiple(url=f"/base/thesaurus_available/?sysid={item.id}&lang={lang}"),
label=f"{tname}",
required=required,
)

@staticmethod
def _define_choicefield(item, required, tname, lang):
return models.ModelChoiceField(
label=f"{tname[0] if len(tname) > 0 else item.title}",
def _define_choicefield(self, item, required, tname, lang):
return models.ChoiceField(
label=f"{tname}",
required=required,
queryset=ThesaurusKeywordLabel.objects.filter(keyword__thesaurus_id=item.id).filter(lang=lang),
)
choices=self._get_thesauro_keyword_label(item, lang))

@staticmethod
def _get_thesauro_keyword_label(item, lang):
qs_local = []
qs_non_local = []
for key in ThesaurusKeyword.objects.filter(thesaurus_id=item.id):
label = ThesaurusKeywordLabel.objects.filter(keyword=key).filter(lang=lang)
if label.exists():
qs_local.append((label.get().keyword.id, label.get().label))
else:
qs_non_local.append((key.id, key.alt_label))

return qs_non_local + qs_local

@staticmethod
def _get_thesauro_title_label(item, lang):
tname = ThesaurusLabel.objects.values_list("label", flat=True).filter(thesaurus=item).filter(lang=lang)
if not tname:
return Thesaurus.objects.get(id=item.id).title
return tname.first()


class ResourceBaseDateTimePicker(DateTimePicker):
Expand Down
8 changes: 1 addition & 7 deletions geonode/base/templatetags/thesaurus.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import template
from geonode.base.models import Thesaurus, ThesaurusKeywordLabel
from geonode.base.models import Thesaurus, ThesaurusKeyword
from django.conf import settings

register = template.Library()
Expand All @@ -10,12 +10,6 @@ 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
Expand Down
24 changes: 5 additions & 19 deletions geonode/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
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,
)
Expand Down Expand Up @@ -894,24 +893,16 @@ def test_complex_tags_in_attribute(self):

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",
)
fixtures = [
"test_thesaurus.json"
]

def setUp(self):
self.sut = Thesaurus(
identifier="foo_name",
title="Mocked Title",
title="GEMET - INSPIRE themes, version 1.0",
date="2018-05-23T10:25:56",
description="Mocked Title",
description="GEMET - INSPIRE themes, version 1.0",
slug="",
about="http://inspire.ec.europa.eu/theme",
)
Expand All @@ -922,11 +913,6 @@ def test_get_unique_thesaurus_list(self):
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)
Expand Down
18 changes: 14 additions & 4 deletions geonode/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


# Geonode functionality
from django.db.models.query import Prefetch
from django.shortcuts import render
from django.conf import settings
from django.http import HttpResponse
Expand Down Expand Up @@ -53,7 +54,7 @@
from geonode.base.models import (
Region,
ResourceBase,
HierarchicalKeyword,
HierarchicalKeyword, ThesaurusKeyword,
ThesaurusKeywordLabel
)

Expand Down Expand Up @@ -324,19 +325,28 @@ def get_queryset(self):
tid = self.request.GET.get("sysid")
lang = self.request.GET.get("lang")

qs = ThesaurusKeywordLabel.objects.filter(keyword__thesaurus__id=tid).filter(lang=lang).order_by('id')
return qs
qs_local = []
qs_non_local = []
for key in ThesaurusKeyword.objects.filter(thesaurus_id=tid):
label = ThesaurusKeywordLabel.objects.filter(keyword=key).filter(lang=lang)
if label.exists():
qs_local.append(label.get())
else:
qs_non_local.append(key)

return qs_non_local + qs_local

def get_results(self, context):
return [
{
'id': self.get_result_value(result.keyword),
'id': str(result.keyword.pk) if isinstance(result, ThesaurusKeywordLabel) else str(result.pk),
'text': self.get_result_label(result),
'selected_text': self.get_selected_result_label(result),
} for result in context['object_list']
]



class OwnerRightsRequestView(LoginRequiredMixin, FormView):
template_name = 'owner_rights_request.html'
form_class = OwnerRightsRequestForm
Expand Down
2 changes: 1 addition & 1 deletion geonode/catalogue/templates/catalogue/full_metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@
{% for keyword in layer.tkeywords.all %}
{% if keyword.thesaurus.id == thesaurus_id %}
<gmd:keyword>
<gco:CharacterString>{{keyword|get_keyword_label}}</gco:CharacterString>
<gco:CharacterString>{{keyword.alt_label}}</gco:CharacterString>
</gmd:keyword>
{% endif %}
{% endfor %}
Expand Down

0 comments on commit f613abc

Please sign in to comment.