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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -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
3 changes: 2 additions & 1 deletion geonode/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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
17 changes: 17 additions & 0 deletions geonode/documents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,23 @@ def test_document_isuploaded(self):
)
self.assertEqual(response.status_code, 200)

def test_document_upload_without_title(self):
"""/documents/upload -> Test uploading a document without a title"""

f = SimpleUploadedFile("test_img_file.gif", self.imgfile.read(), "image/gif")

self.client.login(username="admin", password="admin")
response = self.client.post(
f"{reverse('document_upload')}?no__redirect=true",
data={
"doc_file": f,
"type": "document",
},
)
self.assertEqual(response.status_code, 200)
d = Document.objects.get(title="test_img_file.gif")
self.assertIsNotNone(d)

# Permissions Tests

def test_set_document_permissions(self):
Expand Down