Skip to content

Commit

Permalink
fix: taxonomy terms ecosystems renamed, lang codes for landscape expo…
Browse files Browse the repository at this point in the history
…rt (#840)
  • Loading branch information
josebui authored Sep 29, 2023
1 parent 07f8ebc commit e7d24aa
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 15 deletions.
4 changes: 2 additions & 2 deletions terraso_backend/apps/core/landscapes_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
]
12 changes: 6 additions & 6 deletions terraso_backend/apps/core/models/taxonomy_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
6 changes: 6 additions & 0 deletions terraso_backend/apps/graphql/schema/taxnomy_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
8 changes: 4 additions & 4 deletions terraso_backend/tests/core/test_landscapes_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion terraso_backend/tests/graphql/test_taxonomy_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e7d24aa

Please sign in to comment.