Skip to content

Commit

Permalink
PB-1157 Validate the hreflang links
Browse files Browse the repository at this point in the history
  • Loading branch information
schtibe committed Dec 12, 2024
1 parent bb977a7 commit 0a5427e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ django-admin-autocomplete-filter = "~=0.7.1"
django-pgtrigger = "~=4.11.1"
logging-utilities = "~=4.5.0"
django-environ = "*"
language-tags = "*"

[requires]
python_version = "3.12"
10 changes: 9 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions app/stac_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
from uuid import uuid4

from language_tags import tags
from multihash import encode as multihash_encode
from multihash import to_hex_string

Expand Down Expand Up @@ -161,6 +162,15 @@ class Meta:
def __str__(self):
return f'{self.rel}: {self.href}'

def save(self, *args, **kwargs) -> None:
"""Validate the hreflang"""
self.full_clean()

if self.hreflang is not None and self.hreflang != '' and not tags.check(self.hreflang):
raise ValidationError(_(", ".join([v.message for v in tags.tag(self.hreflang).errors])))

super().save(*args, **kwargs)


class LandingPage(models.Model):
# using "name" instead of "id", as "id" has a default meaning in django
Expand Down
19 changes: 19 additions & 0 deletions app/tests/tests_10/test_collections_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,22 @@ def test_read_collection_with_hreflang(self):
fr_link = link_data[-1]
self.assertEqual(de_link['hreflang'], 'de')
self.assertEqual(fr_link['hreflang'], 'fr-CH')

def test_create_collection_link_with_invalid_hreflang(self):
data = self.collection_data.get_json('put')
data['links'] = [{
'rel': 'more-info',
'href': 'http://www.meteoschweiz.ch/',
'title': 'A link to a german page',
'type': 'text/html',
'hreflang': "deUtsches_sprache"
}]

path = f'/{STAC_BASE_V}/collections/{self.collection.name}'
response = self.client.put(path, data=data, content_type="application/json")

self.assertEqual(response.status_code, 400)
content = response.json()
description = content['description'][0]
self.assertIn('Unknown code', description)
self.assertIn('Missing language', description)
19 changes: 19 additions & 0 deletions app/tests/tests_10/test_items_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,22 @@ def test_read_item_with_hreflang(self):
fr_link = link_data[-1]
self.assertEqual(de_link['hreflang'], 'de')
self.assertEqual(fr_link['hreflang'], 'fr-CH')

def test_update_item_link_with_invalid_hreflang(self):
data = self.item_data.get_json('put')
data['links'] = [{
'rel': 'more-info',
'href': 'http://www.meteoschweiz.ch/',
'title': 'A link to a german page',
'type': 'text/html',
'hreflang': "fr/ch"
}]

path = f'/{STAC_BASE_V}/collections/{self.collection.name}/items/{self.item.name}'
response = self.client.put(path, data=data, content_type="application/json")

self.assertEqual(response.status_code, 400)
content = response.json()
description = content['description'][0]
self.assertIn('Unknown code', description)
self.assertIn('Missing language', description)

0 comments on commit 0a5427e

Please sign in to comment.