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

[Backport 4x][Fixes #9591] Expose supported upload file formats as configurations… #9612

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,8 @@ def get_geonode_catalogue_service():
# Extensions path to use in importing custom extensions into geonode
MAPSTORE_EXTENSIONS_FOLDER_PATH = '/static/mapstore/extensions/'

# Supported Dataset file types for uploading Datasets. This setting is being from from the client

# -- END Client Hooksets Setup

SERVICE_UPDATE_INTERVAL = 0
Expand Down Expand Up @@ -2152,3 +2154,54 @@ def get_geonode_catalogue_service():
'''

SIZE_RESTRICTED_FILE_UPLOAD_ELEGIBLE_URL_NAMES = ("data_upload", "uploads-upload", "document_upload",)

SUPPORTED_DATASET_FILE_TYPES = [
{
"id": "shp",
"label": "ESRI Shapefile",
"format": "vector",
"ext": ["shp"],
"requires": ["shp", "prj", "dbf", "shx"],
"optional": ["xml", "sld"]
},
{
"id": "tiff",
"label": "GeoTIFF",
"format": "raster",
"ext": ["tiff", "tif"],
"mimeType": ["image/tiff"],
"optional": ["xml", "sld"]
},
{
"id": "csv",
"label": "Comma Separated Value (CSV)",
"format": "vector",
"ext": ["csv"],
"mimeType": ["text/csv"],
"optional": ["xml", "sld"]
},
{
"id": "zip",
"label": "Zip Archive",
"format": "archive",
"ext": ["zip"],
"mimeType": ["application/zip"],
"optional": ["xml", "sld"]
},
{
"id": "xml",
"label": "XML Metadata File",
"format": "metadata",
"ext": ["xml"],
"mimeType": ["application/json"],
"needsFiles": ["shp", "prj", "dbf", "shx", "csv", "tiff", "zip", "sld"]
},
{
"id": "sld",
"label": "Styled Layer Descriptor (SLD)",
"format": "metadata",
"ext": ["sld"],
"mimeType": ["application/json"],
"needsFiles": ["shp", "prj", "dbf", "shx", "csv", "tiff", "zip", "xml"]
}
]
54 changes: 53 additions & 1 deletion geonode/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import os
import copy
import shutil
from unittest import TestCase
import zipfile
import tempfile
from django.test import override_settings

from osgeo import ogr
from unittest.mock import patch
Expand All @@ -35,7 +37,8 @@
from geonode.geoserver.helpers import set_attributes
from geonode.tests.base import GeoNodeBaseTestSupport
from geonode.br.management.commands.utils.utils import ignore_time
from geonode.utils import copy_tree, fixup_shp_columnnames, unzip_file
from geonode.utils import copy_tree, fixup_shp_columnnames, get_supported_file_types, unzip_file
from geonode import settings


class TestCopyTree(GeoNodeBaseTestSupport):
Expand Down Expand Up @@ -219,3 +222,52 @@ def test_set_attributes_creates_attributes(self):
# The name and type should be set as provided by attribute map
for a in _l.attributes:
self.assertIn([a.attribute, a.attribute_type], expected_results)


class TestSupportedTypes(TestCase):

def setUp(self):
self.replaced = [
{
"id": "shp",
"label": "Replaced type",
"format": "vector",
"ext": ["shp"],
"requires": ["shp", "prj", "dbf", "shx"],
"optional": ["xml", "sld"]
},
]

@override_settings(ADDITIONAL_DATASET_FILE_TYPES=[
{
"id": "dummy_type",
"label": "Dummy Type",
"format": "dummy",
"ext": ["dummy"]
},
])
def test_should_append_additional_type_if_config_is_provided(self):
prev_count = len(settings.SUPPORTED_DATASET_FILE_TYPES)
supported_types = get_supported_file_types()
supported_keys = [t.get('id') for t in supported_types]
self.assertIn('dummy_type', supported_keys)
self.assertEqual(len(supported_keys), prev_count + 1)

@override_settings(ADDITIONAL_DATASET_FILE_TYPES=[
{
"id": "shp",
"label": "Replaced type",
"format": "vector",
"ext": ["shp"],
"requires": ["shp", "prj", "dbf", "shx"],
"optional": ["xml", "sld"]
},
])
def test_should_replace_the_type_id_if_already_exists(self):
prev_count = len(settings.SUPPORTED_DATASET_FILE_TYPES)
supported_types = get_supported_file_types()
supported_keys = [t.get('id') for t in supported_types]
self.assertIn('shp', supported_keys)
self.assertEqual(len(supported_keys), prev_count)
shp_type = [t for t in supported_types if t['id'] == "shp"][0]
self.assertEqual(shp_type['label'], "Replaced type")
23 changes: 23 additions & 0 deletions geonode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,3 +1876,26 @@ def get_xpath_value(
def get_geonode_app_types():
from geonode.geoapps.models import GeoApp
return list(set(GeoApp.objects.values_list('resource_type', flat=True)))


def get_supported_file_types():
mattiagiupponi marked this conversation as resolved.
Show resolved Hide resolved
from django.conf import settings as gn_settings
'''
Return a list of all supported file type in geonode
If one of the type provided in the custom type exists in the default
is going to override it
'''
default_types = settings.SUPPORTED_DATASET_FILE_TYPES
types_module = (
gn_settings.ADDITIONAL_DATASET_FILE_TYPES
if hasattr(gn_settings, "ADDITIONAL_DATASET_FILE_TYPES")
else []
)
supported_types = default_types.copy()
default_types_id = [t.get('id') for t in default_types]
for _type in types_module:
if _type.get("id") in default_types_id:
supported_types[default_types_id.index(_type.get("id"))] = _type
else:
supported_types.extend([_type])
return supported_types