Skip to content

Commit 9b44e02

Browse files
committed
♻️(models) allow null titles on documents
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.
1 parent 2c3eef4 commit 9b44e02

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to
1717

1818
## Changed
1919

20+
- ♻️ Allow null titles on documents for easier creation #234
2021
- 🛂(backend) stop to list public doc to everyone #234
2122
- 🚚(frontend) change visibility in share modal #235
2223

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.1 on 2024-09-09 17:49
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('core', '0004_migrate_is_public_to_link_reach'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='document',
15+
name='title',
16+
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='title'),
17+
),
18+
]

src/backend/core/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def _get_abilities(self, resource, user):
320320
class Document(BaseModel):
321321
"""Pad document carrying the content."""
322322

323-
title = models.CharField(_("title"), max_length=255)
323+
title = models.CharField(_("title"), max_length=255, null=True, blank=True)
324324
link_reach = models.CharField(
325325
max_length=20,
326326
choices=LinkReachChoices.choices,
@@ -339,7 +339,7 @@ class Meta:
339339
verbose_name_plural = _("Documents")
340340

341341
def __str__(self):
342-
return self.title
342+
return str(self.title) if self.title else str(_("Untitled Document"))
343343

344344
def save(self, *args, **kwargs):
345345
"""Write content to object storage only if _content has changed."""

src/backend/core/tests/documents/test_api_documents_create.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_api_documents_create_anonymous():
2626
assert not Document.objects.exists()
2727

2828

29-
def test_api_documents_create_authenticated():
29+
def test_api_documents_create_authenticated_success():
3030
"""
3131
Authenticated users should be able to create documents and should automatically be declared
3232
as the owner of the newly created document.
@@ -50,6 +50,21 @@ def test_api_documents_create_authenticated():
5050
assert document.accesses.filter(role="owner", user=user).exists()
5151

5252

53+
def test_api_documents_create_authenticated_title_null():
54+
"""It should be possible to create several documents with a null title."""
55+
user = factories.UserFactory()
56+
57+
client = APIClient()
58+
client.force_login(user)
59+
60+
factories.DocumentFactory(title=None)
61+
62+
response = client.post("/api/v1.0/documents/", {}, format="json")
63+
64+
assert response.status_code == 201
65+
assert Document.objects.filter(title__isnull=True).count() == 2
66+
67+
5368
def test_api_documents_create_force_id_success():
5469
"""It should be possible to force the document ID when creating a document."""
5570
user = factories.UserFactory()

src/backend/core/tests/test_models_documents.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ def test_models_documents_id_unique():
2727

2828

2929
def test_models_documents_title_null():
30-
"""The "title" field should not be null."""
31-
with pytest.raises(ValidationError, match="This field cannot be null."):
32-
models.Document.objects.create(title=None)
30+
"""The "title" field can be null."""
31+
document = models.Document.objects.create(title=None)
32+
assert document.title is None
3333

3434

3535
def test_models_documents_title_empty():
36-
"""The "title" field should not be empty."""
37-
with pytest.raises(ValidationError, match="This field cannot be blank."):
38-
models.Document.objects.create(title="")
36+
"""The "title" field can be empty."""
37+
document = models.Document.objects.create(title="")
38+
assert document.title == ""
3939

4040

4141
def test_models_documents_title_max_length():

0 commit comments

Comments
 (0)