Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/document_upload_without_…
Browse files Browse the repository at this point in the history
…title' into thuenen_4.x
  • Loading branch information
ridoo committed Feb 20, 2024
2 parents 4d83a67 + 8e85224 commit 1eeaf71
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions geonode/documents/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions geonode/documents/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -143,6 +144,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}")
Expand Down
3 changes: 2 additions & 1 deletion geonode/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ 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 self.title

Expand Down

0 comments on commit 1eeaf71

Please sign in to comment.