🏷️(backend) accept string as saved document

Saved documents has to be a string now.
Before it has to be a json object.
This commit is contained in:
Anthony LC
2024-05-21 14:46:23 +02:00
committed by Anthony LC
parent 7aeea18202
commit 5c9e4ab3e6
7 changed files with 21 additions and 24 deletions

View File

@@ -7,8 +7,6 @@ from timezone_field.rest_framework import TimeZoneSerializerField
from core import models
from .fields import JSONField
class UserSerializer(serializers.ModelSerializer):
"""Serialize users."""
@@ -136,7 +134,7 @@ class BaseResourceSerializer(serializers.ModelSerializer):
class DocumentSerializer(BaseResourceSerializer):
"""Serialize documents."""
content = JSONField(required=False)
content = serializers.CharField(required=False)
class Meta:
model = models.Document

View File

@@ -344,7 +344,7 @@ class DocumentViewSet(
return drf_response.Response(
{
"content": json.loads(response["Body"].read()),
"content": response["Body"].read().decode("utf-8"),
"last_modified": response["LastModified"],
}
)

View File

@@ -35,7 +35,7 @@ class DocumentFactory(factory.django.DjangoModelFactory):
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):

View File

@@ -325,16 +325,15 @@ class Document(BaseModel):
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=""):
@@ -349,7 +348,7 @@ class Document(BaseModel):
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(

View File

@@ -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,
}
@@ -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,
}
@@ -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,
}
@@ -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,
}
@@ -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,
}
@@ -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,
}

View File

@@ -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(
@@ -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"]
@@ -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():
@@ -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"]
@@ -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"]

View File

@@ -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
@@ -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(