Skip to content

Commit

Permalink
[Fixes: #6925] Thesauri improvements (#7047)
Browse files Browse the repository at this point in the history
* Resolves #6925: fix thesaurus keyword label, to let it change based on the language selected

* Resolves #6925: fix flake8 issues

* Resolves #6925: add thesauro title translation in facet, add new tests

* [Fixes #6925] Add new thesaurus behaviour also for maps

* [Fixes #6925] Add new thesaurus behaviour also for maps

* [Fixes #6925] Add new thesaurus behaviour for documents

* [Fixes #6925] fix flake8 issues
  • Loading branch information
mattiagiupponi authored Mar 10, 2021
1 parent 6416fea commit 2192cb0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 73 deletions.
18 changes: 9 additions & 9 deletions geonode/documents/templates/layouts/doc_panels.html
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,6 @@
</div>
</div>
<div class="col-lg-4">
<div class="col-lg-12">
<span><label for="{{ document_form.date_type|id }}">{{ document_form.keywords.label }}</label></span>
{{ document_form.keywords }}
</div>
{% if THESAURI_FILTERS %}
<div class="col-lg-12 thesauri_keywords">
{{ tkeywords_form }}
</div>
{% endif %}
<div class="col-lg-6 col-xs-12">
<span><label for="{{ document_form.date_type|id }}">{{ document_form.date_type.label }}</label></span>
<!--<p class="xxs-font-size">(Identification of when a given event occurred)</p>-->
Expand Down Expand Up @@ -357,6 +348,15 @@
{% endfor %}
</select>
</div>
<div class="col-lg-12">
<span><label for="{{ document_form.date_type|id }}">{{ document_form.keywords.label }}</label></span>
{{ document_form.keywords }}
</div>
{% if THESAURI_FILTERS %}
<div class="col-lg-12 thesauri_keywords">
{{ tkeywords_form }}
</div>
{% endif %}
</div>
</div>
</div>
Expand Down
73 changes: 46 additions & 27 deletions geonode/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import logging
import traceback
from itertools import chain
import warnings

from guardian.shortcuts import get_perms, get_objects_for_user

Expand All @@ -46,7 +47,7 @@
from geonode.people.forms import ProfileForm
from geonode.base.auth import get_or_create_token
from geonode.base.bbox_utils import BBOXHelper
from geonode.base.forms import CategoryForm, TKeywordForm
from geonode.base.forms import CategoryForm, TKeywordForm, ThesaurusAvailableForm
from geonode.base.models import (
Thesaurus,
TopicCategory)
Expand Down Expand Up @@ -373,7 +374,12 @@ def document_metadata(
category_form = CategoryForm(request.POST, prefix="category_choice_field", initial=int(
request.POST["category_choice_field"]) if "category_choice_field" in request.POST and
request.POST["category_choice_field"] else None)
tkeywords_form = TKeywordForm(request.POST)

if hasattr(settings, 'THESAURUS'):
tkeywords_form = TKeywordForm(request.POST)
else:
tkeywords_form = ThesaurusAvailableForm(request.POST, prefix='tkeywords')

else:
document_form = DocumentForm(instance=document, prefix="resource")
document_form.disable_keywords_widget_for_non_superuser(request.user)
Expand All @@ -383,31 +389,41 @@ def document_metadata(

# Keywords from THESAURUS management
doc_tkeywords = document.tkeywords.all()
tkeywords_list = ''
lang = 'en' # TODO: use user's language
if doc_tkeywords and len(doc_tkeywords) > 0:
tkeywords_ids = doc_tkeywords.values_list('id', flat=True)
if hasattr(settings, 'THESAURUS') and settings.THESAURUS:
el = settings.THESAURUS
thesaurus_name = el['name']
try:
t = Thesaurus.objects.get(identifier=thesaurus_name)
for tk in t.thesaurus.filter(pk__in=tkeywords_ids):
tkl = tk.keyword.filter(lang=lang)
if len(tkl) > 0:
tkl_ids = ",".join(
map(str, tkl.values_list('id', flat=True)))
tkeywords_list += "," + \
tkl_ids if len(
tkeywords_list) > 0 else tkl_ids
except Exception:
tb = traceback.format_exc()
logger.error(tb)

tkeywords_form = TKeywordForm(instance=document)
if hasattr(settings, 'THESAURUS') and settings.THESAURUS:
warnings.warn('The settings for Thesaurus has been moved to Model, \
this feature will be removed in next releases', DeprecationWarning)
tkeywords_list = ''
lang = 'en' # TODO: use user's language
if doc_tkeywords and len(doc_tkeywords) > 0:
tkeywords_ids = doc_tkeywords.values_list('id', flat=True)
if hasattr(settings, 'THESAURUS') and settings.THESAURUS:
el = settings.THESAURUS
thesaurus_name = el['name']
try:
t = Thesaurus.objects.get(identifier=thesaurus_name)
for tk in t.thesaurus.filter(pk__in=tkeywords_ids):
tkl = tk.keyword.filter(lang=lang)
if len(tkl) > 0:
tkl_ids = ",".join(
map(str, tkl.values_list('id', flat=True)))
tkeywords_list += "," + \
tkl_ids if len(
tkeywords_list) > 0 else tkl_ids
except Exception:
tb = traceback.format_exc()
logger.error(tb)

tkeywords_form = TKeywordForm(instance=document)
else:
tkeywords_form = ThesaurusAvailableForm(prefix='tkeywords')
# set initial values for thesaurus form
for tid in tkeywords_form.fields:
values = []
values = [keyword.id for keyword in doc_tkeywords if int(tid) == keyword.thesaurus.id]
tkeywords_form.fields[tid].initial = values

if request.method == "POST" and document_form.is_valid(
) and category_form.is_valid():
) and category_form.is_valid() and tkeywords_form.is_valid():
new_poc = document_form.cleaned_data['poc']
new_author = document_form.cleaned_data['metadata_author']
new_keywords = current_keywords if request.keyword_readonly else document_form.cleaned_data['keywords']
Expand Down Expand Up @@ -481,14 +497,17 @@ def document_metadata(
if not tkeywords_form.is_valid():
return HttpResponse(json.dumps({'message': "Invalid thesaurus keywords"}, status_code=400))

tkeywords_data = tkeywords_form.cleaned_data['tkeywords']

thesaurus_setting = getattr(settings, 'THESAURUS', None)
if thesaurus_setting:
tkeywords_data = tkeywords_form.cleaned_data['tkeywords']
tkeywords_data = tkeywords_data.filter(
thesaurus__identifier=thesaurus_setting['name']
)
document.tkeywords.set(tkeywords_data)
elif Thesaurus.objects.all().exists():
fields = tkeywords_form.cleaned_data
document.tkeywords.set(tkeywords_form.cleanx(fields))

except Exception:
tb = traceback.format_exc()
logger.error(tb)
Expand Down
18 changes: 9 additions & 9 deletions geonode/maps/templates/layouts/map_panels.html
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,6 @@
</div>
</div>
<div class="col-lg-5">
<div class="col-lg-12">
<span><label for="{{ map_form.date_type|id }}">{{ map_form.keywords.label }}</label></span>
{{ map_form.keywords }}
</div>
{% if THESAURI_FILTERS %}
<div class="col-lg-12 thesauri_keywords">
{{ tkeywords_form }}
</div>
{% endif %}
<div class="col-lg-6 col-xs-12">
<span><label for="{{ map_form.date_type|id }}">{{ map_form.date_type.label }}</label></span>
<!--<p class="xxs-font-size">(Identification of when a given event occurred)</p>-->
Expand Down Expand Up @@ -357,6 +348,15 @@
{% endfor %}
</select>
</div>
<div class="col-lg-12">
<span><label for="{{ map_form.date_type|id }}">{{ map_form.keywords.label }}</label></span>
{{ map_form.keywords }}
</div>
{% if THESAURI_FILTERS %}
<div class="col-lg-12 thesauri_keywords">
{{tkeywords_form.as_p}}
</div>
{% endif %}
</div>
</div>
</div>
Expand Down
78 changes: 50 additions & 28 deletions geonode/maps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import traceback
from urllib.parse import quote, urlsplit, urljoin
from itertools import chain
import warnings

from guardian.shortcuts import get_perms

Expand Down Expand Up @@ -59,7 +60,7 @@
check_ogc_backend)
from geonode.maps.forms import MapForm
from geonode.security.views import _perms_info_json
from geonode.base.forms import CategoryForm, TKeywordForm
from geonode.base.forms import CategoryForm, TKeywordForm, ThesaurusAvailableForm
from geonode.base.models import (
Thesaurus,
TopicCategory)
Expand Down Expand Up @@ -233,7 +234,7 @@ def map_metadata(
map_obj.add_missing_metadata_author_or_poc()
current_keywords = [keyword.name for keyword in map_obj.keywords.all()]
poc = map_obj.poc

topic_thesaurus = map_obj.tkeywords.all()
metadata_author = map_obj.metadata_author

topic_category = map_obj.category
Expand All @@ -243,7 +244,11 @@ def map_metadata(
category_form = CategoryForm(request.POST, prefix="category_choice_field", initial=int(
request.POST["category_choice_field"]) if "category_choice_field" in request.POST and
request.POST["category_choice_field"] else None)
tkeywords_form = TKeywordForm(request.POST)

if hasattr(settings, 'THESAURUS'):
tkeywords_form = TKeywordForm(request.POST)
else:
tkeywords_form = ThesaurusAvailableForm(request.POST, prefix='tkeywords')
else:
map_form = MapForm(instance=map_obj, prefix="resource")
map_form.disable_keywords_widget_for_non_superuser(request.user)
Expand All @@ -254,30 +259,43 @@ def map_metadata(
# Keywords from THESAURUS management
map_tkeywords = map_obj.tkeywords.all()
tkeywords_list = ''
lang = 'en' # TODO: use user's language
if map_tkeywords and len(map_tkeywords) > 0:
tkeywords_ids = map_tkeywords.values_list('id', flat=True)
if hasattr(settings, 'THESAURUS') and settings.THESAURUS:
el = settings.THESAURUS
thesaurus_name = el['name']
try:
t = Thesaurus.objects.get(identifier=thesaurus_name)
for tk in t.thesaurus.filter(pk__in=tkeywords_ids):
tkl = tk.keyword.filter(lang=lang)
if len(tkl) > 0:
tkl_ids = ",".join(
map(str, tkl.values_list('id', flat=True)))
tkeywords_list += "," + \
tkl_ids if len(
tkeywords_list) > 0 else tkl_ids
except Exception:
tb = traceback.format_exc()
logger.error(tb)

tkeywords_form = TKeywordForm(instance=map_obj)
# Create THESAURUS widgets
lang = 'en'
if hasattr(settings, 'THESAURUS') and settings.THESAURUS:
warnings.warn('The settings for Thesaurus has been moved to Model, \
this feature will be removed in next releases', DeprecationWarning)
tkeywords_list = ''
if map_tkeywords and len(map_tkeywords) > 0:
tkeywords_ids = map_tkeywords.values_list('id', flat=True)
if hasattr(settings, 'THESAURUS') and settings.THESAURUS:
el = settings.THESAURUS
thesaurus_name = el['name']
try:
t = Thesaurus.objects.get(identifier=thesaurus_name)
for tk in t.thesaurus.filter(pk__in=tkeywords_ids):
tkl = tk.keyword.filter(lang=lang)
if len(tkl) > 0:
tkl_ids = ",".join(
map(str, tkl.values_list('id', flat=True)))
tkeywords_list += "," + \
tkl_ids if len(
tkeywords_list) > 0 else tkl_ids
except Exception:
tb = traceback.format_exc()
logger.error(tb)

tkeywords_form = TKeywordForm(instance=map_obj)
else:
tkeywords_form = ThesaurusAvailableForm(prefix='tkeywords')
# set initial values for thesaurus form
for tid in tkeywords_form.fields:
values = []
values = [keyword.id for keyword in topic_thesaurus if int(tid) == keyword.thesaurus.id]
tkeywords_form.fields[tid].initial = values

if request.method == "POST" and map_form.is_valid(
) and category_form.is_valid():
) and category_form.is_valid() and tkeywords_form.is_valid():

new_poc = map_form.cleaned_data['poc']
new_author = map_form.cleaned_data['metadata_author']
new_keywords = current_keywords if request.keyword_readonly else map_form.cleaned_data['keywords']
Expand Down Expand Up @@ -321,7 +339,6 @@ def map_metadata(
map_obj.regions.clear()
map_obj.regions.add(*new_regions)
map_obj.category = new_category
map_obj.save(notify=True)

register_event(request, EventType.EVENT_CHANGE_METADATA, map_obj)
if not ajax:
Expand All @@ -340,18 +357,23 @@ def map_metadata(
if not tkeywords_form.is_valid():
return HttpResponse(json.dumps({'message': "Invalid thesaurus keywords"}, status_code=400))

tkeywords_data = tkeywords_form.cleaned_data['tkeywords']

thesaurus_setting = getattr(settings, 'THESAURUS', None)
if thesaurus_setting:
tkeywords_data = tkeywords_form.cleaned_data['tkeywords']
tkeywords_data = tkeywords_data.filter(
thesaurus__identifier=thesaurus_setting['name']
)
map_obj.tkeywords.set(tkeywords_data)
elif Thesaurus.objects.all().exists():
fields = tkeywords_form.cleaned_data
map_obj.tkeywords.set(tkeywords_form.cleanx(fields))

except Exception:
tb = traceback.format_exc()
logger.error(tb)

map_obj.save(notify=True)

return HttpResponse(json.dumps({'message': message}))

# - POST Request Ends here -
Expand Down

0 comments on commit 2192cb0

Please sign in to comment.