diff --git a/terraso_backend/apps/core/landscapes_views.py b/terraso_backend/apps/core/landscapes_views.py index a1d91f040..739edf60d 100644 --- a/terraso_backend/apps/core/landscapes_views.py +++ b/terraso_backend/apps/core/landscapes_views.py @@ -70,8 +70,8 @@ def to_json(self, landscape): "type": term.type, "value": { "original": term.value_original, - "en": term.value_en, - "es": term.value_es, + "eng": term.value_en, + "spa": term.value_es, }, } for term in terms diff --git a/terraso_backend/apps/core/migrations/0045_taxonomyterms_ecosystems_renamed.py b/terraso_backend/apps/core/migrations/0045_taxonomyterms_ecosystems_renamed.py new file mode 100644 index 000000000..40fa3a607 --- /dev/null +++ b/terraso_backend/apps/core/migrations/0045_taxonomyterms_ecosystems_renamed.py @@ -0,0 +1,84 @@ +# Copyright © 2023 Technology Matters +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see https://www.gnu.org/licenses/. + +from django.db import migrations + +TERMS_TYPES = [ + ("ecosystem-type", "ECOSYSTEM_TYPE"), + ("language", "LANGUAGE"), + ("livelihood", "LIVELIHOOD"), + ("commodity", "COMMODITY"), + ("organization", "ORGANIZATION"), + ("agricultural-production-method", "AGRICULTURAL_PRODUCTION_METHOD"), +] + +TERMS = { + "ECOSYSTEM_TYPE": [ + ("Deserts", "Desert", "Desierto"), + ("Savannas", "Savannah", "Sabana"), + ("Shrublands", "Shrubland", "Matorrales"), + ], +} + + +def fix_case_term_types(apps, schema_editor): + TaxonomyTerm = apps.get_model("core", "TaxonomyTerm") + Landscape = apps.get_model("core", "Landscape") + for wrong_type, correct_type in TERMS_TYPES: + wrong_terms = TaxonomyTerm.objects.filter(type=wrong_type).all() + for wrong_term in wrong_terms: + try: + correct_term = TaxonomyTerm.objects.filter( + type=correct_type, slug=wrong_term.slug + ).get() + except TaxonomyTerm.DoesNotExist: + correct_term = None + if not correct_term: + wrong_term.type = correct_type + wrong_term.save() + else: + landscapes = Landscape.objects.filter(taxonomy_terms__in=[wrong_term]).all() + for landscape in landscapes: + landscape.taxonomy_terms.remove(wrong_term) + landscape.taxonomy_terms.add(correct_term) + wrong_term.delete() + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0044_notifications"), + ] + + operations = [migrations.RunPython(fix_case_term_types)] + [ + migrations.RunSQL( + sql=[ + ( + """ + UPDATE core_taxonomyterm + SET + value_es = %s, + value_original = %s, + value_en = %s + WHERE + value_original = %s AND + type = %s + """, + [value_es, value_en, value_en, current_value, type], + ) + ], + ) + for type, values in TERMS.items() + for (current_value, value_en, value_es) in values + ] diff --git a/terraso_backend/apps/core/models/taxonomy_terms.py b/terraso_backend/apps/core/models/taxonomy_terms.py index 4d805e97d..b6b0836c1 100644 --- a/terraso_backend/apps/core/models/taxonomy_terms.py +++ b/terraso_backend/apps/core/models/taxonomy_terms.py @@ -21,12 +21,12 @@ class TaxonomyTerm(SlugModel): - TYPE_ECOSYSTEM_TYPE = "ecosystem-type" - TYPE_LANGUAGE = "language" - TYPE_LIVELIHOOD = "livelihood" - TYPE_COMMODITY = "commodity" - TYPE_ORGANIZATION = "organization" - TYPE_AGRICULTURAL_PRODUCTION_METHOD = "agricultural-production-method" + TYPE_ECOSYSTEM_TYPE = "ECOSYSTEM_TYPE" + TYPE_LANGUAGE = "LANGUAGE" + TYPE_LIVELIHOOD = "LIVELIHOOD" + TYPE_COMMODITY = "COMMODITY" + TYPE_ORGANIZATION = "ORGANIZATION" + TYPE_AGRICULTURAL_PRODUCTION_METHOD = "AGRICULTURAL_PRODUCTION_METHOD" TYPES = ( (TYPE_ECOSYSTEM_TYPE, _("Ecosystem Type")), diff --git a/terraso_backend/apps/graphql/schema/taxnomy_terms.py b/terraso_backend/apps/graphql/schema/taxnomy_terms.py index 5d4285cea..b4a276c36 100644 --- a/terraso_backend/apps/graphql/schema/taxnomy_terms.py +++ b/terraso_backend/apps/graphql/schema/taxnomy_terms.py @@ -24,6 +24,8 @@ logger = structlog.get_logger(__name__) +VALID_TYPES = [key for (key, text) in TaxonomyTerm.TYPES] + class TaxonomyTermNode(DjangoObjectType): id = graphene.ID(source="pk", required=True) @@ -42,3 +44,7 @@ class Meta: ) interfaces = (relay.Node,) connection_class = TerrasoConnection + + @classmethod + def get_queryset(cls, queryset, info): + return queryset.filter(type__in=VALID_TYPES) diff --git a/terraso_backend/tests/core/test_landscapes_views.py b/terraso_backend/tests/core/test_landscapes_views.py index b1189a468..51668bd37 100644 --- a/terraso_backend/tests/core/test_landscapes_views.py +++ b/terraso_backend/tests/core/test_landscapes_views.py @@ -127,7 +127,7 @@ def test_get_landscape_json_view_terms(client, landscape): landscape.taxonomy_terms.add( mixer.blend( TaxonomyTerm, - type=TaxonomyTerm.TYPE_ECOSYSTEM_TYPE.upper(), + type=TaxonomyTerm.TYPE_ECOSYSTEM_TYPE, value_original="Test Term", value_en="Test Term en", value_es="Test Term es", @@ -143,11 +143,11 @@ def test_get_landscape_json_view_terms(client, landscape): assert json_response["taxonomyTerms"] == [ { - "type": TaxonomyTerm.TYPE_ECOSYSTEM_TYPE.upper(), + "type": TaxonomyTerm.TYPE_ECOSYSTEM_TYPE, "value": { "original": "Test Term", - "en": "Test Term en", - "es": "Test Term es", + "eng": "Test Term en", + "spa": "Test Term es", }, } ] diff --git a/terraso_backend/tests/graphql/mutations/test_landscape_mutations.py b/terraso_backend/tests/graphql/mutations/test_landscape_mutations.py index 17302bf09..c1ceac62b 100644 --- a/terraso_backend/tests/graphql/mutations/test_landscape_mutations.py +++ b/terraso_backend/tests/graphql/mutations/test_landscape_mutations.py @@ -229,13 +229,13 @@ def test_landscapes_update_taxonomy_terms(client_query, managed_landscapes): { "language": [ { - "type": "language", + "type": "LANGUAGE", "valueOriginal": "an", "valueEn": "Aragonese", "valueEs": "Aragonés", }, { - "type": "language", + "type": "LANGUAGE", "valueOriginal": "eo", "valueEn": "Esperanto", "valueEs": "Esperanto", diff --git a/terraso_backend/tests/graphql/test_taxonomy_terms.py b/terraso_backend/tests/graphql/test_taxonomy_terms.py index 250586616..41aee7851 100644 --- a/terraso_backend/tests/graphql/test_taxonomy_terms.py +++ b/terraso_backend/tests/graphql/test_taxonomy_terms.py @@ -49,6 +49,6 @@ def test_landscape_get_one_by_type(client_query, taxonomy_terms): """ response = client_query(query) result = response.json()["data"]["taxonomyTerms"]["edges"] - language_terms = [term for term in taxonomy_terms if term.type == "language"] + language_terms = [term for term in taxonomy_terms if term.type == "LANGUAGE"] assert len(result) == len(language_terms)