diff --git a/CHANGELOG.md b/CHANGELOG.md index e357e3ec..3820f25d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to ## Changed +- ♻️ Allow null titles on documents for easier creation #234 - 🛂(backend) stop to list public doc to everyone #234 - 🚚(frontend) change visibility in share modal #235 diff --git a/src/backend/core/migrations/0005_remove_document_is_public_alter_document_link_reach_and_more.py b/src/backend/core/migrations/0005_remove_document_is_public_alter_document_link_reach_and_more.py new file mode 100644 index 00000000..49c3983a --- /dev/null +++ b/src/backend/core/migrations/0005_remove_document_is_public_alter_document_link_reach_and_more.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1 on 2024-09-09 17:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0004_migrate_is_public_to_link_reach'), + ] + + operations = [ + migrations.AlterField( + model_name='document', + name='title', + field=models.CharField(blank=True, max_length=255, null=True, verbose_name='title'), + ), + ] diff --git a/src/backend/core/models.py b/src/backend/core/models.py index f5e1ba85..d7f19a2e 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -320,7 +320,7 @@ class BaseAccess(BaseModel): class Document(BaseModel): """Pad document carrying the content.""" - title = models.CharField(_("title"), max_length=255) + title = models.CharField(_("title"), max_length=255, null=True, blank=True) link_reach = models.CharField( max_length=20, choices=LinkReachChoices.choices, @@ -339,7 +339,7 @@ class Document(BaseModel): verbose_name_plural = _("Documents") def __str__(self): - return self.title + return str(self.title) if self.title else str(_("Untitled Document")) def save(self, *args, **kwargs): """Write content to object storage only if _content has changed.""" diff --git a/src/backend/core/tests/documents/test_api_documents_create.py b/src/backend/core/tests/documents/test_api_documents_create.py index ba125ff5..57ff4455 100644 --- a/src/backend/core/tests/documents/test_api_documents_create.py +++ b/src/backend/core/tests/documents/test_api_documents_create.py @@ -26,7 +26,7 @@ def test_api_documents_create_anonymous(): assert not Document.objects.exists() -def test_api_documents_create_authenticated(): +def test_api_documents_create_authenticated_success(): """ Authenticated users should be able to create documents and should automatically be declared as the owner of the newly created document. @@ -50,6 +50,21 @@ def test_api_documents_create_authenticated(): assert document.accesses.filter(role="owner", user=user).exists() +def test_api_documents_create_authenticated_title_null(): + """It should be possible to create several documents with a null title.""" + user = factories.UserFactory() + + client = APIClient() + client.force_login(user) + + factories.DocumentFactory(title=None) + + response = client.post("/api/v1.0/documents/", {}, format="json") + + assert response.status_code == 201 + assert Document.objects.filter(title__isnull=True).count() == 2 + + def test_api_documents_create_force_id_success(): """It should be possible to force the document ID when creating a document.""" user = factories.UserFactory() diff --git a/src/backend/core/tests/test_models_documents.py b/src/backend/core/tests/test_models_documents.py index fe81036f..3acb9f00 100644 --- a/src/backend/core/tests/test_models_documents.py +++ b/src/backend/core/tests/test_models_documents.py @@ -27,15 +27,15 @@ def test_models_documents_id_unique(): def test_models_documents_title_null(): - """The "title" field should not be null.""" - with pytest.raises(ValidationError, match="This field cannot be null."): - models.Document.objects.create(title=None) + """The "title" field can be null.""" + document = models.Document.objects.create(title=None) + assert document.title is None def test_models_documents_title_empty(): - """The "title" field should not be empty.""" - with pytest.raises(ValidationError, match="This field cannot be blank."): - models.Document.objects.create(title="") + """The "title" field can be empty.""" + document = models.Document.objects.create(title="") + assert document.title == "" def test_models_documents_title_max_length():