diff --git a/geonode/documents/api/serializers.py b/geonode/documents/api/serializers.py index 1fbcb8ed790..b0c63875060 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 @@ -36,6 +37,7 @@ def get_attribute(self, instance): class DocumentSerializer(ResourceBaseSerializer): + title = serializers.CharField(required=False) file_path = GeonodeFilePathField(required=False, write_only=True) doc_file = DocumentFieldField(required=False, write_only=True) diff --git a/geonode/documents/api/tests.py b/geonode/documents/api/tests.py index f14b5ada5ef..4d144748d41 100644 --- a/geonode/documents/api/tests.py +++ b/geonode/documents/api/tests.py @@ -45,6 +45,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): """ @@ -176,6 +177,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_patch_point_of_contact(self): document = Document.objects.first() url = urljoin(f"{reverse('documents-list')}/", f"{document.id}") diff --git a/geonode/documents/models.py b/geonode/documents/models.py index cdd069929a5..626cccb5933 100644 --- a/geonode/documents/models.py +++ b/geonode/documents/models.py @@ -84,8 +84,8 @@ def files(self): @property def name(self): - if not self.title: - return str(self.id) + if not self.title and len(self.files) > 0: + return self.files[0].split("/")[-1] else: return self.title diff --git a/geonode/resource/utils.py b/geonode/resource/utils.py index 4edfd9ccd47..9a303d6c003 100644 --- a/geonode/resource/utils.py +++ b/geonode/resource/utils.py @@ -361,7 +361,11 @@ def document_post_save(instance, *args, **kwargs): url = instance.doc_url Document.objects.filter(id=instance.id).update( - extension=instance.extension, subtype=instance.subtype, doc_url=instance.doc_url, csw_type=instance.csw_type + title=instance.title, + extension=instance.extension, + subtype=instance.subtype, + doc_url=instance.doc_url, + csw_type=instance.csw_type, ) if name and url and ext: