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

[Fixes #11703] Documents can now be uploaded without specifying title via the REST API. #11872

Merged
merged 17 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
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 @@ -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)

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 @@ -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}")
Expand Down
4 changes: 2 additions & 2 deletions geonode/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion geonode/resource/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading