Skip to content

Commit c767263

Browse files
lunikaAntoLC
authored andcommitted
⚡️(backend) remove content from Document serializer when asked
The frontend can fetch the retrieve endpoint just for having the title. Everytime, the content is added and to get the content, a request is made to the s3 bucket. A query string `without_content` can be used, if the value is `true` then the content is not added (and not also not fetch).
1 parent 9f9f269 commit c767263

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to
1414
- ✨(frontend) Can print a doc #1832
1515
- ✨(backend) manage reconciliation requests for user accounts #1878
1616
- 👷(CI) add GHCR workflow for forked repo testing #1851
17+
- ⚡️(backend) remove content from Document serializer when asked
1718
- ✨(backend) allow the duplication of subpages #1893
1819
- ✨(backend) Onboarding docs for new users #1891
1920

src/backend/core/api/serializers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,16 @@ def get_fields(self):
225225
fields = super().get_fields()
226226

227227
request = self.context.get("request")
228-
if request and request.method == "POST":
229-
fields["id"].read_only = False
228+
if request:
229+
if request.method == "POST":
230+
fields["id"].read_only = False
231+
if (
232+
serializers.BooleanField().to_internal_value(
233+
request.query_params.get("without_content", False)
234+
)
235+
is True
236+
):
237+
del fields["content"]
230238

231239
return fields
232240

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,3 +1057,48 @@ def test_api_documents_retrieve_permanently_deleted_related(role, depth):
10571057

10581058
assert response.status_code == 404
10591059
assert response.json() == {"detail": "Not found."}
1060+
1061+
1062+
def test_api_documents_retrieve_without_content():
1063+
"""
1064+
Test retrieve using without_content query string should remove the content in the response
1065+
"""
1066+
1067+
user = factories.UserFactory()
1068+
1069+
document = factories.DocumentFactory(creator=user, users=[(user, "owner")])
1070+
1071+
client = APIClient()
1072+
client.force_login(user)
1073+
1074+
with mock.patch("core.models.Document.content") as mock_document_content:
1075+
response = client.get(
1076+
f"/api/v1.0/documents/{document.id!s}/?without_content=true"
1077+
)
1078+
1079+
assert response.status_code == 200
1080+
1081+
payload = response.json()
1082+
assert "content" not in payload
1083+
mock_document_content.assert_not_called()
1084+
1085+
1086+
def test_api_documents_retrieve_without_content_invalid_value():
1087+
"""
1088+
Test retrieve using without_content query string but an invalid value
1089+
should return a 400
1090+
"""
1091+
1092+
user = factories.UserFactory()
1093+
1094+
document = factories.DocumentFactory(creator=user, users=[(user, "owner")])
1095+
1096+
client = APIClient()
1097+
client.force_login(user)
1098+
1099+
response = client.get(
1100+
f"/api/v1.0/documents/{document.id!s}/?without_content=invalid-value"
1101+
)
1102+
assert response.status_code == 400
1103+
1104+
assert response.json() == ["Must be a valid boolean."]

0 commit comments

Comments
 (0)