Skip to content

Commit

Permalink
[Fixes #7214] Sorting Thesauri (#7225)
Browse files Browse the repository at this point in the history
Co-authored-by: Alessio Fabiani <[email protected]>
  • Loading branch information
mattiagiupponi and Alessio Fabiani authored Apr 7, 2021
1 parent 52340e4 commit ebb4153
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
6 changes: 4 additions & 2 deletions geonode/base/fixtures/test_thesaurus.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"about": "http://inspire.ec.europa.eu/theme",
"card_min": 1,
"card_max": 1,
"facet": true
"facet": true,
"order": 1
}
},{
"model": "base.thesaurus",
Expand All @@ -25,7 +26,8 @@
"about": "http://optional/theme2",
"card_min": 0,
"card_max": -1,
"facet": true
"facet": true,
"order": 0
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion geonode/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class ThesaurusAvailableForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ThesaurusAvailableForm, self).__init__(*args, **kwargs)
lang = get_language()
for item in Thesaurus.objects.all():
for item in Thesaurus.objects.all().order_by('order', 'id'):
tname = self._get_thesauro_title_label(item, lang)
if item.card_max == 0:
continue
Expand Down
18 changes: 18 additions & 0 deletions geonode/base/migrations/0058_thesaurus_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.16 on 2021-04-06 07:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('base', '0057_resourcebase_metadata_only'),
]

operations = [
migrations.AddField(
model_name='thesaurus',
name='order',
field=models.IntegerField(default=0),
),
]
1 change: 1 addition & 0 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ class Thesaurus(models.Model):
card_min = models.IntegerField(default=0)
card_max = models.IntegerField(default=-1)
facet = models.BooleanField(default=True)
order = models.IntegerField(null=False, default=0)

def __str__(self):
return str(self.identifier)
Expand Down
20 changes: 20 additions & 0 deletions geonode/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,26 @@ def test_field_class_treq_is_not_set_when_field_is_optional(self):
obj_class = required.widget.attrs.get('class')
self.assertTrue(obj_class == '')

def test_will_return_thesaurus_with_the_expected_defined_order(self):
actual = self.sut(data={"1": 1})
fields = list(actual.fields.items())
# will check if the first element of the tuple is the thesaurus_id = 2
self.assertEqual(fields[0][0], '2')
# will check if the second element of the tuple is the thesaurus_id = 1
self.assertEqual(fields[1][0], '1')

def test_will_return_thesaurus_with_the_defaul_order_as_0(self):
# Update thesaurus order to 0 in order to check if the default order by id is observed
t = Thesaurus.objects.get(identifier='inspire-theme')
t.order = 0
t.save()
actual = ThesaurusAvailableForm(data={"1": 1})
fields = list(actual.fields.items())
# will check if the first element of the tuple is the thesaurus_id = 2
self.assertEqual(fields[0][0], '1')
# will check if the second element of the tuple is the thesaurus_id = 1
self.assertEqual(fields[1][0], '2')


class TestFacets(TestCase):
def setUp(self):
Expand Down
2 changes: 1 addition & 1 deletion geonode/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
def resource_urls(request):
"""Global values to pass to templates"""
site = Site.objects.get_current()
thesaurus = Thesaurus.objects.filter(facet=True).all()
thesaurus = Thesaurus.objects.filter(facet=True).all().order_by('order', 'id')
if hasattr(settings, 'THESAURUS'):
warnings.warn(
'Thesaurus settings is going to be'
Expand Down

0 comments on commit ebb4153

Please sign in to comment.