Skip to content

Commit 8be1760

Browse files
[Fixes #12749] Sanitize uploaded layers that start with a numeric character (#13000)
1 parent 80e2f5f commit 8be1760

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

geonode/upload/handlers/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ def fixup_name(self, name):
193193
We use replace because it looks to be one of the fasted options:
194194
https://stackoverflow.com/questions/3411771/best-way-to-replace-multiple-characters-in-a-string
195195
"""
196+
prefix = name[0]
197+
if prefix.isnumeric():
198+
name = name.replace(name[0], "_")
196199
return (
197200
name.lower()
198201
.replace("-", "_")

geonode/upload/handlers/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
drop_dynamic_model_schema,
2626
should_be_imported,
2727
)
28+
from geonode.upload.handlers.base import BaseHandler
2829

2930

3031
class TestHandlersUtils(TestCase):
@@ -81,3 +82,13 @@ def test_drop_dynamic_model_schema(self):
8182
drop_dynamic_model_schema(schema_model=_model_schema)
8283

8384
self.assertFalse(ModelSchema.objects.filter(name="model_schema").exists())
85+
86+
def test_fixup_name_replace_digits_with_underscore(self):
87+
"""
88+
Ref https://github.com/GeoNode/geonode/issues/12749
89+
If the layer start with a digit, we should translate as a string
90+
"""
91+
layer_name = "1layername"
92+
expected_name = "_layername"
93+
actual = BaseHandler().fixup_name(layer_name)
94+
self.assertEqual(expected_name, actual)

0 commit comments

Comments
 (0)