From 91de5c37b5abd522248ac868e95c73a41b5eecdd Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Mon, 4 Dec 2023 09:19:43 +0100 Subject: [PATCH 1/3] Documents can now be uploaded without specifying title via the REST API. --- geonode/documents/api/serializers.py | 2 ++ geonode/documents/models.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/geonode/documents/api/serializers.py b/geonode/documents/api/serializers.py index dc11cb201f5..534a6c3fafb 100644 --- a/geonode/documents/api/serializers.py +++ b/geonode/documents/api/serializers.py @@ -19,6 +19,7 @@ import logging from dynamic_rest.fields.fields import DynamicComputedField +from rest_framework import serializers from geonode.base.api.serializers import ResourceBaseSerializer from geonode.documents.models import Document @@ -39,6 +40,7 @@ class DocumentSerializer(ResourceBaseSerializer): def __init__(self, *args, **kwargs): # Instantiate the superclass normally super().__init__(*args, **kwargs) + self.fields["title"] = serializers.CharField(required=False) file_path = GeonodeFilePathField(required=False) doc_file = DocumentFieldField(required=False) diff --git a/geonode/documents/models.py b/geonode/documents/models.py index e9a81dac1dc..c5f1a49fdc2 100644 --- a/geonode/documents/models.py +++ b/geonode/documents/models.py @@ -85,7 +85,10 @@ def compact_permission_labels(cls): @property def name(self): if not self.title: - return str(self.id) + if len(self.files) > 0: + return self.files[0].split("/")[-1] + else: + return str(self.id) else: return self.title From 8e2d4352503672b11a06aeda5f97e82616c6790e Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Wed, 14 Feb 2024 15:20:07 +0100 Subject: [PATCH 2/3] Added test to verify that document is uploaded without specifying a title. --- geonode/documents/api/tests.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/geonode/documents/api/tests.py b/geonode/documents/api/tests.py index dbb3b13588b..b39c81a2d63 100644 --- a/geonode/documents/api/tests.py +++ b/geonode/documents/api/tests.py @@ -44,6 +44,7 @@ def setUp(self): self.url = reverse("documents-list") self.invalid_file_path = f"{settings.PROJECT_ROOT}/tests/data/thesaurus.rdf" self.valid_file_path = f"{settings.PROJECT_ROOT}/base/fixtures/test_xml.xml" + self.no_title_file_path = f"{settings.PROJECT_ROOT}/base/fixtures/test_sld.sld" def test_documents(self): """ @@ -142,6 +143,18 @@ def test_creation_should_create_the_doc(self): self.assertEqual("xml", extension) self.assertTrue(Document.objects.filter(title="New document for testing").exists()) + def test_uploading_doc_without_title(self): + """ + A document should be uploaded without specifying a title + """ + self.client.force_login(self.admin) + payload = {"document": {"metadata_only": True, "file_path": self.no_title_file_path}} + actual = self.client.post(self.url, data=payload, format="json") + self.assertEqual(201, actual.status_code) + extension = actual.json().get("document", {}).get("extension", "") + self.assertEqual("sld", extension) + self.assertTrue(Document.objects.filter(title="test_sld.sld").exists()) + def test_file_path_and_doc_path_are_not_returned(self): """ If file_path and doc_path should not be visible From 8e85224fb872a219a806a85021ff4c4675570659 Mon Sep 17 00:00:00 2001 From: ahmdthr Date: Thu, 15 Feb 2024 16:31:37 +0100 Subject: [PATCH 3/3] Removed unnecessary else statement. --- geonode/documents/models.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/geonode/documents/models.py b/geonode/documents/models.py index 191b4e0a2c8..b3889d5f87d 100644 --- a/geonode/documents/models.py +++ b/geonode/documents/models.py @@ -82,8 +82,6 @@ def name(self): if not self.title: if len(self.files) > 0: return self.files[0].split("/")[-1] - else: - return str(self.id) else: return self.title