From 9b44e021fdaa154ae9571f7a62838a13b592c5b5 Mon Sep 17 00:00:00 2001 From: Samuel Paccoud - DINUM Date: Mon, 9 Sep 2024 20:01:27 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(models)=20allow=20null=20tit?= =?UTF-8?q?les=20on=20documents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to make it as fast as possible to create a new document. We should not have any modal asking the title before creating the document but rather show an "untitle document" title and let the owner set it on the already created document. --- CHANGELOG.md | 1 + ...ublic_alter_document_link_reach_and_more.py | 18 ++++++++++++++++++ src/backend/core/models.py | 4 ++-- .../documents/test_api_documents_create.py | 17 ++++++++++++++++- .../core/tests/test_models_documents.py | 12 ++++++------ 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 src/backend/core/migrations/0005_remove_document_is_public_alter_document_link_reach_and_more.py 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():