Skip to content

Commit

Permalink
🏷️(backend) accept string as saved document
Browse files Browse the repository at this point in the history
Saved documents has to be a string now.
Before it has to be a json object.
  • Loading branch information
AntoLC committed May 22, 2024
1 parent 363f852 commit 7aa945b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 24 deletions.
4 changes: 1 addition & 3 deletions src/backend/core/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from core import models

from .fields import JSONField


class UserSerializer(serializers.ModelSerializer):
"""Serialize users."""
Expand Down Expand Up @@ -136,7 +134,7 @@ def get_abilities(self, document) -> dict:
class DocumentSerializer(BaseResourceSerializer):
"""Serialize documents."""

content = JSONField(required=False)
content = serializers.CharField(required=False)

class Meta:
model = models.Document
Expand Down
2 changes: 1 addition & 1 deletion src/backend/core/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def versions_detail(self, request, pk, version_id, *args, **kwargs):

return drf_response.Response(
{
"content": json.loads(response["Body"].read()),
"content": response["Body"].read().decode("utf-8"),
"last_modified": response["LastModified"],
}
)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Meta:

title = factory.Sequence(lambda n: f"document{n}")
is_public = factory.Faker("boolean")
content = factory.LazyFunction(lambda: {"foo": fake.word()})
content = factory.Sequence(lambda n: f"content{n}")

@factory.post_generation
def users(self, create, extracted, **kwargs):
Expand Down
11 changes: 5 additions & 6 deletions src/backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,15 @@ def content(self):
except (FileNotFoundError, ClientError):
pass
else:
self._content = json.loads(response["Body"].read())
self._content = response["Body"].read().decode('utf-8')
return self._content

@content.setter
def content(self, content):
"""Cache the content, don't write to object storage yet"""
if isinstance(content, str):
content = json.loads(content)
if not isinstance(content, dict):
raise ValueError("content should be a json object.")
if not isinstance(content, str):
raise ValueError("content should be a string.")

self._content = content

def get_content_response(self, version_id=""):
Expand All @@ -305,7 +304,7 @@ def save(self, *args, **kwargs):

if self._content:
file_key = self.file_key
bytes_content = json.dumps(self._content).encode("utf-8")
bytes_content = self._content.encode("utf-8")

if default_storage.exists(file_key):
response = default_storage.connection.meta.client.head_object(
Expand Down
12 changes: 6 additions & 6 deletions src/backend/core/tests/documents/test_api_documents_retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_api_documents_retrieve_anonymous_public():
"accesses": [],
"title": document.title,
"is_public": True,
"content": {"foo": document.content["foo"]},
"content": document.content,
}


Expand Down Expand Up @@ -76,7 +76,7 @@ def test_api_documents_retrieve_authenticated_unrelated_public():
"accesses": [],
"title": document.title,
"is_public": True,
"content": {"foo": document.content["foo"]},
"content": document.content,
}


Expand Down Expand Up @@ -140,7 +140,7 @@ def test_api_documents_retrieve_authenticated_related_direct():
assert response.json() == {
"id": str(document.id),
"title": document.title,
"content": {"foo": document.content["foo"]},
"content": document.content,
"abilities": document.get_abilities(user),
"is_public": document.is_public,
}
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_api_documents_retrieve_authenticated_related_team_members(
assert response.json() == {
"id": str(document.id),
"title": document.title,
"content": {"foo": document.content["foo"]},
"content": document.content,
"abilities": document.get_abilities(user),
"is_public": False,
}
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_api_documents_retrieve_authenticated_related_team_administrators(
assert response.json() == {
"id": str(document.id),
"title": document.title,
"content": {"foo": document.content["foo"]},
"content": document.content,
"abilities": document.get_abilities(user),
"is_public": False,
}
Expand Down Expand Up @@ -455,7 +455,7 @@ def test_api_documents_retrieve_authenticated_related_team_owners(
assert response.json() == {
"id": str(document.id),
"title": document.title,
"content": {"foo": document.content["foo"]},
"content": document.content,
"abilities": document.get_abilities(user),
"is_public": False,
}
10 changes: 5 additions & 5 deletions src/backend/core/tests/test_api_document_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_api_document_versions_list_authenticated_related(via, mock_user_get_tea
assert len(content["versions"]) == 0

# Add a new version to the document
document.content = {"foo": "bar"}
document.content = "new content"
document.save()

response = client.get(
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_api_document_versions_retrieve_authenticated_related(via, mock_user_get

# Create a new version should make it available to the user
time.sleep(1) # minio stores datetimes with the precision of a second
document.content = {"foo": "bar"}
document.content = "new content"
document.save()

version_id = document.get_versions_slice()["versions"][0]["version_id"]
Expand All @@ -253,7 +253,7 @@ def test_api_document_versions_retrieve_authenticated_related(via, mock_user_get
)

assert response.status_code == 200
assert response.json()["content"] == {"foo": "bar"}
assert response.json()["content"] == "new content"


def test_api_document_versions_create_anonymous():
Expand Down Expand Up @@ -459,7 +459,7 @@ def test_api_document_versions_delete_member(via, mock_user_get_teams):

# Create a new version should make it available to the user
time.sleep(1) # minio stores datetimes with the precision of a second
document.content = {"foo": "bar"}
document.content = "new content"
document.save()

versions = document.get_versions_slice()["versions"]
Expand Down Expand Up @@ -503,7 +503,7 @@ def test_api_document_versions_delete_administrator_or_owner(via, mock_user_get_

# Create a new version should make it available to the user
time.sleep(1) # minio stores datetimes with the precision of a second
document.content = {"foo": "bar"}
document.content = "new content"
document.save()

versions = document.get_versions_slice()["versions"]
Expand Down
4 changes: 2 additions & 2 deletions src/backend/core/tests/test_models_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_models_documents_get_versions_slice(settings):
# Create a document with 7 versions
document = factories.DocumentFactory()
for i in range(6):
document.content = {"foo": f"bar{i:d}"}
document.content = f"bar{i:d}"
document.save()

# Add a version not related to the first document
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_models_documents_version_duplicate():
assert len(response["Versions"]) == 1

# Save modified content
document.content = {"foo": "spam"}
document.content = "new content"
document.save()

response = default_storage.connection.meta.client.list_object_versions(
Expand Down

0 comments on commit 7aa945b

Please sign in to comment.