Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: translated spatial coverage names #704

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions backend/apps/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,36 @@ def spatial_coverage(self) -> list[str]:
]
return sorted(list(get_spatial_coverage(resources)))

@property
def spatial_coverage_name_pt(self) -> list[str]:
"""Union spatial coverage of all related resources"""
resources = [
*self.tables.all(),
*self.raw_data_sources.all(),
*self.information_requests.all(),
]
return sorted(list(get_spatial_coverage_name(resources, locale = 'pt')))

@property
def spatial_coverage_name_en(self) -> list[str]:
"""Union spatial coverage of all related resources"""
resources = [
*self.tables.all(),
*self.raw_data_sources.all(),
*self.information_requests.all(),
]
return sorted(list(get_spatial_coverage_name(resources, locale = 'en')))

@property
def spatial_coverage_name_es(self) -> list[str]:
"""Union spatial coverage of all related resources"""
resources = [
*self.tables.all(),
*self.raw_data_sources.all(),
*self.information_requests.all(),
]
return sorted(list(get_spatial_coverage_name(resources, locale = 'es')))

@property
def entities(self) -> list[dict]:
"""Entity of all related resources"""
Expand Down Expand Up @@ -1030,6 +1060,21 @@ def spatial_coverage(self) -> list[str]:
"""Unique list of areas across all coverages"""
return sorted(list(get_spatial_coverage([self])))

@property
def spatial_coverage_name_pt(self) -> list[str]:
"""Union spatial coverage of all related resources"""
return get_spatial_coverage_name([self], locale = 'pt')

@property
def spatial_coverage_name_en(self) -> list[str]:
"""Union spatial coverage of all related resources"""
return get_spatial_coverage_name([self], locale = 'en')

@property
def spatial_coverage_name_es(self) -> list[str]:
"""Union spatial coverage of all related resources"""
return get_spatial_coverage_name([self], locale = 'es')

@property
def neighbors(self) -> list[dict]:
"""Similiar tables and columns without filters"""
Expand Down Expand Up @@ -1405,6 +1450,30 @@ def spatial_coverage(self) -> list[str]:
return get_spatial_coverage([self.table])
return coverage

@property
def spatial_coverage_name_pt(self) -> list[str]:
"""Union spatial coverage of all related resources"""
coverage = get_spatial_coverage_name([self], locale = 'pt')
if not coverage:
coverage = get_spatial_coverage_name([self.table], locale = 'pt')
return coverage

@property
def spatial_coverage_name_en(self) -> list[str]:
"""Union spatial coverage of all related resources"""
coverage = get_spatial_coverage_name([self], locale = 'en')
if not coverage:
coverage = get_spatial_coverage_name([self.table], locale = 'en')
return coverage

@property
def spatial_coverage_name_es(self) -> list[str]:
"""Union spatial coverage of all related resources"""
coverage = get_spatial_coverage_name([self], locale = 'es')
if not coverage:
coverage = get_spatial_coverage_name([self.table], locale = 'es')
return coverage

@property
def dir_column(self):
"""Column of directory table and column"""
Expand Down Expand Up @@ -2152,3 +2221,63 @@ def get_spatial_coverage(resources: list) -> list:
filtered_areas.add(area)

return sorted(list(filtered_areas))

def get_spatial_coverage_name(resources: list, locale: str = 'pt') -> list:
"""Get spatial coverage of resources by returning unique area names in the specified locale,
keeping only the highest level in each branch

Args:
resources: List of resources to get coverage from
locale: Language code ('pt', 'en', etc). Defaults to 'pt'

For example:
- If areas = [br_mg_3100104, br_mg_3100104] -> returns [Belo Horizonte]
- If areas = [br_mg_3100104, br_sp_3500105] -> returns [Belo Horizonte, São Paulo]
- If areas = [br_mg, us_ny, us] -> returns [Minas Gerais, United States] (en)
returns [Minas Gerais, Estados Unidos] (pt)
- If areas = [br_mg, world, us] -> returns [World] (en)
returns [Mundo] (pt)
- If resources have no areas -> returns empty list
"""
# Translation mapping for special cases
translations = {
'world': {
'pt': 'Mundo',
'en': 'World',
'es': 'Mundo',
}
}

# Collect all unique areas (both slug and name) across resources
all_areas = {}
for resource in resources:
for coverage in resource.coverages.all():
if coverage.area:
# Get localized name using getattr, fallback to default name if not found
localized_name = getattr(coverage.area, f'name_{locale}', None) or coverage.area.name
all_areas[coverage.area.slug] = localized_name

if not all_areas:
return []

# If 'world' is present, it encompasses everything
if 'world' in all_areas:
return [translations['world'].get(locale, translations['world']['pt'])]

# Filter out areas that have a parent in the set
filtered_areas = set()
for area_slug in all_areas:
parts = area_slug.split('_')
is_parent_present = False

# Check if any parent path exists in all_areas
for i in range(1, len(parts)):
parent = '_'.join(parts[:i])
if parent in all_areas:
is_parent_present = True
break

if not is_parent_present:
filtered_areas.add(all_areas[area_slug])

return sorted(list(filtered_areas))
Loading